-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
314 lines (311 loc) · 8.86 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
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
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar
Parameters:
ProjectId:
Type: String
Description: AWS CodeStar projectID used to associate new resources to team members
CodeDeployRole:
Type: String
Description: IAM role to allow AWS CodeDeploy to manage deployment of AWS Lambda functions
Stage:
Type: String
Description: The name for a project pipeline stage, such as Staging or Prod, for which resources are provisioned and deployed.
Default: ''
Globals:
Function:
AutoPublishAlias: live
Tracing: Active
Environment:
Variables:
PICTURE_BUCKET: picminder.aleonard.dev.pictures
DeadLetterQueue:
Type: SNS
TargetArn: arn:aws:sns:us-west-2:917159232232:DeadLetterTopic
Api:
Cors:
AllowMethods: "'OPTIONS,GET'"
AllowHeaders: "'Content-Type'"
AllowOrigin: "'*'"
Resources:
# begin frontend
S3WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: picminder.aleonard.dev
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
WebsiteConfiguration:
IndexDocument: index.html
Tags:
- Key: Project
Value: PicMinder
OriginIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "picminder.aleonard.dev origin"
DependsOn: S3WebsiteBucket
S3WebsiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref S3WebsiteBucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "s3:GetObject"
Effect: "Allow"
Resource: arn:aws:s3:::picminder.aleonard.dev/*
Principal:
AWS: !Join ["", ["arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ", !Ref OriginIdentity] ]
DependsOn: OriginIdentity
WebsiteDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- Id: S3WebsiteBucket
DomainName: picminder.aleonard.dev.s3.amazonaws.com
S3OriginConfig:
OriginAccessIdentity: !Join ["", ["origin-access-identity/cloudfront/", !Ref OriginIdentity] ]
Enabled: 'true'
DefaultRootObject: index.html
Aliases:
- picminder.aleonard.dev
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
ViewerProtocolPolicy: redirect-to-https
TargetOriginId: S3WebsiteBucket
ForwardedValues:
QueryString: false
CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6"
PriceClass: PriceClass_100
ViewerCertificate:
AcmCertificateArn: arn:aws:acm:us-east-1:917159232232:certificate/408296a8-d7c1-494a-b9ff-abe529616322
MinimumProtocolVersion: TLSv1.2_2018
SslSupportMethod: sni-only
HttpVersion: http2
IPV6Enabled: true
Tags:
- Key: Project
Value: PicMinder
DependsOn: S3WebsiteBucketPolicy
DnsZoneIPv4:
Type: AWS::Route53::RecordSet
Properties:
AliasTarget:
DNSName:
Fn::GetAtt:
- WebsiteDistribution
- DomainName
EvaluateTargetHealth: false
HostedZoneId: Z2FDTNDATAQYW2
HostedZoneName: aleonard.dev.
Name: picminder.aleonard.dev.
Type: A
DependsOn: WebsiteDistribution
DnsZoneIPv6:
Type: AWS::Route53::RecordSet
Properties:
AliasTarget:
DNSName:
Fn::GetAtt:
- WebsiteDistribution
- DomainName
EvaluateTargetHealth: false
HostedZoneId: Z2FDTNDATAQYW2
HostedZoneName: aleonard.dev.
Name: picminder.aleonard.dev.
Type: AAAA
DependsOn: WebsiteDistribution
# end frontend
# begin backend
API:
Type: AWS::Serverless::Api
Properties:
Name: PicminderApi
Domain:
DomainName: picminder-api.aleonard.dev
CertificateArn: arn:aws:acm:us-west-2:917159232232:certificate/ba2d0a69-0de2-44ff-b3bd-e3acef4c9122
Route53:
HostedZoneId: Z21QA4NBX4IRX8
StageName: prod
TracingEnabled: true
OptionsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PicMinderOptions
Handler: api/options.handler
Runtime: python3.7
MemorySize: 128
Timeout: 3
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
GetEvent:
Type: Api
Properties:
Path: /
Method: options
RestApiId: !Ref API
GetUploadUrlFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PicMinderGetUploadUrl
Handler: api/get_upload_url.handler
Runtime: python3.7
MemorySize: 128
Timeout: 3
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
GetEvent:
Type: Api
Properties:
Path: /get_upload_url/
Method: get
RestApiId: !Ref API
ProcessUploadFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PicMinderProcessUpload
Handler: api/process_upload.handler
Runtime: python3.7
MemorySize: 512
Timeout: 120
Layers:
- !Ref Libs
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
S3Event:
Type: S3
Properties:
Bucket: !Ref PictureBucket # creates circular dependency
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: prefix
Value: input_
Libs:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: PythonPilLayer
Description: PIL dependency for ProcessUploadFunction
ContentUri: api/pillow.zip
CompatibleRuntimes:
- python3.7
GetDownloadUrlFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PicMinderGetDownloadUrl
Handler: api/get_download_url.handler
Runtime: python3.7
MemorySize: 128
Timeout: 3
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
GetEvent:
Type: Api
Properties:
Path: /get_download_url/
Method: get
RestApiId: !Ref API
PictureBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: picminder.aleonard.dev.pictures
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
CorsConfiguration:
CorsRules:
-
AllowedMethods:
- HEAD
- GET
- PUT
- DELETE
AllowedOrigins:
- '*'
AllowedHeaders:
- '*'
LifecycleConfiguration:
Rules:
- Id: DeletionRule
Status: Enabled
ExpirationInDays: '1'
Tags:
- Key: Project
Value: PicMinder
PictureBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref PictureBucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "s3:GetObject"
- "s3:PutObject"
- "s3:DeleteObject"
Effect: "Allow"
Resource: arn:aws:s3:::picminder.aleonard.dev.pictures/*
Principal: "*" # TODO: change to lambda arn
DependsOn: PictureBucket
LambdaExecutionRole:
Description: Creating service role in IAM for AWS Lambda
Type: AWS::IAM::Role
Properties:
RoleName: !Sub 'CodeStar-${ProjectId}-Execution${Stage}'
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Action: sts:AssumeRole
Path: /
ManagedPolicyArns:
- !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
- arn:aws:iam::917159232232:policy/XRayPutTraceSegments
Policies:
-
PolicyName: !Sub 'CodeStar-${ProjectId}-Execution${Stage}-inline-policy'
PolicyDocument: {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::picminder.aleonard.dev.pictures/*"
},{
"Effect": "Allow",
"Action": "sns:*",
"Resource": "*"
}
]
}
# end backend