forked from alexcasalboni/aws-lambda-power-tuning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
248 lines (235 loc) · 9.17 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
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: 3.4.2
SourceCodeUrl: https://github.com/alexcasalboni/aws-lambda-power-tuning
Parameters:
PowerValues:
Type: List<Number>
Default: 128,256,512,1024,1536,3008
# Default: 128,192,256,320,384,448,512,576,640,704,768,832,896,960,1024,1088,1152,1216,1280,1344,1408,1472,1536,3008
# AllowedValues: ['128','192','256','320','384','448','512','576','640','704','768','832','896','960','1024','1088','1152','1216','1280','1344','1408','1472','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.
Conditions:
UsePermissionsBoundary: !Not [!Equals [!Ref permissionsBoundary, '']]
Globals:
Function:
Runtime: nodejs12.x
MemorySize: 128
Timeout: !Ref totalExecutionTimeout
PermissionsBoundary: !If [UsePermissionsBoundary, !Ref permissionsBoundary, !Ref AWS::NoValue]
Environment:
Variables:
defaultPowerValues: !Join [ ",", !Ref PowerValues ]
minRAM: '128'
baseCosts: '{"ap-east-1":2.9e-9,"af-south-1":2.8e-9,"me-south-1":2.6e-9,"eu-south-1":2.4e-9,"default":2.1e-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:
initializer:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: initializer.handler
Policies:
- AWSLambdaExecute # Managed Policy
- Version: '2012-10-17' # Policy Document
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
Policies:
- AWSLambdaExecute # Managed Policy
- Version: '2012-10-17' # Policy Document
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: !Ref lambdaResource
cleaner:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: cleaner.handler
Policies:
- AWSLambdaExecute # Managed Policy
- Version: '2012-10-17' # Policy Document
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:
- AWSLambdaExecute # Managed Policy
optimizer:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda
Handler: optimizer.handler
Policies:
- AWSLambdaExecute # Managed Policy
- Version: '2012-10-17' # Policy Document
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