-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemplate.yml
265 lines (247 loc) · 7.22 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Parameters:
Stage:
Type: String
Description: Stage to deploy resources to
AllowedValues:
- staging
- production
Conditions:
IsProduction: !Equals [ !Ref Stage, production ]
Globals:
Api:
OpenApiVersion: 3.0.1 # to avoid default stage creation
Function:
Runtime: nodejs12.x
MemorySize: 128
Timeout: 5
Tracing: Active
Tags:
project: my-project
environment: !Ref Stage
Resources:
ApiGwAccountConfig:
Type: "AWS::ApiGateway::Account"
Properties:
CloudWatchRoleArn: !GetAtt "ApiGatewayLoggingRole.Arn"
ApiGatewayLoggingRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- "apigateway.amazonaws.com"
Action: "sts:AssumeRole"
Path: "/"
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
BooksApi:
Type: AWS::Serverless::Api
Properties:
Name: !Sub "books-api-${Stage}"
StageName: !Ref Stage
MethodSettings:
- LoggingLevel: INFO
ResourcePath: '/*' # allows for logging on any resource
HttpMethod: '*' # allows for logging on any method
TracingEnabled: true
Variables:
LAMBDA_ALIAS: !Ref Stage
Auth:
Authorizers:
CognitoAuth:
UserPoolArn: !GetAtt CognitoUserPool.Arn
GetAllBooks:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${Stage}-books-get-all'
CodeUri: ./src/books/get-all/dist/
Handler: index.handler
AutoPublishAlias: !Ref Stage
DeploymentPreference:
Type: !If [IsProduction, Linear10PercentEvery1Minute, AllAtOnce]
Alarms:
- !Ref GetAllBooksAliasErrorMetricGreaterThanZeroAlarm
Environment:
Variables:
TABLE: !Ref BooksTable
Policies:
- DynamoDBReadPolicy:
TableName: !Ref BooksTable
Events:
ApiEvent:
Type: Api
Properties:
Path: /books
Method: get
RestApiId:
Ref: BooksApi
CreateBook:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${Stage}-books-create'
CodeUri: ./src/books/create/dist/
Handler: index.handler
AutoPublishAlias: !Ref Stage
DeploymentPreference:
Type: !If [IsProduction, Linear10PercentEvery1Minute, AllAtOnce]
Hooks:
PreTraffic: !Ref CreateBookPreTraffic
Alarms:
- !Ref CreateBookAliasErrorMetricGreaterThanZeroAlarm
Environment:
Variables:
TABLE: !Ref BooksTable
Policies:
- DynamoDBWritePolicy:
TableName: !Ref BooksTable
Events:
ApiEvent:
Type: Api
Properties:
Path: /books
Method: post
RestApiId:
Ref: BooksApi
Auth:
AuthorizationScopes:
- email
- !If
- IsProduction
- !Ref 'AWS::NoValue'
- aws.cognito.signin.user.admin
Authorizer: CognitoAuth
BooksTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: isbn
Type: String
TableName: !Sub '${Stage}-books'
Tags:
project: my-project
environment: !Ref Stage
SSESpecification:
SSEEnabled: true
CognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: !Sub "${Stage}-books-api-user-pool"
Policies:
PasswordPolicy:
MinimumLength: 6
RequireLowercase: false
RequireNumbers: true
RequireSymbols: false
RequireUppercase: false
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: false
UserPoolClient:
Type: "AWS::Cognito::UserPoolClient"
Properties:
UserPoolId: !Ref CognitoUserPool
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthFlows:
- implicit
AllowedOAuthScopes:
- email
- openid
CallbackURLs:
- https://aws.amazon.com/
SupportedIdentityProviders:
- COGNITO
ExplicitAuthFlows:
- ALLOW_REFRESH_TOKEN_AUTH
- !If
- IsProduction
- !Ref 'AWS::NoValue'
- ALLOW_USER_PASSWORD_AUTH
UserPoolDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: !Sub "book-api-${Stage}-${AWS::AccountId}"
UserPoolId: !Ref CognitoUserPool
CreateBookPreTraffic:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub 'CodeDeployHook_${Stage}-books-create-pre-traffic-check'
CodeUri: ./src/books/create-pre-traffic/dist/
Handler: index.handler
Environment:
Variables:
TABLE: !Ref BooksTable
FN_NEW_VERSION: !Ref CreateBook.Version
Tags:
project: my-project
environment: !Ref Stage
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref BooksTable
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- codedeploy:PutLifecycleEventHookExecutionStatus
Resource:
!Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: !Ref CreateBook.Version
CreateBookAliasErrorMetricGreaterThanZeroAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Lambda Function Error > 0
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: Resource
Value: !Sub '${GetAllBooks}:${Stage}'
- Name: FunctionName
Value: !Ref GetAllBooks
EvaluationPeriods: 2
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Threshold: 0
GetAllBooksAliasErrorMetricGreaterThanZeroAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Lambda Function Error > 0
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: Resource
Value: !Sub '${CreateBook}:${Stage}'
- Name: FunctionName
Value: !Ref CreateBook
EvaluationPeriods: 2
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Threshold: 0
Outputs:
ApiEndpoint:
Description: "API endpoint"
Value: !Sub "https://${BooksApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/"
UserPoolId:
Description: "User Pool Id"
Value: !Ref CognitoUserPool
UserPoolClientId:
Description: "User Pool Client Id"
Value: !Ref UserPoolClient
BooksTable:
Description: "DynamoDB table where we will be storing books"
Value: !Ref BooksTable