-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.sam
More file actions
282 lines (267 loc) · 10.1 KB
/
template.sam
File metadata and controls
282 lines (267 loc) · 10.1 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
AWSTemplateFormatVersion: 2010-09-09
Description: REST services for the Task API. Includes API, SNS Topic, and DynamoDB Table
# Transform section specifies one or more macros that AWS CloudFormation uses to process your template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Transform:
- AWS::Serverless-2016-10-31
Parameters:
TargetEnvironment:
Description: 'Examples can be dev, test or prod'
Type: 'String'
ProductName:
Description: 'Represents the name of the product you want to call the deployment'
Type: 'String'
Resources:
# DynamoDB Table for storing the Task records
TaskTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub '${ProductName}-${TargetEnvironment}-dynamodb-taskstore'
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: taskId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: taskId
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 2
WriteCapacityUnits: 2
# API Gateway used by the Task API.
# Auth setup example from https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_cognito_auth/template.yaml
TaskApi:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref TargetEnvironment
Name: !Sub '${ProductName}-${TargetEnvironment}-apigateway-task'
Auth:
DefaultAuthorizer: ApiReadTaskAuthorizer
Authorizers:
ApiDeleteTaskAuthorizer:
UserPoolArn: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-customeruserpoolarn'}
Identity:
Header: 'Authorization'
AuthorizationScopes:
- !Sub "app.${ProductName}.api.task/task.delete"
ApiWriteTaskAuthorizer:
UserPoolArn: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-customeruserpoolarn'}
Identity:
Header: 'Authorization'
AuthorizationScopes:
- !Sub "app.${ProductName}.api.task/task.write"
ApiReadTaskAuthorizer:
UserPoolArn: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-customeruserpoolarn'}
Identity:
Header: 'Authorization'
AuthorizationScopes:
- !Sub "app.${ProductName}.api.task/task.read"
# Task SNS subscriber for Project deletions
projectTopicDeletionFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: src/handlers/project-deleted-event-handler.projectDeletedEventHandler
Runtime: nodejs12.x
Role: !GetAtt projectDeletionFunctionRole.Arn
FunctionName: !Sub "${ProductName}-${TargetEnvironment}-lambda-event_task_project_deleted"
Description: 'Deletes Tasks that are related to a project being deleted.'
Environment:
Variables:
deployed_environment: !Ref TargetEnvironment
dynamodb_taskTable: !Ref TaskTable
dynamodb_endpointUrl: ''
sns_project_topic: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-sns-ProjectTopic'}
MemorySize: 256
Timeout: 300
Tracing: Active
ProjectTopicLambdaSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: lambda
Endpoint: !GetAtt projectTopicDeletionFunction.Arn
TopicArn: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-sns-ProjectTopic'}
FilterPolicy:
Event:
- 'project-deleted'
ProjectTopicSubscriberLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref projectTopicDeletionFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: {'Fn::ImportValue': !Sub '${ProductName}-${TargetEnvironment}-sns-ProjectTopic'}
postItemFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${ProductName}-${TargetEnvironment}-lambda-api_task_post'
Handler: src/handlers/post-handler.postHandler
Runtime: nodejs12.x
MemorySize: 256
Timeout: 10
Description: HTT POST handler for the /task API endpoint
Role: !GetAtt postItemFunctionRole.Arn
Environment:
Variables:
deployed_environment: !Ref TargetEnvironment
dynamodb_taskTable: !Ref TaskTable
dynamodb_endpointUrl: ''
Events:
Api:
Type: Api
Properties:
Auth:
Authorizer: ApiWriteTaskAuthorizer
Path: /
Method: POST
RestApiId:
Ref: TaskApi
getAllItemsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/get-all-handler.getAllHandler
FunctionName: !Sub '${ProductName}-${TargetEnvironment}-lambda-api_task_getall'
Runtime: nodejs12.x
MemorySize: 256
Timeout: 100
Description: HTTP GET handler for the /task API endpoint to retrieve all Tasks for a user.
Role: !GetAtt getAllItemsFunctionRole.Arn
Environment:
Variables:
deployed_environment: !Ref TargetEnvironment
dynamodb_taskTable: !Ref TaskTable
dynamodb_endpointUrl: ''
Events:
Api:
Type: Api
Properties:
Auth:
Authorizer: ApiReadTaskAuthorizer
Path: /
Method: GET
RestApiId:
Ref: TaskApi
projectDeletionFunctionRole:
Type: AWS::IAM::Role
Properties:
Description: !Sub 'Allows deleting existing records from the ${TaskTable} table'
RoleName: !Sub '${ProductName}-${TargetEnvironment}-role-api_task_project_topic'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: !Sub '${ProductName}-${TargetEnvironment}-policy-api_task_project_topic'
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'dynamodb:DeleteItem'
- 'dynamodb:Query'
Resource: !GetAtt TaskTable.Arn
- Effect: Allow
Action:
- 'xray:CreateGroup'
- 'xray:CreateSamplingRule'
Resource:
- 'arn:aws:xray:*:*:group/*/*'
- 'arn:aws:xray:*:*:sampling-rule/*'
- Effect: Allow
Action:
- 'xray:PutTelemetryRecords'
- 'xray:PutTraceSegments'
Resource: '*'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
# IAM Role used by the Lambda for handling HTTP POST requests that create projects.
postItemFunctionRole:
Type: AWS::IAM::Role
Properties:
Description: !Sub 'Allows inserting new records into the ${TaskTable} table'
RoleName: !Sub '${ProductName}-${TargetEnvironment}-role-api_task_post'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: !Sub '${ProductName}-${TargetEnvironment}-policy-api_task_post'
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'dynamodb:PutItem'
Resource: !GetAtt TaskTable.Arn
- Effect: Allow
Action:
- 'xray:CreateGroup'
- 'xray:CreateSamplingRule'
Resource:
- 'arn:aws:xray:*:*:group/*/*'
- 'arn:aws:xray:*:*:sampling-rule/*'
- Effect: Allow
Action:
- 'xray:PutTelemetryRecords'
- 'xray:PutTraceSegments'
Resource: '*'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
# IAM Role used by the Lambda for handling HTTP GET requests that queries tasks.
getAllItemsFunctionRole:
Type: AWS::IAM::Role
Properties:
Description: !Sub 'Allows querying records from the ${TaskTable} table that represent a Task'
RoleName: !Sub '${ProductName}-${TargetEnvironment}-role-api_task_getall'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: !Sub '${ProductName}-${TargetEnvironment}-policy-api_task_getall'
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'dynamodb:Query'
Resource: !GetAtt TaskTable.Arn
- Effect: Allow
Action:
- 'xray:CreateGroup'
- 'xray:CreateSamplingRule'
Resource:
- 'arn:aws:xray:*:*:group/*/*'
- 'arn:aws:xray:*:*:sampling-rule/*'
- Effect: Allow
Action:
- 'xray:PutTelemetryRecords'
- 'xray:PutTraceSegments'
Resource: '*'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
Outputs:
ApiEndPoint:
Description: "API Gateway endpoint URL for target environment stage"
Value: !Sub "https://${TaskApi}.execute-api.${AWS::Region}.amazonaws.com/${TargetEnvironment}/"
ApiId:
Description: "ID of the Task API resource"
Value: !Ref TaskApi
Export:
Name: !Sub '${ProductName}-${TargetEnvironment}-apigateway-task'