forked from benkehoe/sfn-callback-urls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
384 lines (363 loc) · 11.6 KB
/
template.yaml
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Copyright 2019 Ben Kehoe
#
# 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.
Transform: 'AWS::Serverless-2016-10-31'
Metadata:
AWS::ServerlessRepo::Application:
Name: sfn-callback-urls
SemanticVersion: 1.1.0
Description: Easy handling of Step Function tokens as callback URLs
Author: Ben Kehoe
Labels:
- stepfunctions
- callback
- email
SpdxLicenseId: Apache-2.0
LicenseUrl: LICENSE
ReadmeUrl: README.md
HomePageUrl: https://github.com/benkehoe/sfn-callback-urls
SourceCodeUrl: https://github.com/benkehoe/sfn-callback-urls
Globals:
Function:
Runtime: python3.8
Parameters:
DisableEncryption:
Description: Disable encryption for callback payloads
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
EncryptionKeyArn:
Description: If encryption is enabled, set this to use your own KMS key, or set to NONE to create one
Type: String
Default: 'NONE'
EnableOutputParameters:
Description: Allow the use of query parameters to customize the result of callbacks
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
EnablePostActions:
Description: Allow the use of post actions, which pass the callback request body to Step Functions
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
VerboseLogging:
Description: Log requests and payloads
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
Conditions:
EncryptionEnabled:
Fn::Equals: [ !Ref DisableEncryption, "false" ]
CreateKey:
Fn::And:
- Fn::Equals: [ !Ref DisableEncryption, "false" ]
- Fn::Equals: [ !Ref EncryptionKeyArn, "NONE" ]
OutputParametersDisabled:
Fn::Equals: [ !Ref EnableOutputParameters, "false" ]
PostActionsDisabled:
Fn::Equals: [ !Ref EnablePostActions, "false" ]
VerboseLoggingEnabled:
Fn::Equals: [ !Ref VerboseLogging, "true" ]
Outputs:
Api:
Value: !Sub "https://${Api}.execute-api.${AWS::Region}.amazonaws.com/${ApiStage}"
Function:
Value: !Ref CreateUrls
Policy:
Value: !Ref CreateUrlsPolicy
Resources:
CreateUrlsPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: Permission to call the API and the function directly
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: execute-api:Invoke
Resource: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${Api}/${ApiStage}/*
- Effect: Allow
Action: lambda:Invoke
Resource: !GetAtt CreateUrls.Arn
Api:
Type: AWS::ApiGateway::RestApi
Properties:
Name: Callback URLs service for Step Functions
EndpointConfiguration:
Types:
- REGIONAL
CreateUrlsResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId: !GetAtt Api.RootResourceId
PathPart: urls
RestApiId: !Ref Api
CreateUrlsMethod:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST
ResourceId: !Ref CreateUrlsResource
RestApiId: !Ref Api
AuthorizationType: AWS_IAM
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${CreateUrlsForApi}/invocations"
CreateUrlsInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref CreateUrlsForApi
Action: "lambda:InvokeFunction"
Principal: apigateway.amazonaws.com
ProcessCallbackResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId: !GetAtt Api.RootResourceId
PathPart: respond
RestApiId: !Ref Api
ProcessCallbackGetMethod:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: GET
ResourceId: !Ref ProcessCallbackResource
RestApiId: !Ref Api
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${ProcessCallbackFunction}/invocations"
ProcessCallbackPostMethod:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST
ResourceId: !Ref ProcessCallbackResource
RestApiId: !Ref Api
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${ProcessCallbackFunction}/invocations"
ProcessCallbackInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref ProcessCallbackFunction
Action: "lambda:InvokeFunction"
Principal: apigateway.amazonaws.com
CreateUrlsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument: |
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
CreateUrlsLogsPolicy:
Type: AWS::IAM::Policy
Properties:
Roles:
- !Ref CreateUrlsRole
PolicyName: LambdaLogging
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogStream"
- "logs:CreateLogGroup"
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${CreateUrls}:*"
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${CreateUrlsForApi}:*"
- Effect: Allow
Action:
- "logs:PutLogEvents"
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${CreateUrls}:log-stream:*"
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${CreateUrlsForApi}:log-stream:*"
CreateUrlsForApi:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: ./src
Handler: create_urls.api_handler
Role: !GetAtt CreateUrlsRole.Arn
MemorySize: 1024
Environment:
Variables:
DISABLE_OUTPUT_PARAMETERS: {"Fn::If": [OutputParametersDisabled, "true", "false"]}
DISABLE_POST_ACTIONS: {"Fn::If": [PostActionsDisabled, "true", "false"]}
VERBOSE: {"Fn::If": [VerboseLoggingEnabled, "true", "false"]}
KEY_ID:
"Fn::If":
- EncryptionEnabled
- "Fn::If":
- CreateKey
- !Ref EncryptionKey
- !Ref EncryptionKeyArn
- !Ref AWS::NoValue
CreateUrls:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: ./src
Handler: create_urls.direct_handler
Role: !GetAtt CreateUrlsRole.Arn
MemorySize: 1024
Environment:
Variables:
DISABLE_OUTPUT_PARAMETERS: {"Fn::If": [OutputParametersDisabled, "true", "false"]}
DISABLE_POST_ACTIONS: {"Fn::If": [PostActionsDisabled, "true", "false"]}
VERBOSE: {"Fn::If": [VerboseLoggingEnabled, "true", "false"]}
API_ID: !Ref Api
STAGE: !Ref ApiStage
KEY_ID:
"Fn::If":
- EncryptionEnabled
- "Fn::If":
- CreateKey
- !Ref EncryptionKey
- !Ref EncryptionKeyArn
- !Ref AWS::NoValue
ProcessCallbackRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument: |
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
ProcessCallbackSfnPolicy:
Type: AWS::IAM::Policy
Properties:
Roles:
- !Ref ProcessCallbackRole
PolicyName: AccessSfn
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "states:SendTaskSuccess"
- "states:SendTaskFailure"
- "states:SendTaskHeartbeat"
Resource: "*"
ProcessCallbackLogsPolicy:
Type: AWS::IAM::Policy
Properties:
Roles:
- !Ref ProcessCallbackRole
PolicyName: LambdaLogging
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogStream"
- "logs:CreateLogGroup"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${ProcessCallbackFunction}:*"
- Effect: Allow
Action:
- "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${ProcessCallbackFunction}:log-stream:*"
ProcessCallbackFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: ./src
Handler: process_callback.handler
Role: !GetAtt ProcessCallbackRole.Arn
MemorySize: 1024
Timeout: 15
Environment:
Variables:
DISABLE_OUTPUT_PARAMETERS: {"Fn::If": [OutputParametersDisabled, "true", "false"]}
DISABLE_POST_ACTIONS: {"Fn::If": [PostActionsDisabled, "true", "false"]}
VERBOSE: {"Fn::If": [VerboseLoggingEnabled, "true", "false"]}
KEY_ID:
"Fn::If":
- EncryptionEnabled
- "Fn::If":
- CreateKey
- !Ref EncryptionKey
- !Ref EncryptionKeyArn
- !Ref AWS::NoValue
ApiDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- CreateUrlsMethod
- ProcessCallbackGetMethod
- ProcessCallbackPostMethod
Properties:
RestApiId: !Ref Api
ApiStage:
Type: AWS::ApiGateway::Stage
Properties:
RestApiId: !Ref Api
StageName: v1
DeploymentId: !Ref ApiDeployment
EncryptionKey:
Type: AWS::KMS::Key
Condition: CreateKey
Properties:
KeyPolicy: !Sub |
{
"Version": "2012-10-17",
"Id": "key-default-1",
"Statement": [{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::${AWS::AccountId}:root"},
"Action": "kms:*",
"Resource": "*"
}]
}
EncryptionKeyPolicy:
Type: AWS::IAM::Policy
Condition: EncryptionEnabled
Properties:
Roles:
- !Ref CreateUrlsRole
- !Ref ProcessCallbackRole
PolicyName: AccessEncryptionKey
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "kms:GenerateDataKey"
- "kms:Decrypt"
Resource:
"Fn::If":
- CreateKey
- !GetAtt EncryptionKey.Arn
- !Ref EncryptionKeyArn