-
Notifications
You must be signed in to change notification settings - Fork 378
/
template.yml
305 lines (285 loc) · 10.7 KB
/
template.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Lambda Power Tuning resources.
Metadata:
AWS::ServerlessRepo::Application:
Name: aws-lambda-power-tuning
Description: AWS Lambda Power Tuning is an open-source tool that can help you visualize and fine-tune the memory/power configuration of Lambda functions. It runs in your AWS account - powered by AWS Step Functions - and it supports multiple optimization strategies.
Author: Alex Casalboni
SpdxLicenseId: Apache-2.0
LicenseUrl: LICENSE.txt
ReadmeUrl: README-SAR.md
Labels: ['lambda', 'power', 'state-machine', 'step-functions', 'optimization']
HomePageUrl: https://github.com/alexcasalboni/aws-lambda-power-tuning
SemanticVersion: 4.3.6
SourceCodeUrl: https://github.com/alexcasalboni/aws-lambda-power-tuning
Parameters:
PowerValues:
Type: List<Number>
Default: 128,256,512,1024,1536,3008
Description: Default RAM values, used only if not provided as execution input (comma-separated).
visualizationURL:
Type: String
Default: https://lambda-power-tuning.show/
Description: Stats visualization URL
lambdaResource:
Type: String
Default: '*'
Description: AWS Lambda resource (or prefix) to be used for IAM policies
totalExecutionTimeout:
Type: Number
Default: 300
MinValue: 10
MaxValue: 900
Description: Maximum invocation timeout (in seconds) for the Executor step, after which you get a States.Timeout error
permissionsBoundary:
Type: String
Default: ''
Description: The ARN of a permissions boundary to use for all functions' execution role.
payloadS3Bucket:
Type: String
Default: ''
Description: S3 bucket name used for large payloads (optional), included in IAM policy if not an empty string
payloadS3Key:
Type: String
Default: '*'
Description: S3 object key used for large payloads (optional), included in IAM policy if bucket is not an empty string (all objects are readable by default)
layerSdkName:
Type: String
Default: ''
Description: Name of the SDK layer, if customization is needed (optional)
logGroupRetentionInDays:
Type: Number
Default: 7
Description: The number of days to retain the log events in the Lambda log groups.
securityGroupIds:
Type: CommaDelimitedList
Default: ''
Description: List of Security Groups to use in every Lambda function's VPC Configuration (optional).
subnetIds:
Type: CommaDelimitedList
Default: ''
Description: List of Subnets to use in every Lambda function's VPC Configuration (optional).
stateMachineNamePrefix:
Type: String
MaxLength: 43
AllowedPattern: ^[a-zA-Z0-9\-_]*$
ConstraintDescription: Prefix must conform to StateMachineName requirements.
Default: 'powerTuningStateMachine'
Description: Prefix to the name of the StateMachine. The StackId will be appended to this value (optional).
Conditions:
UsePermissionsBoundary: !Not [!Equals [!Ref permissionsBoundary, '']]
S3BucketProvided: !Not [!Equals [!Ref payloadS3Bucket, '']]
UseLayerSdkName: !Not [!Equals [!Ref layerSdkName, '']]
UseSecurityGroupIds: !Not [!Equals [!Join ['', !Ref securityGroupIds], '']]
UseSubnetIds: !Not [!Equals [!Join ['', !Ref subnetIds], '']]
UseVPCConfig: !Or [Condition: UseSecurityGroupIds, Condition: UseSubnetIds]
Globals:
Function:
Runtime: nodejs20.x
MemorySize: 128
Timeout: !Ref totalExecutionTimeout
PermissionsBoundary: !If [UsePermissionsBoundary, !Ref permissionsBoundary, !Ref AWS::NoValue]
VpcConfig: !If [UseVPCConfig, {
SecurityGroupIds: !If [UseSecurityGroupIds, !Ref securityGroupIds, !Ref AWS::NoValue],
SubnetIds: !If [UseSubnetIds, !Ref subnetIds, !Ref AWS::NoValue]
}, !Ref AWS::NoValue]
Environment:
Variables:
defaultPowerValues: !Join [ ",", !Ref PowerValues ]
minRAM: '128'
baseCosts: '{"x86_64": {"ap-east-1":2.9e-9,"af-south-1":2.8e-9,"me-south-1":2.6e-9,"eu-south-1":2.4e-9,"ap-northeast-3":2.7e-9,"cn-north-1":0.0000000142,"cn-northwest-1":0.0000000142,"default":2.1e-9}, "arm64": {"default":1.7e-9}}'
sfCosts: '{"default": 0.000025,"us-gov-west-1": 0.00003,"ap-northeast-2": 0.0000271,"eu-south-1": 0.00002625,"af-south-1": 0.00002975,"us-west-1": 0.0000279,"eu-west-3": 0.0000297,"ap-east-1": 0.0000275,"me-south-1": 0.0000275,"ap-south-1": 0.0000285,"us-gov-east-1": 0.00003,"sa-east-1": 0.0000375,"cn-north-1":0.0001891,"cn-northwest-1":0.0001891}'
visualizationURL: !Ref visualizationURL
Resources:
SDKlayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !If [UseLayerSdkName, !Ref layerSdkName, AWS-SDK-v3]
Description: AWS SDK 3
ContentUri: ./layer-sdk
CompatibleRuntimes:
- nodejs20.x
LicenseInfo: 'Available under the MIT-0 license.'
RetentionPolicy: Retain
Metadata:
BuildMethod: nodejs20.x
initializerLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: initializer
Properties:
LogGroupName: !Sub '/aws/lambda/${initializer}'
RetentionInDays: !Ref logGroupRetentionInDays
initializer:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: initializer.handler
Layers:
- !Ref SDKlayer
Policies:
- AWSLambdaBasicExecutionRole # Only logs
- Version: '2012-10-17' # allow Lambda actions
Statement:
- Effect: Allow
Action:
- lambda:GetFunctionConfiguration
Resource: !Ref lambdaResource
publisher:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: publisher.handler
Layers:
- !Ref SDKlayer
Policies:
- AWSLambdaBasicExecutionRole # Only logs
- Version: '2012-10-17' # allow Lambda actions
Statement:
- Effect: Allow
Action:
- lambda:GetAlias
- lambda:GetFunctionConfiguration
- lambda:PublishVersion
- lambda:UpdateFunctionConfiguration
- lambda:CreateAlias
- lambda:UpdateAlias
Resource: !Ref lambdaResource
executorLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: executor
Properties:
LogGroupName: !Sub '/aws/lambda/${executor}'
RetentionInDays: !Ref logGroupRetentionInDays
executor:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: executor.handler
Layers:
- !Ref SDKlayer
Policies:
- AWSLambdaBasicExecutionRole # only logs
- Version: '2012-10-17' # allow Lambda actions
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
- lambda:GetFunctionConfiguration
Resource: !Ref lambdaResource
- !If
- S3BucketProvided # if S3 bucket is provided
- !Ref payloadS3Policy # allow read-only access to S3
- !Ref AWS::NoValue
payloadS3Policy:
Condition: S3BucketProvided
Type: AWS::IAM::ManagedPolicy
Properties:
Description: Lambda Power Tunning - S3 Read Only for large payloads
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:ListBucket # this is needed only to detect 404 errors correctly
- s3:GetObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${payloadS3Bucket}
- !Sub arn:${AWS::Partition}:s3:::${payloadS3Bucket}/${payloadS3Key} # payloadS3Key is * by default
cleanerLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: cleaner
Properties:
LogGroupName: !Sub '/aws/lambda/${cleaner}'
RetentionInDays: !Ref logGroupRetentionInDays
cleaner:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: cleaner.handler
Layers:
- !Ref SDKlayer
Policies:
- AWSLambdaBasicExecutionRole # only logs
- Version: '2012-10-17' # allow Lambda actions
Statement:
- Effect: Allow
Action:
- lambda:GetAlias
- lambda:DeleteAlias
- lambda:DeleteFunction # only by version/qualifier
Resource: !Ref lambdaResource
analyzerLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: analyzer
Properties:
LogGroupName: !Sub '/aws/lambda/${analyzer}'
RetentionInDays: !Ref logGroupRetentionInDays
analyzer:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: analyzer.handler
Timeout: 10
Policies:
- AWSLambdaBasicExecutionRole # only logs
optimizerLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: optimizer
Properties:
LogGroupName: !Sub '/aws/lambda/${optimizer}'
RetentionInDays: !Ref logGroupRetentionInDays
optimizer:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: optimizer.handler
Layers:
- !Ref SDKlayer
Policies:
- AWSLambdaBasicExecutionRole # only logs
- Version: '2012-10-17' # allow Lambda actions
Statement:
- Effect: Allow
Action:
- lambda:GetAlias
- lambda:PublishVersion
- lambda:UpdateFunctionConfiguration
- lambda:GetFunctionConfiguration
- lambda:CreateAlias
- lambda:UpdateAlias
Resource: !Ref lambdaResource
statemachineRole:
Type: AWS::IAM::Role
Properties:
PermissionsBoundary: !If [UsePermissionsBoundary, !Ref permissionsBoundary, !Ref AWS::NoValue]
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: !Sub states.${AWS::Region}.amazonaws.com
Action: sts:AssumeRole
powerTuningStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Name:
Fn::Join:
- '-'
- - !Ref stateMachineNamePrefix
- !Select [2, !Split ['/', !Ref AWS::StackId]]
Role: !GetAtt statemachineRole.Arn
DefinitionUri: statemachine/statemachine.asl.json
DefinitionSubstitutions:
initializerArn: !GetAtt initializer.Arn
publisherArn: !GetAtt publisher.Arn
executorArn: !GetAtt executor.Arn
cleanerArn: !GetAtt cleaner.Arn
analyzerArn: !GetAtt analyzer.Arn
optimizerArn: !GetAtt optimizer.Arn
totalExecutionTimeout: !Ref totalExecutionTimeout
Outputs:
StateMachineARN:
Value: !Ref powerTuningStateMachine