-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongtrail.yml
323 lines (302 loc) · 9.16 KB
/
longtrail.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
Parameters:
DeploymentBucket:
Type: String
APILambdaKey:
Type: String
APILambdaVersion:
Type: String
SenderEmailArn:
Type: String
WebhookURL:
Type: String
NoEcho: true
Conditions:
HasWebhook:
Fn::Not:
- Fn::Equals:
- ""
- !Ref WebhookURL
HasSenderEmail:
Fn::Not:
- Fn::Equals:
- ""
- !Ref SenderEmailArn
Resources:
LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: CognitoPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action: cognito-idp:*
Resource: !GetAtt LongtrailUserPool.Arn
- PolicyName: AllowLogging
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !GetAtt APIHandlerLogGroup.Arn
- PolicyName: AllowDynamoAccess
PolicyDocument:
Statement:
- Effect: Allow
Action: dynamodb:*
Resource:
- !GetAtt EventsTable.Arn
- !Sub ${EventsTable.Arn}/index/*
APIHandlerLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${AWS::StackName}-api
RetentionInDays: 30
APIHandlerLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-api
Timeout: 30
Environment:
Variables:
COGNITO_USER_POOL_ID: !Ref LongtrailUserPool
EVENTS_TABLE_NAME: !Ref EventsTable
Handler: longtrail-api
Runtime: go1.x
Role: !GetAtt LambdaRole.Arn
Code:
S3Bucket: !Ref DeploymentBucket
S3Key: !Ref APILambdaKey
S3ObjectVersion: !Ref APILambdaVersion
APIHandlerLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref APIHandlerLambda
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${BackendAPI}/*/*/*
EventsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: userId
AttributeType: S
- AttributeName: start
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: userId
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: userId-start-index
Projection:
ProjectionType: ALL
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: start
KeyType: RANGE
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
BackendAPI:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub ${AWS::StackName}-backend-api
EndpointConfiguration:
Types:
- EDGE
BackendAPIMainResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId: !GetAtt BackendAPI.RootResourceId
PathPart: "{proxy+}"
RestApiId: !Ref BackendAPI
BackendAPIProxy:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: COGNITO_USER_POOLS
AuthorizerId: !Ref LongtrailAPICognitoAuthorizer
HttpMethod: ANY
ResourceId: !Ref BackendAPIMainResource
RestApiId: !Ref BackendAPI
RequestParameters:
method.request.path.proxy: true
method.request.header.Authorization: true
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APIHandlerLambda.Arn}/invocations
# Define a specific OPTIONS endpoint without authorization. This is required for
# CORS preflight requests, which do not include authorization information.
BackendAPIOptions:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: OPTIONS
ResourceId: !Ref BackendAPIMainResource
RestApiId: !Ref BackendAPI
RequestParameters:
method.request.path.proxy: true
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APIHandlerLambda.Arn}/invocations
# BackendAPIDeployment will not update by default
BackendAPIDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref BackendAPI
DependsOn:
- BackendAPIProxy
- BackendAPIOptions
BackendAPIStage:
Type: AWS::ApiGateway::Stage
Properties:
RestApiId: !Ref BackendAPI
DeploymentId: !Ref BackendAPIDeployment
StageName: api
MethodSettings:
- ResourcePath: "/*"
HttpMethod: "*"
ThrottlingRateLimit: 10
ThrottlingBurstLimit: 10
LongtrailUserPool:
Type: AWS::Cognito::UserPool
Properties:
Schema:
- Name: teacherId
AttributeDataType: String
DeveloperOnlyAttribute: true
Mutable: true
UserPoolName: !Sub ${AWS::StackName}-userpool
AutoVerifiedAttributes:
- email
LambdaConfig:
Fn::If:
- HasWebhook
- PostConfirmation: !GetAtt AlertsLambda.Arn
- !Ref AWS::NoValue
EmailConfiguration:
Fn::If:
- HasSenderEmail
- EmailSendingAccount: DEVELOPER
SourceArn: !Ref SenderEmailArn
- EmailSendingAccount: COGNITO_DEFAULT
EmailVerificationMessage: |
<p>Thank you for signing up for Long Trail! Your verification code is here:</p>
<p><b>{####}</b></p>
<p>The link to Long Trail is: https://longtrail.gmtools.org</p>
<p>Enjoy!</p>
EmailVerificationSubject: Long Trail Email Verification
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: false
RequireNumbers: false
RequireSymbols: false
RequireUppercase: false
LongtrailUserPoolAdminGroup:
Type: AWS::Cognito::UserPoolGroup
Properties:
GroupName: Admin
UserPoolId: !Ref LongtrailUserPool
LongtrailAPICognitoAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
Name: !Sub ${AWS::StackName}-api-authorizer
IdentitySource: method.request.header.Authorization
ProviderARNs:
- !GetAtt LongtrailUserPool.Arn
RestApiId: !Ref BackendAPI
Type: COGNITO_USER_POOLS
LongtrailCognitoWebClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref LongtrailUserPool
AlertsLambdaRole:
Type: AWS::IAM::Role
Condition: HasWebhook
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: AllowLogging
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !GetAtt AlertsHandlerLogGroup.Arn
AlertsLambda:
Type: AWS::Lambda::Function
Condition: HasWebhook
Properties:
FunctionName: !Sub ${AWS::StackName}-alerts
Timeout: 5
Handler: index.lambda_handler
Runtime: python3.7
Role: !GetAtt AlertsLambdaRole.Arn
Environment:
Variables:
WEBHOOK_URL: !Ref WebhookURL
Code:
ZipFile: |
import urllib3
import json
import os
http = urllib3.PoolManager()
def lambda_handler(event, context):
try:
url = os.getenv('WEBHOOK_URL')
msg = {
'username': 'Longtrail Signups',
'content': f'{event["userName"]} just signed up!',
}
encoded_msg = json.dumps(msg).encode('utf-8')
resp = http.request('POST', url, body=encoded_msg, headers={'Content-Type': 'application/json'})
print(resp.status, resp.data)
except Exception as e:
print(e)
return event
AlertsHandlerLogGroup:
Type: AWS::Logs::LogGroup
Condition: HasWebhook
Properties:
LogGroupName: !Sub /aws/lambda/${AWS::StackName}-alerts
RetentionInDays: 7
AlertsLambdaPermission:
Type: AWS::Lambda::Permission
Condition: HasWebhook
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt AlertsLambda.Arn
Principal: cognito-idp.amazonaws.com
SourceArn: !GetAtt LongtrailUserPool.Arn
Outputs:
UserPoolId:
Description: Cognito user pool ID
Value: !Ref LongtrailUserPool
WebClientId:
Value: !Ref LongtrailCognitoWebClient
BackendAPIId:
Value: !Ref BackendAPI