forked from alexcasalboni/aws-lambda-power-tuning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
228 lines (216 loc) · 7.86 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
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.2.3
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
Globals:
Function:
Runtime: nodejs12.x
MemorySize: 128
Timeout: 300
Environment:
Variables:
defaultPowerValues: !Join [ ",", !Ref PowerValues ]
minRAM: '128'
minCost: '0.000000208'
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:
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",
"Catch": [{
"ErrorEquals": [ "States.ALL" ],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
}]
},
"Branching": {
"Type": "Map",
"Next": "Cleaner",
"ItemsPath": "$.powerValues",
"ResultPath": "$.stats",
"Parameters": {
"value.$": "$$.Map.Item.Value",
"lambdaARN.$": "$.lambdaARN",
"num.$": "$.num",
"payload.$": "$.payload",
"parallelInvocation.$": "$.parallelInvocation"
},
"MaxConcurrency": 0,
"Catch": [{
"ErrorEquals": ["States.ALL"],
"Next": "CleanUpOnError",
"ResultPath": "$.error"
}],
"Iterator": {
"StartAt": "Executor",
"States": {
"Executor": {
"Type": "Task",
"Resource": "${executorArn}",
"End": true,
"Retry": [{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 3,
"MaxAttempts": 2
}]
}
}
}
},
"Cleaner": {
"Type": "Task",
"Next": "Analyzer",
"ResultPath": null,
"Resource": "${cleanerArn}"
},
"Analyzer": {
"Type": "Task",
"Resource": "${analyzerArn}",
"ResultPath": "$.analysis",
"Next": "Optimizer"
},
"Optimizer": {
"Type": "Task",
"Resource": "${optimizerArn}",
"ResultPath": null,
"OutputPath": "$.analysis",
"End": true
},
"CleanUpOnError": {
"Type": "Task",
"ResultPath": null,
"OutputPath": null,
"Resource": "${cleanerArn}",
"End": true
}
}
}'
- initializerArn: !GetAtt initializer.Arn
executorArn: !GetAtt executor.Arn
cleanerArn: !GetAtt cleaner.Arn
analyzerArn: !GetAtt analyzer.Arn
optimizerArn: !GetAtt optimizer.Arn
Outputs:
StateMachineARN:
Value: !Ref powerTuningStateMachine