-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.yml
315 lines (297 loc) · 9.79 KB
/
main.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
AWSTemplateFormatVersion: 2010-09-09
Description: Events API v2
Transform: AWS::Serverless-2016-10-31
Parameters:
RunEnvironment:
Type: String
AllowedValues: [ 'dev', 'prod' ]
AlertSNSArn:
Description: SNS Queue to send alarm alerts to (prod only)
Type: String
Default: arn:aws:sns:us-east-1:298118738376:infra-monitor-alerts
ApplicationPrefix:
Type: String
Description: Application prefix, no ending dash
AllowedPattern: ^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$
ApplicationFriendlyName:
Type: String
Description: Application friendly name that will be used in resource descriptions
VpcRequired:
Description: Set to true if the default lambda should be attached to a VPC and a NoVPC version of the lambda should be created. Lambdas to be attached to VPCs to access Postgres.
Default: false
Type: String
AllowedValues: [true, false]
Conditions:
IsProd: !Equals [!Ref RunEnvironment, 'prod']
ShouldAttachVpc:
!Equals [true, !Ref VpcRequired]
Mappings:
General:
dev:
LogRetentionDays: 7
prod:
LogRetentionDays: 30
ApiGwConfig:
dev:
ApiCertificateArn: arn:aws:acm:us-east-1:427040638965:certificate/63ccdf0b-d2b5-44f0-b589-eceffb935c23
HostedZoneId: Z04502822NVIA85WM2SML
ApiDomainName: "aws.qa.acmuiuc.org"
prod:
ApiCertificateArn: arn:aws:acm:us-east-1:298118738376:certificate/6142a0e2-d62f-478e-bf15-5bdb616fe705
HostedZoneId: Z05246633460N5MEB9DBF
ApiDomainName: "aws.acmuiuc.org" # CHANGE ME
EnvironmentToCidr:
dev:
SecurityGroupIds:
- sg-00443e61d39721a0d
- sg-0e99aa1c1e4e9e5b6
SubnetIds:
- subnet-0cd2b887e61f6edd8
- subnet-065857a2bdf3a51aa
- subnet-09e12f156435e4cc9
prod:
SecurityGroupIds:
- sg-066e3ae1d05e70fcd
SubnetIds:
- subnet-0cf33aff80b81eb1b
- subnet-0cbe89f6ab2665610
Resources:
AppSecurityRoles:
Type: AWS::Serverless::Application
Properties:
Location: ./iam.yml
Parameters:
RunEnvironment: !Ref RunEnvironment
LambdaFunctionName: !Sub ${ApplicationPrefix}-lambda
AppLogGroups:
Type: AWS::Serverless::Application
Properties:
Location: ./logs.yml
Parameters:
LambdaFunctionName: !Sub ${ApplicationPrefix}-lambda
LogRetentionDays: !FindInMap [General, !Ref RunEnvironment, LogRetentionDays]
AppApiLambdaFunction:
Type: AWS::Serverless::Function
DependsOn:
- AppLogGroups
Properties:
CodeUri: ../dist
AutoPublishAlias: live
Runtime: nodejs20.x
Description: !Sub "${ApplicationFriendlyName} API Lambda"
FunctionName: !Sub ${ApplicationPrefix}-lambda
Handler: api/lambda.handler
MemorySize: 512
Role: !GetAtt AppSecurityRoles.Outputs.MainFunctionRoleArn
Timeout: 60
Environment:
Variables:
RunEnvironment: !Ref RunEnvironment
VpcConfig:
Ipv6AllowedForDualStack: !If [ShouldAttachVpc, True, !Ref AWS::NoValue]
SecurityGroupIds: !If [ShouldAttachVpc, !FindInMap [EnvironmentToCidr, !Ref RunEnvironment, SecurityGroupIds], !Ref AWS::NoValue]
SubnetIds: !If [ShouldAttachVpc, !FindInMap [EnvironmentToCidr, !Ref RunEnvironment, SubnetIds], !Ref AWS::NoValue]
Events:
ApiEvent:
Type: Api
Properties:
RestApiId: !Ref AppApiGateway
Path: /{proxy+}
Method: ANY
IamGroupRolesTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: "Retain"
Properties:
BillingMode: 'PAY_PER_REQUEST'
TableName: infra-core-api-iam-grouproles
DeletionProtectionEnabled: true
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: false
AttributeDefinitions:
- AttributeName: groupUuid
AttributeType: S
KeySchema:
- AttributeName: groupUuid
KeyType: HASH
IamUserRolesTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: "Retain"
Properties:
BillingMode: 'PAY_PER_REQUEST'
TableName: infra-core-api-iam-userroles
DeletionProtectionEnabled: true
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: false
AttributeDefinitions:
- AttributeName: userEmail
AttributeType: S
KeySchema:
- AttributeName: userEmail
KeyType: HASH
EventRecordsTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: "Retain"
Properties:
BillingMode: 'PAY_PER_REQUEST'
TableName: infra-core-api-events
DeletionProtectionEnabled: true
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: !If [IsProd, true, false]
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: host
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: HostIndex
KeySchema:
- AttributeName: host
KeyType: HASH
Projection:
ProjectionType: ALL
MembershipRecordsTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: "Retain"
Properties:
BillingMode: 'PAY_PER_REQUEST'
TableName: infra-core-api-membership-logs
DeletionProtectionEnabled: true
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: !If [IsProd, true, false]
AttributeDefinitions:
- AttributeName: email
AttributeType: S
KeySchema:
- AttributeName: email
KeyType: HASH
CacheRecordsTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: "Retain"
Properties:
BillingMode: 'PAY_PER_REQUEST'
TableName: infra-core-api-cache
DeletionProtectionEnabled: true
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: false
AttributeDefinitions:
- AttributeName: primaryKey
AttributeType: S
KeySchema:
- AttributeName: primaryKey
KeyType: HASH
TimeToLiveSpecification:
AttributeName: "expireAt"
Enabled: true
AppApiGateway:
Type: AWS::Serverless::Api
DependsOn:
- AppApiLambdaFunction
Properties:
Name: !Sub ${ApplicationPrefix}-gateway
Description: !Sub "${ApplicationFriendlyName} API Gateway"
AlwaysDeploy: True
DefinitionBody:
Fn::Transform:
Name: AWS::Include
Parameters:
Location: ./phony-swagger.yml
Domain:
DomainName: !Sub
- "${ApplicationPrefix}.${BaseDomainName}"
- BaseDomainName: !FindInMap
- ApiGwConfig
- !Ref RunEnvironment
- ApiDomainName
CertificateArn: !FindInMap [ApiGwConfig, !Ref RunEnvironment, ApiCertificateArn]
Route53:
HostedZoneId: !FindInMap [ApiGwConfig, !Ref RunEnvironment, HostedZoneId]
StageName: default
Cors:
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
AllowOrigin: "'*'"
MaxAge: "'300'"
APIDefault4XXResponse:
Type: AWS::ApiGateway::GatewayResponse
Properties:
RestApiId: !Ref AppApiGateway
ResponseType: DEFAULT_4XX
StatusCode: "404"
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: "{\"error\": true, \"message\": \"Resource not found. Check your URL or contact support.\"}"
APIAccessDeniedResponse:
Type: AWS::ApiGateway::GatewayResponse
Properties:
RestApiId: !Ref AppApiGateway
ResponseType: ACCESS_DENIED
StatusCode: "403"
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: "{\"error\": true, \"message\": \"Access denied. Perhaps reauthenticate and try again?\"}"
APIUnauthorizedResponse:
Type: AWS::ApiGateway::GatewayResponse
Properties:
RestApiId: !Ref AppApiGateway
ResponseType: UNAUTHORIZED
StatusCode: "401"
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: "{\"error\": true, \"message\": \"Request could not be authenticated. Perhaps reauthenticate and try again?\"}"
AppApiGatewayLatencyAlarm:
Type: 'AWS::CloudWatch::Alarm'
Condition: IsProd
Properties:
AlarmName: !Sub ${ApplicationPrefix}-gateway-latency-high
AlarmDescription: !Sub 'Alarm if ${ApplicationPrefix} API gateway latency is > 3s.'
Namespace: 'AWS/ApiGateway'
MetricName: 'Latency'
Statistic: 'Average'
Period: '60'
EvaluationPeriods: '1'
ComparisonOperator: 'GreaterThanThreshold'
Threshold: '3000'
AlarmActions:
- !Ref AlertSNSArn
Dimensions:
- Name: 'ApiName'
Value: !Sub ${ApplicationPrefix}-gateway
AppApiGateway5XXErrorAlarm:
Type: 'AWS::CloudWatch::Alarm'
Condition: IsProd
Properties:
AlarmName: !Sub ${ApplicationPrefix}-gateway-5xx
AlarmDescription: !Sub 'Alarm if ${ApplicationPrefix} API gateway 5XX errors are detected.'
Namespace: 'AWS/ApiGateway'
MetricName: '5XXError'
Statistic: 'Average'
Period: '60'
EvaluationPeriods: '1'
ComparisonOperator: 'GreaterThanThreshold'
Threshold: '2'
AlarmActions:
- !Ref AlertSNSArn
Dimensions:
- Name: 'ApiName'
Value: !Sub ${ApplicationPrefix}-gateway
APILambdaPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt AppApiLambdaFunction.Arn
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
SourceArn:
Fn::Join:
- ''
- - 'arn:aws:execute-api:'
- !Ref AWS::Region
- ":"
- !Ref AWS::AccountId
- ":"
- !Ref AppApiGateway
- "/*/*/*"