-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-app-infrastructure.yml
executable file
·316 lines (295 loc) · 9.29 KB
/
react-app-infrastructure.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
AWSTemplateFormatVersion: 2010-09-09
Conditions:
CreateDeployBucketAndPolicy: !Equals [!Ref ExistingDeployBucket, '']
CreateDnsRecordWww: !Equals [!Ref DnsRecordWwwFlag, 'true']
Parameters:
MasterStackName:
Type: String
Default: my-site
DnsRecordWwwFlag:
Type: String
Default: false
ExistingDeployBucket:
Type: String
Default: arn:aws:s3:::<REPLACE>.brendandagys.com
Repository:
Type: String
Default: <REPLACE>
Branch:
Type: String
Default: main
Description: e.g. main or master
Domain:
Type: String
Default: <REPLACE>.brendandagys.com
DomainWww:
Type: String
Default: www.<REPLACE>.brendandagys.com
HostedZoneId:
Description: Route53 Hosted Zone ID
Type: String
Default: Z1048063LC3J2IKH5GGI
CloudFrontHostedZoneId:
Type: String
Default: Z2FDTNDATAQYW2
DistributionHomePage:
Type: String
Default: /index.html
Resources:
DeployBucket:
Type: AWS::S3::Bucket
Condition: CreateDeployBucketAndPolicy
DeletionPolicy: Retain
Properties:
BucketName: !Ref Domain
AccessControl: Private
PublicAccessBlockConfiguration:
BlockPublicAcls: false
BlockPublicPolicy: true
IgnorePublicAcls: false
RestrictPublicBuckets: true
DeployBucketPolicy:
Type: AWS::S3::BucketPolicy
Condition: CreateDeployBucketAndPolicy
DeletionPolicy: Retain
Properties:
Bucket:
!If [
CreateDeployBucketAndPolicy,
!Ref DeployBucket,
!Ref ExistingDeployBucket,
]
PolicyDocument:
Id: MySiteDeployBucketPolicy
Version: 2012-10-17
Statement:
- Sid: PolicyForCloudFrontPrivateContent
Effect: Allow
Principal:
CanonicalUser: !GetAtt OriginAccessIdentity.S3CanonicalUserId
Action: 's3:GetObject'
Resource: !Join
- ''
- - 'arn:aws:s3:::'
- !If [
CreateDeployBucketAndPolicy,
!Ref DeployBucket,
!Ref ExistingDeployBucket,
]
- /*
CodePipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Location: !Sub '${MasterStackName}-pipeline-artifacts'
Type: S3
RoleArn:
!ImportValue { 'Fn::Sub': '${MasterStackName}-codepipeline-role' }
Stages:
- Name: Source
Actions:
- Name: SourceAction
ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeCommit
Version: 1
OutputArtifacts:
- Name: !Sub '${AWS::StackName}-code'
Configuration:
RepositoryName: !Ref Repository
BranchName: !Ref Branch
- Name: Approval
Actions:
- Name: ApprovalAction
ActionTypeId:
Category: Approval
Owner: AWS
Provider: Manual
Version: 1
- Name: Build
Actions:
- Name: BuildAction
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: 1
InputArtifacts:
- Name: !Sub '${AWS::StackName}-code'
OutputArtifacts:
- Name: !Sub '${AWS::StackName}-build'
Configuration:
ProjectName: !Ref CodeBuild
CodeBuild:
Type: AWS::CodeBuild::Project
Properties:
Name: !Sub ${AWS::StackName}-codebuild
ServiceRole:
!ImportValue { 'Fn::Sub': '${MasterStackName}-codebuild-role' }
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Type: LINUX_CONTAINER
Image: aws/codebuild/standard:4.0
Source:
Type: CODEPIPELINE
BuildSpec: !Sub |
version: 0.2
env:
variables:
INLINE_RUNTIME_CHUNK: "false"
phases:
install:
commands:
- cd frontend
- npm install
build:
commands:
- npm run build
# - npm test
# - echo $CODEBUILD_SRC_DIR
- ls
post_build:
on-failure: ABORT #CONTINUE
commands:
- aws s3 cp --recursive --acl public-read ./build s3://${Domain}/
- aws s3 cp --acl public-read --cache-control="max-age=0, no-cache, no-store, must-revalidate" ./build/index.html s3://${Domain}/
- aws cloudfront create-invalidation --distribution-id ${Distribution} --paths "/*"
artifacts:
files:
- 'frontend/build/**/*'
Distribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
Comment: !Sub 'Origin 1: S3 SPA for ${AWS::StackName} | Origin 2: API Fargate container for ${AWS::StackName}'
Aliases:
- !Ref Domain
Origins:
- Id:
!If [
CreateDeployBucketAndPolicy,
!Ref DeployBucket,
!Ref ExistingDeployBucket,
]
DomainName: !Sub '${Domain}.s3.amazonaws.com'
S3OriginConfig:
OriginAccessIdentity:
!Join [
'',
[
'origin-access-identity/cloudfront/',
!Ref OriginAccessIdentity,
],
]
- Id: !ImportValue { 'Fn::Sub': '${MasterStackName}-fargate-service' }
DomainName:
!ImportValue {
'Fn::Sub': '${MasterStackName}-fargate-service-load-balancer-dns-name',
}
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1.2
ViewerCertificate:
AcmCertificateArn:
!ImportValue { 'Fn::Sub': '${MasterStackName}-certificate' }
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2021
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: !Ref DistributionHomePage
ErrorCachingMinTTL: 10
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: !Ref DistributionHomePage
ErrorCachingMinTTL: 10
DefaultRootObject: !Ref DistributionHomePage
PriceClass: PriceClass_100
HttpVersion: http2
IPV6Enabled: false
DefaultCacheBehavior:
TargetOriginId:
!If [
CreateDeployBucketAndPolicy,
!Ref DeployBucket,
!Ref ExistingDeployBucket,
]
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
Compress: true
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
FunctionAssociations:
- EventType: viewer-request
FunctionARN:
!ImportValue {
'Fn::Sub': '${MasterStackName}-cloudfront-function-canonical-url',
}
- EventType: viewer-response
FunctionARN:
!ImportValue {
'Fn::Sub': '${MasterStackName}-cloudfront-function-security-headers',
}
CacheBehaviors:
- PathPattern: api/*
TargetOriginId:
!ImportValue { 'Fn::Sub': '${MasterStackName}-fargate-service' }
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3
Compress: true
AllowedMethods:
- GET
- HEAD
- OPTIONS
- POST
- PUT
- PATCH
- DELETE
CachedMethods:
- GET
- HEAD
FunctionAssociations:
- EventType: viewer-response
FunctionARN:
!ImportValue {
'Fn::Sub': '${MasterStackName}-cloudfront-function-security-headers',
}
Logging:
Bucket: !Sub '${MasterStackName}-cloudfront-distribution-logs.s3.amazonaws.com'
IncludeCookies: true
Prefix: !Sub '${AWS::StackName}-logs'
OriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Sub '${Domain} OAI'
DnsRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZoneId
Name: !Ref Domain
Type: A
AliasTarget:
HostedZoneId: !Ref CloudFrontHostedZoneId
DNSName: !GetAtt Distribution.DomainName
DnsRecordWww:
Condition: CreateDnsRecordWww
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZoneId
Name: !Ref DomainWww
Type: A
AliasTarget:
HostedZoneId: !Ref CloudFrontHostedZoneId
DNSName: !GetAtt Distribution.DomainName