forked from ChelseaKR/nearmiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws-static-site.yml
More file actions
291 lines (277 loc) · 9.2 KB
/
Copy pathaws-static-site.yml
File metadata and controls
291 lines (277 loc) · 9.2 KB
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
AWSTemplateFormatVersion: "2010-09-09"
Description: >-
Private S3 and CloudFront origin for nearmiss.chelseakr.com, including
Route 53 DNS, an ACM certificate, and a least-privilege GitHub OIDC deploy role.
Parameters:
DomainName:
Type: String
Default: nearmiss.chelseakr.com
AllowedPattern: ^[a-z0-9.-]+$
HostedZoneId:
Type: AWS::Route53::HostedZone::Id
Default: Z09063681IX9IVZF7L6YB
SiteBucketName:
Type: String
Default: nearmiss.chelseakr.com
GitHubRepository:
Type: String
Default: ChelseaKR/nearmiss
AllowedPattern: ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$
PublishDns:
Type: String
Default: "false"
AllowedValues:
- "true"
- "false"
Conditions:
PublishDnsRecords: !Equals [!Ref PublishDns, "true"]
Resources:
SiteBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
BucketName: !Ref SiteBucketName
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
LifecycleConfiguration:
Rules:
- Id: ExpireNoncurrentVersions
Status: Enabled
NoncurrentVersionExpiration:
NoncurrentDays: 30
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerEnforced
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
VersioningConfiguration:
Status: Enabled
SiteCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref DomainName
DomainValidationOptions:
- DomainName: !Ref DomainName
HostedZoneId: !Ref HostedZoneId
ValidationMethod: DNS
OriginAccessControl:
Type: AWS::CloudFront::OriginAccessControl
Properties:
OriginAccessControlConfig:
Description: !Sub "Private ${DomainName} S3 origin"
Name: !Sub "${AWS::StackName}-oac"
OriginAccessControlOriginType: s3
SigningBehavior: always
SigningProtocol: sigv4
RouteRewriteFunction:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
FunctionCode: |
function handler(event) {
var request = event.request;
var uri = request.uri;
if (uri.endsWith('/')) {
request.uri += 'index.html';
} else if (!uri.split('/').pop().includes('.')) {
request.uri += '/index.html';
}
return request;
}
FunctionConfig:
Comment: Resolve directory-style public routes to their reviewed index documents.
Runtime: cloudfront-js-2.0
Name: !Sub "${AWS::StackName}-route-rewrite"
SiteResponseHeaders:
Type: AWS::CloudFront::ResponseHeadersPolicy
Properties:
ResponseHeadersPolicyConfig:
Comment: Security headers that preserve the intentionally embeddable hotspot widget.
CustomHeadersConfig:
Items:
- Header: Cache-Control
Override: true
Value: public, max-age=0, must-revalidate
- Header: Permissions-Policy
Override: true
Value: camera=(), geolocation=(self), microphone=(), payment=(), usb=()
Name: !Sub "${AWS::StackName}-security-headers"
SecurityHeadersConfig:
ContentTypeOptions:
Override: true
ReferrerPolicy:
Override: true
ReferrerPolicy: strict-origin-when-cross-origin
StrictTransportSecurity:
AccessControlMaxAgeSec: 63072000
IncludeSubdomains: true
Override: true
Preload: true
SiteCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Comment: >-
Cache public assets at the edge while keeping verifier query tokens distinct.
DefaultTTL: 300
MaxTTL: 86400
MinTTL: 0
Name: !Sub "${AWS::StackName}-public-cache"
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingBrotli: true
EnableAcceptEncodingGzip: true
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: whitelist
QueryStrings:
- verify
SiteDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- !Ref DomainName
Comment: !Sub "Public NearMiss evidence site at ${DomainName}"
CustomErrorResponses:
- ErrorCachingMinTTL: 0
ErrorCode: 403
ResponseCode: 404
ResponsePagePath: /404.html
- ErrorCachingMinTTL: 0
ErrorCode: 404
ResponseCode: 404
ResponsePagePath: /404.html
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachePolicyId: !Ref SiteCachePolicy
CachedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt RouteRewriteFunction.FunctionMetadata.FunctionARN
ResponseHeadersPolicyId: !Ref SiteResponseHeaders
TargetOriginId: private-site-bucket
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
Enabled: true
HttpVersion: http2and3
IPV6Enabled: true
Origins:
- DomainName: !GetAtt SiteBucket.RegionalDomainName
Id: private-site-bucket
OriginAccessControlId: !GetAtt OriginAccessControl.Id
S3OriginConfig:
OriginAccessIdentity: ""
PriceClass: PriceClass_100
ViewerCertificate:
AcmCertificateArn: !Ref SiteCertificate
MinimumProtocolVersion: TLSv1.2_2021
SslSupportMethod: sni-only
SiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref SiteBucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowCloudFrontReadOnly
Effect: Allow
Principal:
Service: cloudfront.amazonaws.com
Action: s3:GetObject
Resource: !Sub "${SiteBucket.Arn}/*"
Condition:
StringEquals:
AWS:SourceArn: !Sub >-
arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:distribution/${SiteDistribution}
SiteDnsA:
Type: AWS::Route53::RecordSet
Condition: PublishDnsRecords
Properties:
AliasTarget:
DNSName: !GetAtt SiteDistribution.DomainName
EvaluateTargetHealth: false
HostedZoneId: Z2FDTNDATAQYW2
HostedZoneId: !Ref HostedZoneId
Name: !Ref DomainName
Type: A
SiteDnsAAAA:
Type: AWS::Route53::RecordSet
Condition: PublishDnsRecords
Properties:
AliasTarget:
DNSName: !GetAtt SiteDistribution.DomainName
EvaluateTargetHealth: false
HostedZoneId: Z2FDTNDATAQYW2
HostedZoneId: !Ref HostedZoneId
Name: !Ref DomainName
Type: AAAA
GitHubDeployRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Federated: !Sub >-
arn:${AWS::Partition}:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com
Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
"token.actions.githubusercontent.com:aud": sts.amazonaws.com
"token.actions.githubusercontent.com:sub": !Sub >-
repo:${GitHubRepository}:environment:production
Description: Deploy the reviewed NearMiss static artifact from the production environment.
Policies:
- PolicyName: DeployNearMissStaticSite
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: ListSiteBucket
Effect: Allow
Action:
- s3:GetBucketLocation
- s3:ListBucket
Resource: !GetAtt SiteBucket.Arn
- Sid: PublishSiteObjects
Effect: Allow
Action:
- s3:DeleteObject
- s3:GetObject
- s3:PutObject
Resource: !Sub "${SiteBucket.Arn}/*"
- Sid: InvalidateSiteDistribution
Effect: Allow
Action:
- cloudfront:CreateInvalidation
- cloudfront:GetInvalidation
Resource: !Sub >-
arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:distribution/${SiteDistribution}
RoleName: !Sub "${AWS::StackName}-github-deploy"
Outputs:
CloudFrontDistributionId:
Value: !Ref SiteDistribution
CloudFrontDomainName:
Value: !GetAtt SiteDistribution.DomainName
DeploymentRoleArn:
Value: !GetAtt GitHubDeployRole.Arn
ProductionUrl:
Value: !Sub "https://${DomainName}"
SiteBucketName:
Value: !Ref SiteBucket