-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.yml
More file actions
349 lines (349 loc) · 13.4 KB
/
module.yml
File metadata and controls
349 lines (349 loc) · 13.4 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
---
# Copyright 2018 widdix GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
AWSTemplateFormatVersion: '2010-09-09'
Description: 'cfn-modules: AWS Lambda function'
# cfn-modules:implements(ExposeArn, ExposeName, LambdaDependency)
# TODO integrate with event sources
# * Amazon Simple Notification Service (via standalone module using AWS::SNS::Subscription)
# * Amazon Simple Email Service (via TODO)
# * Amazon Cognito (via TODO)
# * AWS CloudFormation (via TODO)
# * Amazon CloudWatch Logs (via TODO)
# * Amazon CloudWatch Events (via standalone module using AWS::Events::Rule)
# * AWS CodeCommit (via TODO)
# * AWS Config (via TODO)
# * Amazon Alexa (via TODO)
# * Amazon Lex (via TODO)
# * Amazon API Gateway (via AWS::Serverless::Api DefinitionBody, not a module)
# * AWS IoT Button (via TODO)
# * Amazon CloudFront (via TODO)
# * Amazon Kinesis Data Firehose (via TODO)
Parameters:
AlertingModule:
Description: 'Optional but recommended stack name of alerting module.'
Type: String
Default: ''
KmsKeyModule:
Description: 'Optional but recommended stack name of kms-key module.'
Type: String
Default: ''
VpcModule:
Description: 'Optional stack name of vpc module.'
Type: String
Default: ''
DeadLetterQueueModule:
Description: 'Optional but recommended for async invocations stack name of sqs-queue module where Lambda sends events to after the maximum number of retries was reached'
Type: String
Default: ''
FunctionName:
Description: 'An optional but recommended name for the function and log group.'
Type: String
Default: ''
Description:
Description: 'Optional description of the function'
Type: String
Default: ''
Handler:
Description: 'The name of the function (within your source code) that Lambda calls to start running your code (file must be in the lambda-src folder)'
Type: String
MemorySize:
Description: 'The amount of memory, in MB, that is allocated to your Lambda function'
Type: Number
Default: 128
MinValue: 128
MaxValue: 10240
ReservedConcurrentExecutions:
Type: Number
Default: -1
Runtime:
Description: 'The runtime environment for the Lambda function that you are uploading'
Type: String
Timeout:
Description: 'The function execution time at which Lambda should terminate the function'
Type: Number
Default: 3
MinValue: 1
MaxValue: 900
TracingConfigMode:
Type: String
Default: PassThrough
AllowedValues: [PassThrough, Active]
LogGroupRetentionInDays:
Description: 'The number of days log events are kept in CloudWatch Logs'
Type: Number
Default: 14
AllowedValues: [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653]
DependencyModule1:
Description: 'Optional stack name of module implementing LambdaDependency to generate IAM Policy'
Type: String
Default: ''
DependencyModule2:
Description: 'Optional stack name of module implementing LambdaDependency to generate IAM Policy'
Type: String
Default: ''
DependencyModule3:
Description: 'Optional stack name of module implementing LambdaDependency to generate IAM Policy'
Type: String
Default: ''
EnvironmentVariable1:
Description: 'Optional value of environment variable VARIABLE1'
Type: String
Default: ''
EnvironmentVariable2:
Description: 'Optional value of environment variable VARIABLE2'
Type: String
Default: ''
EnvironmentVariable3:
Description: 'Optional value of environment variable VARIABLE3'
Type: String
Default: ''
EnvironmentVariable4:
Description: 'Optional value of environment variable VARIABLE4'
Type: String
Default: ''
EnvironmentVariable5:
Description: 'Optional value of environment variable VARIABLE5'
Type: String
Default: ''
ManagedPolicyArns:
Description: 'Optional comma-delimited list of IAM managed policy ARNs to attach to the function''s IAM role'
Type: String
Default: ''
LayerArns:
Description: 'Optional comma-delimited list of Layer ARNs to attach to the function'
Type: String
Default: ''
ClientSgModule1:
Description: 'Optional stack name of client-sg module to mark traffic from the Lambda function.'
Type: String
Default: ''
ClientSgModule2:
Description: 'Optional stack name of client-sg module to mark traffic from the Lambda function.'
Type: String
Default: ''
ClientSgModule3:
Description: 'Optional stack name of client-sg module to mark traffic from the Lambda function.'
Type: String
Default: ''
Conditions:
HasAlertingModule: !Not [!Equals [!Ref AlertingModule, '']]
HasKmsKeyModule: !Not [!Equals [!Ref KmsKeyModule, '']]
HasDeadLetterQueueModule: !Not [!Equals [!Ref DeadLetterQueueModule, '']]
HasReservedConcurrentExecutions: !Not [!Equals [!Ref ReservedConcurrentExecutions, -1]]
HasVpcModule: !Not [!Equals [!Ref VpcModule, '']]
HasFunctionName: !Not [!Equals [!Ref FunctionName, '']]
HasDescription: !Not [!Equals [!Ref Description, '']]
HasDependencyModule1: !Not [!Equals [!Ref DependencyModule1, '']]
HasDependencyModule2: !Not [!Equals [!Ref DependencyModule2, '']]
HasDependencyModule3: !Not [!Equals [!Ref DependencyModule3, '']]
HasManagedPolicyArns: !Not [!Equals [!Ref ManagedPolicyArns, '']]
HasLayerArns: !Not [!Equals [!Ref LayerArns, '']]
HasClientSgModule1: !Not [!Equals [!Ref ClientSgModule1, '']]
HasClientSgModule2: !Not [!Equals [!Ref ClientSgModule2, '']]
HasClientSgModule3: !Not [!Equals [!Ref ClientSgModule3, '']]
HasTracingConfigModeActive: !Equals [!Ref TracingConfigMode, Active]
Resources:
Role:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: 'lambda.amazonaws.com'
Action: 'sts:AssumeRole'
ManagedPolicyArns: !If [HasManagedPolicyArns, !Split [',', !Ref ManagedPolicyArns], !Ref 'AWS::NoValue']
Policies:
- PolicyName: 'cfn-modules'
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: !GetAtt 'LogGroup.Arn'
- !If
- HasVpcModule
- Effect: Allow
Action:
- 'ec2:CreateNetworkInterface'
- 'ec2:DescribeNetworkInterfaces'
- 'ec2:DeleteNetworkInterface'
Resource: '*'
- !Ref 'AWS::NoValue'
- !If
- HasDeadLetterQueueModule
- Effect: Allow
Action: 'sqs:SendMessage'
Resource: {'Fn::ImportValue': !Sub '${DeadLetterQueueModule}-Arn'}
- !Ref 'AWS::NoValue'
- !If
- HasTracingConfigModeActive
- Effect: Allow
Action:
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
- 'xray:GetSamplingRules'
- 'xray:GetSamplingTargets'
- 'xray:GetSamplingStatisticSummaries'
Resource: '*'
- !Ref 'AWS::NoValue'
- !If
- HasDependencyModule1
- Effect: Allow
Action: !Split [',', {'Fn::ImportValue': !Sub '${DependencyModule1}-IamActions'}]
Resource: !Split [',', {'Fn::ImportValue': !Sub '${DependencyModule1}-IamResources'}]
- !Ref 'AWS::NoValue'
- !If
- HasDependencyModule2
- Effect: Allow
Action: !Split [',', {'Fn::ImportValue': !Sub '${DependencyModule2}-IamActions'}]
Resource: !Split [',', {'Fn::ImportValue': !Sub '${DependencyModule2}-IamResources'}]
- !Ref 'AWS::NoValue'
- !If
- HasDependencyModule3
- Effect: Allow
Action: !Split [',', {'Fn::ImportValue': !Sub '${DependencyModule3}-IamActions'}]
Resource: !Split [',', {'Fn::ImportValue': !Sub '${DependencyModule3}-IamResources'}]
- !Ref 'AWS::NoValue'
SecurityGroup:
Condition: HasVpcModule
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: !Ref 'AWS::StackName'
VpcId:
'Fn::ImportValue': !Sub '${VpcModule}-Id'
LogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: !If [HasFunctionName, !Sub '/aws/lambda/${FunctionName}', !Join ['', ['/aws/lambda/cfn-modules-lambda-', !Select [2, !Split ['/', !Ref 'AWS::StackId']]]]]
RetentionInDays: !Ref LogGroupRetentionInDays
Function:
Type: 'AWS::Lambda::Function'
DependsOn: LogGroup
Properties:
Code: '../../../lambda-src/' # we can not make this a parameter because `aws cloudformation package` does not take parameters into account
DeadLetterConfig: !If [HasDeadLetterQueueModule, {TargetArn: {'Fn::ImportValue': !Sub '${DeadLetterQueueModule}-Arn'}}, !Ref 'AWS::NoValue']
Description: !If [HasDescription, !Ref Description, !Ref AWS::NoValue]
Environment:
Variables:
VARIABLE1: !Ref EnvironmentVariable1
VARIABLE2: !Ref EnvironmentVariable2
VARIABLE3: !Ref EnvironmentVariable3
VARIABLE4: !Ref EnvironmentVariable4
VARIABLE5: !Ref EnvironmentVariable5
DEPENDENCY1_ARN: !If [HasDependencyModule1, {'Fn::ImportValue': !Sub '${DependencyModule1}-Arn'}, '']
DEPENDENCY2_ARN: !If [HasDependencyModule2, {'Fn::ImportValue': !Sub '${DependencyModule2}-Arn'}, '']
DEPENDENCY3_ARN: !If [HasDependencyModule3, {'Fn::ImportValue': !Sub '${DependencyModule3}-Arn'}, '']
FunctionName: !If [HasFunctionName, !Ref FunctionName, !Join ['', ['cfn-modules-lambda-', !Select [2, !Split ['/', !Ref 'AWS::StackId']]]]]
Handler: !Ref Handler
KmsKeyArn: !If [HasKmsKeyModule, {'Fn::ImportValue': !Sub '${KmsKeyModule}-Arn'}, !Ref 'AWS::NoValue']
MemorySize: !Ref MemorySize
ReservedConcurrentExecutions: !If [HasReservedConcurrentExecutions, !Ref ReservedConcurrentExecutions, !Ref 'AWS::NoValue']
Role: !GetAtt 'Role.Arn'
Runtime: !Ref Runtime
Timeout: !Ref Timeout
TracingConfig:
Mode: !Ref TracingConfigMode
VpcConfig: !If
- HasVpcModule
- SecurityGroupIds:
- !Ref SecurityGroup
- !If [HasClientSgModule1, {'Fn::ImportValue': !Sub '${ClientSgModule1}-SecurityGroupId'}, !Ref 'AWS::NoValue']
- !If [HasClientSgModule2, {'Fn::ImportValue': !Sub '${ClientSgModule2}-SecurityGroupId'}, !Ref 'AWS::NoValue']
- !If [HasClientSgModule3, {'Fn::ImportValue': !Sub '${ClientSgModule3}-SecurityGroupId'}, !Ref 'AWS::NoValue']
SubnetIds: !Split [',', {'Fn::ImportValue': !Sub '${VpcModule}-SubnetIdsPrivate'}]
- !Ref 'AWS::NoValue'
Layers: !If [HasLayerArns, !Split [',', !Ref LayerArns], !Ref 'AWS::NoValue']
EventInvokeConfig:
Condition: HasDeadLetterQueueModule
Type: 'AWS::Lambda::EventInvokeConfig'
Properties:
FunctionName: !Ref Function
MaximumEventAgeInSeconds: 60
MaximumRetryAttempts: 2
Qualifier: '$LATEST'
ErrorsTooHighAlarm:
Condition: HasAlertingModule
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmDescription: 'Invocations failed due to errors in the function'
Namespace: 'AWS/Lambda'
MetricName: Errors
Statistic: Sum
Period: 60
EvaluationPeriods: 1
ComparisonOperator: GreaterThanThreshold
Threshold: 0
TreatMissingData: notBreaching
AlarmActions:
- 'Fn::ImportValue': !Sub '${AlertingModule}-Arn'
OKActions:
- 'Fn::ImportValue': !Sub '${AlertingModule}-Arn'
Dimensions:
- Name: FunctionName
Value: !Ref Function
ThrottlesTooHighAlarm:
Condition: HasAlertingModule
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmDescription: 'Invocation attempts that were throttled due to invocation rates exceeding the concurrent limits'
Namespace: 'AWS/Lambda'
MetricName: Throttles
Statistic: Sum
Period: 60
EvaluationPeriods: 1
ComparisonOperator: GreaterThanThreshold
Threshold: 0
TreatMissingData: notBreaching
AlarmActions:
- 'Fn::ImportValue': !Sub '${AlertingModule}-Arn'
OKActions:
- 'Fn::ImportValue': !Sub '${AlertingModule}-Arn'
Dimensions:
- Name: FunctionName
Value: !Ref Function
Outputs:
ModuleId:
Value: 'lambda-function'
ModuleVersion:
Value: '2.10.0'
StackName:
Value: !Ref 'AWS::StackName'
Arn:
Value: !GetAtt 'Function.Arn'
Export:
Name: !Sub '${AWS::StackName}-Arn'
Name:
Value: !Ref Function
Export:
Name: !Sub '${AWS::StackName}-Name'
IamActions:
Value: 'lambda:InvokeFunction'
Export:
Name: !Sub '${AWS::StackName}-IamActions'
IamResources:
Value: !GetAtt Function.Arn
Export:
Name: !Sub '${AWS::StackName}-IamResources'
RoleArn:
Value: !GetAtt 'Role.Arn'
Export:
Name: !Sub '${AWS::StackName}-RoleArn'
RoleName:
Value: !Ref Role
Export:
Name: !Sub '${AWS::StackName}-RoleName'