forked from aws-samples/webhooks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.yml
355 lines (338 loc) · 9.93 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
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
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: Sample architecture to receive webhooks
Parameters:
BasicAuthUser:
Type: String
Description: Basic Authentication user name
Default: ""
NoEcho: true
BasicAuthPassword:
Type: String
Description: Basic Authentication password
Default: ""
NoEcho: true
WebhookSecret:
Type: String
Description: Webhook Secret
NoEcho: true
BucketPrefix:
Type: String
Description: S3 Bucket Prefix
Default: "raw/"
Globals:
Function:
Architectures:
- arm64
Environment:
Variables:
LOG_LEVEL: info
Handler: app.lambda_handler.handler
Layers:
- !FindInMap [RegionMap, !Ref "AWS::Region", PowertoolsArn]
MemorySize: 128 # megabytes
Runtime: python3.12
Timeout: 5 # seconds
Tracing: Active
Mappings:
RegionMap:
"us-east-1":
# @see https://docs.powertools.aws.dev/lambda/python/latest/#lambda-layer
PowertoolsArn: "arn:aws:lambda:us-east-1:017000801446:layer:AWSLambdaPowertoolsPythonV2-Arm64:60"
Resources:
DependencyLayer:
Type: "AWS::Serverless::LayerVersion"
Metadata:
BuildMethod: python3.12
BuildArchitecture: arm64
Properties:
LicenseInfo: MIT-0
CompatibleArchitectures:
- arm64
CompatibleRuntimes:
- python3.12
ContentUri: src/dependencies
Description: !Sub "${AWS::StackName} - Dependency Layer"
RetentionPolicy: Delete
EncryptionKey:
Type: "AWS::KMS::Key"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Properties:
Description: !Sub "${AWS::StackName} - Encryption Key"
Enabled: true
EnableKeyRotation: true
KeyPolicy:
Version: "2012-10-17"
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:root"
Action: "kms:*"
Resource: "*"
- Sid: Allow encryption by webhoook function
Effect: Allow
Principal:
AWS: !GetAtt WebhookFunctionRole.Arn
Action:
- "kms:DescribeKey"
- "kms:Encrypt"
- "kms:GenerateDataKey"
Resource: "*"
KeySpec: SYMMETRIC_DEFAULT
KeyUsage: ENCRYPT_DECRYPT
MultiRegion: false
PendingWindowInDays: 7
EncryptionAlias:
Type: "AWS::KMS::Alias"
Properties:
AliasName: !Sub "alias/${AWS::StackName}"
TargetKeyId: !Ref EncryptionKey
WebhookParameter:
Type: "AWS::SSM::Parameter"
Properties:
Description: Webhook Credential
Name: "/webhook/credentials"
Type: String
Value: !Sub |-
{
"basic_auth_user": "${BasicAuthUser}",
"basic_auth_password": "${BasicAuthPassword}",
"webhook_secret": "${WebhookSecret}"
}
Table:
Type: "AWS::DynamoDB::GlobalTable"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Properties:
AttributeDefinitions:
- AttributeName: pk
AttributeType: S
- AttributeName: sk
AttributeType: S
- AttributeName: gsi1pk
AttributeType: S
- AttributeName: gsi1sk
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: pk
KeyType: HASH
- AttributeName: sk
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: gsi1
KeySchema:
- AttributeName: gsi1pk
KeyType: HASH
- AttributeName: gsi1sk
KeyType: RANGE
Projection:
ProjectionType: ALL
Replicas:
- PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
Region: !Ref "AWS::Region"
TableClass: STANDARD
SSESpecification:
SSEEnabled: true
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
TimeToLiveSpecification:
AttributeName: expire_at
Enabled: true
HttpApi:
Type: "AWS::Serverless::HttpApi"
Properties:
CorsConfiguration:
AllowHeaders:
- "*"
AllowMethods:
- POST
AllowOrigins:
- "*"
Description: !Sub "${AWS::StackName} - Webhook API"
Name: webhook
DisableExecuteApiEndpoint: false
WebhookFunctionLogGroup:
Type: "AWS::Logs::LogGroup"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
cfn_nag:
rules_to_suppress:
- id: W84
reason: "Ignoring KMS key"
Properties:
LogGroupName: !Sub "/aws/lambda/${WebhookFunction}"
RetentionInDays: 3
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: WebhookFunctionLogGroup
WebhookFunctionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: !Sub "lambda.${AWS::URLSuffix}"
Action: "sts:AssumeRole"
Description: !Sub "DO NOT DELETE - Used by Lambda. Created by CloudFormation ${AWS::StackId}"
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/AWSXRayDaemonWriteAccess"
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: WebhookFunctionRole
WebhookFunctionPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: WebhookFunctionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "s3:PutObject"
Resource: !Sub "${Bucket.Arn}/${BucketPrefix}*"
Condition:
ArnEquals:
"lambda:SourceFunctionArn": !GetAtt WebhookFunction.Arn
- Effect: Allow
Action:
- "kms:DescribeKey"
- "kms:Encrypt"
- "kms:GenerateDataKey"
Resource: !GetAtt EncryptionKey.Arn
- Effect: Allow
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
Resource: !GetAtt Table.Arn
- Effect: Allow
Action: "ssm:GetParameter"
Resource: !Sub "arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter${WebhookParameter}"
Roles:
- !Ref WebhookFunctionRole
CloudWatchLogsPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: CloudWatchLogs
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !GetAtt WebhookFunctionLogGroup.Arn
Roles:
- !Ref WebhookFunctionRole
WebhookFunction:
Type: "AWS::Serverless::Function"
Metadata:
cfn_nag:
rules_to_suppress:
- id: W58
reason: "Ignoring CloudWatch"
- id: W89
reason: "Ignoring VPC"
- id: W92
reason: "Ignoring Reserved Concurrency"
Properties:
CodeUri: src/webhook
Description: !Sub "${AWS::StackName} - Webhook Function"
Events:
HttpApiEvent:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Environment:
Variables:
BUCKET_NAME: !Ref Bucket
BUCKET_OWNER_ID: !Ref "AWS::AccountId"
BUCKET_PREFIX: !Ref BucketPrefix
TABLE_NAME: !Ref Table
KMS_KEY_ID: !Ref EncryptionKey
SSM_PARAMETER: !Ref WebhookParameter
Layers:
- !Ref DependencyLayer
Role: !GetAtt WebhookFunctionRole.Arn
Bucket:
Type: "AWS::S3::Bucket"
Metadata:
cfn_nag:
rules_to_suppress:
- id: W35
reason: "Ignoring access logging"
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- BucketKeyEnabled: true
ServerSideEncryptionByDefault:
KMSMasterKeyID: !GetAtt EncryptionKey.Arn
SSEAlgorithm: "aws:kms"
LifecycleConfiguration:
Rules:
- ExpirationInDays: 3
Id: RetentionRule
Status: Enabled
NotificationConfiguration:
EventBridgeConfiguration:
EventBridgeEnabled: true
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerEnforced
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
VersioningConfiguration:
Status: Enabled
BucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref Bucket
PolicyDocument:
Statement:
- Sid: AllowSSLRequestsOnly
Effect: Deny
Principal: "*"
Action: "s3:*"
Resource:
- !Sub "${Bucket.Arn}/*"
- !GetAtt Bucket.Arn
Condition:
Bool:
"aws:SecureTransport": false
- Sid: DenyUnEncryptedObjectUploads
Effect: Deny
Principal: "*"
Action: "s3:PutObject"
Resource: !Sub "${Bucket.Arn}/*"
Condition:
StringNotEquals:
"s3:x-amz-server-side-encryption": "aws:kms"
Outputs:
WebhookUrl:
Description: Webhook API URL
Value: !Sub "https://${HttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/"
KmsKeyArn:
Description: KMS Key ARN
Value: !GetAtt EncryptionKey.Arn
BucketName:
Description: S3 Bucket Name
Value: !Ref Bucket
BucketArn:
Description: S3 Bucket ARN
Value: !GetAtt Bucket.Arn