This repository has been archived by the owner on Jun 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathapi.template.yaml
297 lines (291 loc) · 10.7 KB
/
api.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
This template creates API Gateway and Lambda resources for the backend service.
Metadata:
AWS::ServerlessRepo::Application:
Name: realworld-serverless-application-backend-api
Description: >-
This template creates API Gateway and Lambda resources for the backend service.
Author: AWS Serverless Application Repository
SpdxLicenseId: Apache-2.0
Labels: [github, serverless, sam]
HomePageUrl: https://github.com/awslabs/realworld-serverless-application
SemanticVersion: 1.0.1
SourceCodeUrl: https://github.com/awslabs/realworld-serverless-application/tree/1.0.1
LicenseUrl: ../../../LICENSE
ReadmeUrl: ../../README.md
Parameters:
Stage:
Type: String
Description: The stage where the application is running in, e.g., dev, prod.
Conditions:
IsProd:
!Equals [!Ref Stage, "prod"]
Resources:
ApplicationsApi:
Type: AWS::Serverless::Api
Properties:
# Use DefinitionBody for swagger file so that we can use CloudFormation intrinsic functions within the swagger file
DefinitionBody:
'Fn::Transform':
Name: 'AWS::Include'
Parameters:
Location: '../../swagger/api.yaml'
StageName: Prod
TracingEnabled: true # Enable AWS X-Ray to help debug API requests
MethodSettings:
- ResourcePath: '/*'
HttpMethod: '*'
# Disable data trace in production to avoid logging customer sensitive information from requests and responses
DataTraceEnabled: !If [IsProd, false, true]
LoggingLevel: INFO
MetricsEnabled: true
ThrottlingRateLimit: 5
ThrottlingBurstLimit: 10
AccessLogSetting:
DestinationArn: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:${ApiAccessLogGroup}'
Format: '$context.identity.sourceIp $context.authorizer.claims.sub [$context.requestTime] "$context.httpMethod $context.resourcePath $context.protocol" $context.status $context.requestId $context.awsEndpointRequestId $context.xrayTraceId $context.responseLatency $context.integrationLatency "$context.error.message"'
OpenApiVersion: '3.0.0'
Cors:
AllowOrigin: "'*'"
AllowHeaders: "'authorization, content-type'"
ApplicationsApiUserPool:
Type: AWS::Cognito::UserPool
Properties:
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: true
RequireNumbers: true
TemporaryPasswordValidityDays: 1
UsernameAttributes:
- email
AutoVerifiedAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Mutable: false
ApiLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: '../../target/realworld-serverless-application-backend-1.0.1.jar'
Handler: 'software.amazon.serverless.apprepo.container.ApiLambdaHandler::handleRequest'
MemorySize: 1536
Timeout: 30
Runtime: java8
Tracing: Active # Enable AWS X-Ray to help debug API requests
Environment:
Variables:
STAGE: !Ref Stage
Policies:
- SSMParameterReadPolicy:
ParameterName: !Sub "applications/apprepo/${Stage}/*"
- DynamoDBCrudPolicy:
TableName: !Sub "{{resolve:ssm:/applications/apprepo/${Stage}/ddb/Applications/TableName:1}}"
- Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- kms:Encrypt
- kms:Decrypt
Resource: !GetAtt PaginationKey.Arn
Events:
AnyApi:
Type: Api
Properties:
RestApiId: !Ref ApplicationsApi
Path: '/*'
Method: '*'
AutoPublishAlias: live
DeploymentPreference:
Enabled: true
# Use AllAtOnce in non-production environment to accelerate deployment
Type: !If [IsProd, Canary10Percent15Minutes, AllAtOnce]
Alarms:
- !Ref ApiAvailability
- !Ref Api4xxErrors
- !Ref ApiLatencyP50
DependsOn: PaginationTokenTtlInSeconds
PaginationKey:
Type: AWS::KMS::Key
Properties:
Description: "Encrypt and decrypt pagination tokens for List APIs."
EnableKeyRotation: 'true'
KeyPolicy:
Version: "2012-10-17"
Id: key-policy
Statement:
- Sid: Enable Root User Permissions
Effect: Allow
Principal:
AWS:
- !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:root
Action: kms:*
Resource: '*'
# This role allows API Gateway to push execution and access logs to CloudWatch logs
ApiGatewayPushToCloudWatchRole:
Type: "AWS::IAM::Role"
Properties:
Description: "Push logs to CloudWatch logs from API Gateway"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- "apigateway.amazonaws.com"
Action: "sts:AssumeRole"
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
ApiAccount:
Type: "AWS::ApiGateway::Account"
Properties:
CloudWatchRoleArn: !GetAtt ApiGatewayPushToCloudWatchRole.Arn
ApiAccessLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/apigateway/AccessLog-${ApplicationsApi}
RetentionInDays: 3653
# Alarms used in canary deployment for Applications API.
ApiAvailability:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: false
AlarmDescription: !Sub 'Api availability is lower than 90%. Only used in canary deployment for Lambda ${ApiLambda}'
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: ApiName
Value: !Sub realworld-serverless-application-backend-${Stage}
EvaluationPeriods: 5
MetricName: 5XXErrors
Namespace: AWS/ApiGateway
Period: 60
Statistic: Average
Threshold: 0.1
TreatMissingData: notBreaching
Api4xxErrors:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: false
AlarmDescription: !Sub 'Api 4xx Errors are more than 30%. Only used in canary deployment for Lambda ${ApiLambda}'
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: ApiName
Value: !Sub realworld-serverless-application-backend-${Stage}
EvaluationPeriods: 5
MetricName: 4XXErrors
Namespace: AWS/ApiGateway
Period: 60
Statistic: Average
Threshold: 0.3
TreatMissingData: notBreaching
ApiLatencyP50:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: false
AlarmDescription: !Sub 'Api latency p50 is too high. Only used in canary deployment for Lambda ${ApiLambda}'
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: ApiName
Value: !Sub realworld-serverless-application-backend-${Stage}
EvaluationPeriods: 5
MetricName: Latency
Namespace: AWS/ApiGateway
Period: 60
ExtendedStatistic: p50
Threshold: 200
TreatMissingData: notBreaching
# Storing resources names to SSM
ApiLambdaName:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/lambda/ApiLambda/FunctionName"
Type: "String"
Value: !Ref ApiLambda
Description: "ApiLambda function name"
ApiLambdaArn:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/lambda/ApiLambda/FunctionArn"
Type: "String"
Value: !Ref ApiLambda
Description: "ApiLambda function ARN"
ApiLambdaVersion:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/lambda/ApiLambda/FunctionVersion"
Type: "String"
Value: !Ref ApiLambda.Version
Description: "ApiLambda function version for alias live"
ApiId:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/apigateway/ApplicationsApi/Id"
Type: "String"
Value: !Ref ApplicationsApi
Description: "ApplicationsApi Id"
ApiEndpoint:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/apigateway/ApplicationsApi/Endpoint"
Type: "String"
Value: !Sub "https://${ApplicationsApi}.execute-api.${AWS::Region}.amazonaws.com"
Description: "ApplicationsApi endpoint"
ApiStage:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/apigateway/ApplicationsApi/Stage"
Type: "String"
Value: !Ref ApplicationsApi.Stage
Description: "ApplicationsApi API Gateway stage"
ApiAccessLogGroupName:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/apigateway/ApplicationsApi/AccessLog/LogGroupName"
Type: "String"
Value: !Ref ApiAccessLogGroup
Description: "ApplicationsApi access log CloudWatch log group name"
ApiAccessLogGroupArn:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/apigateway/ApplicationsApi/AccessLog/LogGroupArn"
Type: "String"
Value: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:${ApiAccessLogGroup}'
Description: "ApplicationsApi access log CloudWatch log group ARN"
ApiUserPoolUserPoolId:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/cognito/userpool/ApplicationsApi/Id"
Type: "String"
Value: !Ref ApplicationsApiUserPool
Description: "ApplicationsApi Cognito User Pool ID."
ApiUserPoolUserPoolArn:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/cognito/userpool/ApplicationsApi/Arn"
Type: "String"
Value: !GetAtt ApplicationsApiUserPool.Arn
Description: "ApplicationsApi Cognito User Pool ARN."
PaginationKeyId:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/kms/pagination/KeyId"
Type: "String"
Value: !Ref PaginationKey
Description: "KMS pagination key Id."
PaginationKeyArn:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/kms/pagination/KeyArn"
Type: "String"
Value: !GetAtt PaginationKey.Arn
Description: "KMS pagination key ARN."
PaginationTokenTtlInSeconds:
Type: "AWS::SSM::Parameter"
Properties:
Name: !Sub "/applications/apprepo/${Stage}/configuration/pagination/TtlInSeconds"
Type: "String"
Value: 360
Description: "Pagination token TTL in seconds. The pagination token is used in List APIs."