forked from alexcasalboni/aws-lambda-power-tuning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
298 lines (283 loc) · 10.8 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
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.2.0
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)
Conditions:
UsePermissionsBoundary: !Not [!Equals [!Ref permissionsBoundary, '']]
S3BucketProvided: !Not [!Equals [!Ref payloadS3Bucket, '']]
Globals:
Function:
Runtime: nodejs14.x
MemorySize: 128
Timeout: !Ref totalExecutionTimeout
PermissionsBoundary: !If [UsePermissionsBoundary, !Ref permissionsBoundary, !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,"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}'
visualizationURL: !Ref visualizationURL
Resources:
SDKlayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: AWS-SDK-v2_989_0
Description: AWS SDK 2.989.0
ContentUri: ./layer-sdk
CompatibleRuntimes:
- nodejs14.x
LicenseInfo: 'Available under the MIT-0 license.'
RetentionPolicy: Retain
Metadata:
BuildMethod: nodejs14.x
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:GetAlias
- lambda:GetFunctionConfiguration
- lambda:PublishVersion
- lambda:UpdateFunctionConfiguration
- lambda:CreateAlias
- lambda:UpdateAlias
Resource: !Ref lambdaResource
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
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
analyzer:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: analyzer.handler
Timeout: 10
Policies:
- AWSLambdaBasicExecutionRole # only logs
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: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::StepFunctions::StateMachine
Properties:
RoleArn: !GetAtt statemachineRole.Arn
DefinitionString:
!Sub
- '
{
"Comment": "AWS Lambda Power Tuning state machine",
"StartAt": "Initializer",
"States": {
"Initializer": {
"Type": "Task",
"Resource": "${initializerArn}",
"Next": "Branching",
"ResultPath": "$.powerValues",
"TimeoutSeconds": ${totalExecutionTimeout},
"Catch": [{
"ErrorEquals": [ "States.ALL" ],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
}]
},
"Branching": {
"Type": "Map",
"Next": "Cleaner",
"ItemsPath": "$.powerValues",
"ResultPath": "$.stats",
"Parameters": {
"input.$": "$",
"value.$": "$$.Map.Item.Value"
},
"MaxConcurrency": 0,
"Catch": [{
"ErrorEquals": ["States.ALL"],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
}],
"Iterator": {
"StartAt": "Executor",
"States": {
"Executor": {
"Type": "Task",
"Resource": "${executorArn}",
"End": true,
"TimeoutSeconds": ${totalExecutionTimeout},
"Retry": [{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 3,
"MaxAttempts": 2
}]
}
}
}
},
"Cleaner": {
"Type": "Task",
"Next": "Analyzer",
"ResultPath": null,
"TimeoutSeconds": ${totalExecutionTimeout},
"Resource": "${cleanerArn}"
},
"Analyzer": {
"Type": "Task",
"Resource": "${analyzerArn}",
"ResultPath": "$.analysis",
"TimeoutSeconds": 10,
"Next": "Optimizer"
},
"Optimizer": {
"Type": "Task",
"Resource": "${optimizerArn}",
"ResultPath": null,
"OutputPath": "$.analysis",
"TimeoutSeconds": ${totalExecutionTimeout},
"End": true
},
"CleanUpOnError": {
"Type": "Task",
"ResultPath": null,
"OutputPath": null,
"Resource": "${cleanerArn}",
"TimeoutSeconds": ${totalExecutionTimeout},
"End": true
}
}
}'
- initializerArn: !GetAtt initializer.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