-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
293 lines (269 loc) · 8.34 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
281
282
283
284
285
286
287
288
289
290
291
292
293
AWSTemplateFormatVersion: '2010-09-09'
Description: bryceandryanforeverandever.net
Mappings:
EnvironmentMap:
test:
NakedDomain: test.bryceandryanforeverandever.net
WwwDomain: www.test.bryceandryanforeverandever.net
prod:
NakedDomain: bryceandryanforeverandever.net
WwwDomain: www.bryceandryanforeverandever.net
Parameters:
Environment:
AllowedValues:
- test
- prod
Type: String
HostedZoneId:
Type: String
Resources:
NakedCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !FindInMap
- EnvironmentMap
- !Ref Environment
- NakedDomain
DomainValidationOptions:
- DomainName: !FindInMap
- EnvironmentMap
- !Ref Environment
- NakedDomain
HostedZoneId: !Ref HostedZoneId
ValidationMethod: DNS
NakedDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- !FindInMap
- EnvironmentMap
- !Ref Environment
- NakedDomain
Comment: !FindInMap
- EnvironmentMap
- !Ref Environment
- NakedDomain
DefaultCacheBehavior:
# https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html#managed-cache-policies-list
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
Compress: true
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt NakedRedirectFunction.FunctionMetadata.FunctionARN
TargetOriginId: redirectOrigin
ViewerProtocolPolicy: allow-all
Enabled: true
HttpVersion: http2
IPV6Enabled: true
# Logging: {}
Origins:
- CustomOriginConfig:
OriginProtocolPolicy: match-viewer
DomainName: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
Id: redirectOrigin
ViewerCertificate:
AcmCertificateArn: !Ref NakedCertificate
SslSupportMethod: sni-only
NakedRedirectFunction:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
FunctionCode:
Fn::Sub:
- |
function handler(event) {
var request = event.request;
var host = request.headers.host.value;
if (host === '${NakedDomain}') {
// Redirect naked domain to www subdomain.
return {
headers: { location: { value: 'https://${WwwDomain}' } },
statusCode: 301,
statusDescription: 'Moved Permanently'
};
}
return request;
}
- NakedDomain: !FindInMap
- EnvironmentMap
- !Ref Environment
- NakedDomain
WwwDomain: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
FunctionConfig:
Comment: Redirects from the naked domain to the www subdomain
Runtime: cloudfront-js-1.0
Name: !Sub ${AWS::StackName}-naked-redirect
RecordSetGroup:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: bryceandryanforeverandever.net.
RecordSets:
- AliasTarget:
DNSName: !GetAtt NakedDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
Name: !FindInMap
- EnvironmentMap
- !Ref Environment
- NakedDomain
Type: A
- AliasTarget:
DNSName: !GetAtt NakedDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
Name: !FindInMap
- EnvironmentMap
- !Ref Environment
- NakedDomain
Type: AAAA
- AliasTarget:
DNSName: !GetAtt WwwDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
Name: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
Type: A
- AliasTarget:
DNSName: !GetAtt WwwDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
Name: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
Type: AAAA
WwwBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
WwwBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
PolicyDocument:
Statement:
- Action:
- s3:GetObject
- s3:ListBucket
Effect: Allow
Principal:
CanonicalUser: !GetAtt WwwOriginAccessIdentity.S3CanonicalUserId
Resource:
- !GetAtt WwwBucket.Arn
- !Sub ${WwwBucket.Arn}/*
Version: '2012-10-17'
Bucket: !Ref WwwBucket
WwwCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: !Sub ${AWS::StackName}-caching-limited
Comment: Enables a short cache TTL and compression
DefaultTTL: 60
MaxTTL: 600
MinTTL: 0
ParametersInCacheKeyAndForwardedToOrigin:
EnableAcceptEncodingBrotli: true
EnableAcceptEncodingGzip: true
CookiesConfig:
CookieBehavior: none
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: none
WwwCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
DomainValidationOptions:
- DomainName: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
HostedZoneId: !Ref HostedZoneId
ValidationMethod: DNS
WwwDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
Comment: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
DefaultCacheBehavior:
CachePolicyId: !Ref WwwCachePolicy
Compress: true
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt WwwIndexHtmlFunction.FunctionMetadata.FunctionARN
TargetOriginId: bucketOrigin
ViewerProtocolPolicy: redirect-to-https
Enabled: true
HttpVersion: http2
IPV6Enabled: true
# Logging: {}
Origins:
- DomainName: !GetAtt WwwBucket.DomainName
Id: bucketOrigin
S3OriginConfig:
OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${WwwOriginAccessIdentity}
ViewerCertificate:
AcmCertificateArn: !Ref WwwCertificate
MinimumProtocolVersion: TLSv1.2_2021
SslSupportMethod: sni-only
WwwIndexHtmlFunction:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
FunctionCode: |
function handler(event) {
var request = event.request;
var uri = request.uri;
if (uri.endsWith('/')) {
// URI is missing a filename.
request.uri += 'index.html';
} else if (!uri.includes('.')) {
// URI is missing a file extension.
request.uri += '/index.html';
}
return request;
}
FunctionConfig:
Comment: Falls back to the index.html file
Runtime: cloudfront-js-1.0
Name: !Sub ${AWS::StackName}-www-index-html
WwwOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !FindInMap
- EnvironmentMap
- !Ref Environment
- WwwDomain
Outputs:
NakedCertificateArn:
Value: !Ref NakedCertificate
WwwCertificateArn:
Value: !Ref WwwCertificate
WwwS3CanonicalUserId:
Value: !GetAtt WwwOriginAccessIdentity.S3CanonicalUserId