-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtemplate.yaml
280 lines (280 loc) · 9.26 KB
/
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
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
FrontendContent:
Type: AWS::S3::Bucket
Properties:
WebsiteConfiguration:
IndexDocument: index.html
UserPool:
Type: AWS::Cognito::UserPool
Properties:
AdminCreateUserConfig:
AllowAdminCreateUserOnly: false
AliasAttributes:
- email
- preferred_username
AutoVerifiedAttributes:
- email
UserPoolName: !Sub ${AWS::StackName}-UserPool
UserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref UserPool
HttpApi:
Type: AWS::Serverless::HttpApi
Properties:
DefinitionBody:
openapi: '3.0'
info:
title: !Sub ${AWS::StackName}-HttpApi
version: '1.0'
paths:
/ride:
post:
responses: {}
FailOnWarnings: true
Auth:
Authorizers:
Authorizer:
IdentitySource: $request.header.Authorization
JwtConfiguration:
issuer: !Sub https://cognito-idp.${AWS::Region}.amazonaws.com/${UserPool}
audience:
- !Ref UserPoolClient
DefaultAuthorizer: Authorizer
CorsConfiguration:
AllowOrigins:
- '*'
AllowHeaders:
- Authorization
- Content-Type
AllowMethods:
- '*'
Rides:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: RideId
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: RideId
KeyType: HASH
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
TableName: !Sub ${AWS::StackName}-Rides
RequestUnicorn:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-RequestUnicorn
Description: !Sub
- Stack ${StackTagName} Environment ${EnvironmentTagName} Function ${ResourceName}
- ResourceName: RequestUnicorn
CodeUri: src/RequestUnicorn
Handler: index.handler
Runtime: nodejs12.x
MemorySize: 3008
Timeout: 30
Tracing: Active
Policies:
- AWSXrayWriteOnlyAccess
- DynamoDBCrudPolicy:
TableName: !Ref Rides
- AWSSecretsManagerGetSecretValuePolicy:
SecretArn: !Sub arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:/${EnvironmentTagName}/*
Environment:
Variables:
TABLE_NAME: !Ref Rides
TABLE_ARN: !GetAtt Rides.Arn
SECRETS_NAMESPACE: !Sub /${EnvironmentTagName}/
UNICORN_STABLE_API: !Ref EnvConfigunicornStableApiAsString
Events:
HttpApiPOSTride:
Type: HttpApi
Properties:
Path: /ride
Method: POST
ApiId: !Ref HttpApi
PayloadFormatVersion: '2.0'
TimeoutInMillis: 29000
Website:
Type: AWS::CodeBuild::Project
Metadata:
StackeryType: website
DependsOn: WebsiteRole
Properties:
Name: !Sub ${AWS::StackName}-Website
Artifacts:
Type: NO_ARTIFACTS
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: USER_POOL_CLIENT_ID
Value: !Ref UserPoolClient
- Name: USER_POOL_ID
Value: !Ref UserPool
- Name: USER_POOL_ARN
Value: !GetAtt UserPool.Arn
- Name: API_URL
Value: !Sub https://${HttpApi}.execute-api.${AWS::Region}.amazonaws.com
ServiceRole: !GetAtt WebsiteRole.Arn
Source:
Type: NO_SOURCE
BuildSpec: !Sub
- |-
version: 0.2
phases:
install:
runtime-versions:
nodejs: latest
python: latest
ruby: latest
commands:
- |
_SOURCE_LOCATION=${SourceLocation}
if [ s3 != "${!_SOURCE_LOCATION%%:*}" ]; then
git clone ${SourceLocation} repo
cd repo
git checkout ${SourceVersion}
else
aws s3 cp ${SourceLocation} repo.tgz
tar --strip-components 1 -xvvzf repo.tgz
fi
- cd ${SourceDirectory}
pre_build:
commands:
- |
if [ ! -f yarn.lock -a -f package.json ]; then
npm install --production
elif [ -f yarn.lock -a -f package.json ]; then
yarn install --production
elif [ -f requirements.txt ]; then
pip install -r requirements.txt
elif [ -f Gemfile ]; then
bundle install
fi
build:
commands:
- ${BuildCommand}
post_build:
commands:
- if [ $CODEBUILD_BUILD_SUCCEEDING == 0 ]; then exit 1; fi
- |
_SOURCE_LOCATION=${SourceLocation}
if [ s3 != "${!_SOURCE_LOCATION%%:*}" ]; then
cd "${!CODEBUILD_SRC_DIR}/repo"
else
cd ${!CODEBUILD_SRC_DIR}
fi
- aws s3 sync '${PublishDirectory}' 's3://${DestinationBucketName}' --acl public-read --cache-control 'max-age=0, must-revalidate, public' --no-progress --delete
- PublishDirectory: src/site/public
BuildCommand: npm run build
SourceDirectory: src/site
DestinationBucketName: !Ref FrontendContent
Tags:
- Key: Stackery Project Type
Value: Website Builder
WebsiteEvents:
Type: AWS::Events::Rule
DependsOn: Website
Properties:
EventPattern:
source:
- aws.codebuild
detail-type:
- CodeBuild Build State Change
detail:
build-status:
- SUCCEEDED
- FAILED
- FAULT
- STOPPPED
- TIMED_OUT
project-name:
- !Ref Website
Targets:
- Arn: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:stackery-agent-commander
Id: StackeryAgentCommander
WebsiteEventsPermission:
Type: AWS::Lambda::Permission
DependsOn: WebsiteEvents
Properties:
Action: lambda:InvokeFunction
FunctionName: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:stackery-agent-commander
Principal: events.amazonaws.com
SourceArn: !GetAtt WebsiteEvents.Arn
WebsiteRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-Website
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: Logs
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${AWS::StackName}-Website:log-stream:*
- PolicyName: DownloadSourceFromStackeryAssetsBucket
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: s3:GetObject
Resource: arn:aws:s3:::stackery-assetsbucket-*/*
- PolicyName: UploadToDestinationObjectStore
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:DeleteObject
- s3:GetBucketLocation
- s3:GetObject
- s3:ListBucket
- s3:PutObject
- s3:PutObjectAcl
Resource:
- !Sub ${FrontendContent.Arn}/*
- !Sub ${FrontendContent.Arn}
WebsiteBuildTrigger:
Type: Custom::StackeryWebsiteBuildTrigger
DependsOn: WebsiteEvents
Properties:
ServiceToken: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:stackery-agent-commander
Type: website
ProjectName: !Ref Website
SourceVersion: !Ref SourceVersion
Parameters:
StackTagName:
Type: String
Description: Stack Name (injected by Stackery at deployment time)
EnvironmentTagName:
Type: String
Description: Environment Name (injected by Stackery at deployment time)
EnvConfigunicornStableApiAsString:
Type: AWS::SSM::Parameter::Value<String>
Default: /<EnvironmentName>/unicornStableApi
SourceLocation:
Type: String
Description: Location of source code for deployment (injected by Stackery at deployment time)
SourceVersion:
Type: String
Description: Source version for deployment (injected by Stackery at deployment time)
Metadata:
EnvConfigParameters:
EnvConfigunicornStableApiAsString: unicornStableApi