forked from bcgov/bcparks-ar-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
201 lines (191 loc) · 6.81 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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
ar-admin
SAM Template for Angular Admin Frontend
Parameters:
ProjectName:
Type: String
Default: ar-admin
Description: A&R Admin Dev.
Env:
Type: String
Default: dev
Description: Staging environment.
AWSRegion:
Type: String
Default: ca-central-1
Description: AWS Region.
BaseHref:
Type: String
Default: "/"
Description: Base href to use for cloudfront.
DistOriginPath:
Type: String
Default: latest
Description: Cloudfront distribution origin path.
ApiGatewayId:
Type: String
Default: qdp6hi0i78
Description: ID for rest API.
ApiStage:
Type: String
Default: api
Description: API stage.
EnvDomainName:
Type: String
Default: dev-ar.bcparks.ca
DomainCertificateArn:
Type: String
Default: arn:aws:acm:us-east-1:856925536711:certificate/e03ee555-facb-427e-8391-99f01e24e8cb
Resources:
CloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: CloudFront access identity
CloudFrontAPICachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Comment: "Custom CloudFront Caching Policy to allow the Authorization header through to the Origins."
DefaultTTL: 1
MaxTTL: 1
MinTTL: 1
Name: !Sub ${ApiGatewayId}-CFCachePolicy
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingBrotli: false
EnableAcceptEncodingGzip: false
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: all
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Restrictions:
GeoRestriction:
RestrictionType: blacklist
# Belarus, Central African Republic, China, Democratic Republic of the Congo, Iran, Iraq, Democratic People's Republic of Korea, Lebanon, Libya, Mali, Myanmar, Nicaragua, Russia, Somalia, South Sudan, Sudan, Syria, Ukraine, Venezuela, Yemen, Zimbabwe
Locations: [BY, CF, CN, CD, IR, IQ, KP, LB, LY, ML, MM, NI, RU, SO, SS, SD, SY, UA, VE, YE, ZW]
PriceClass: PriceClass_100
CustomErrorResponses:
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: !Sub ${BaseHref}index.html
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: !Sub ${BaseHref}index.html
Aliases:
- !Ref EnvDomainName
ViewerCertificate:
AcmCertificateArn: !Ref DomainCertificateArn
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1
Comment: !Sub "CloudFront distribution for ${AWS::StackName}"
DefaultRootObject: !Sub index.html
Enabled: true
HttpVersion: http2
# List of origins that Cloudfront will connect to
Origins:
- Id: !Ref DistS3Bucket
DomainName: !GetAtt DistS3Bucket.RegionalDomainName
S3OriginConfig:
# Restricting Bucket access through an origin access identity
OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}"
OriginPath: !Sub /${DistOriginPath}
- Id: !Sub ${ApiGatewayId}
DomainName: !Sub "${ApiGatewayId}.execute-api.${AWSRegion}.amazonaws.com"
CustomOriginConfig:
HTTPSPort: 443
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1.2
# To connect the CDN to the origins you need to specify behaviours
DefaultCacheBehavior:
# Compress resources automatically ( gzip )
Compress: true
AllowedMethods:
- GET
- HEAD
- OPTIONS
# CachingDisabled
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
TargetOriginId: !Ref DistS3Bucket
ViewerProtocolPolicy: redirect-to-https
# Simple CORS
ResponseHeadersPolicyId: 60669652-455b-4ae9-85a4-c4c02393f86c
CacheBehaviors:
- PathPattern: !Sub /${ApiStage}/*
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- POST
- PATCH
- DELETE
# CachingDisabled
CachePolicyId: !Ref CloudFrontAPICachePolicy
TargetOriginId: !Sub ${ApiGatewayId}
ViewerProtocolPolicy: redirect-to-https
# Simple CORS
ResponseHeadersPolicyId: 60669652-455b-4ae9-85a4-c4c02393f86c
- PathPattern: !Sub ${BaseHref}*
Compress: true
AllowedMethods:
- GET
- HEAD
- OPTIONS
# CachingDisabled
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
TargetOriginId: !Ref DistS3Bucket
ViewerProtocolPolicy: redirect-to-https
# Simple CORS
ResponseHeadersPolicyId: 60669652-455b-4ae9-85a4-c4c02393f86c
Logging:
Bucket: !GetAtt LogsS3Bucket.DomainName
IncludeCookies: false
Prefix: !Sub ${AWS::StackName}-${Env}-logs
DistS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-${Env}
DistS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref DistS3Bucket
PolicyDocument:
Statement:
- Effect: Allow
Principal:
AWS: !Sub arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOriginAccessIdentity}
Action: "s3:GetObject"
Resource: !Sub "arn:aws:s3:::${DistS3Bucket}/*"
LogsS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-${Env}-logs
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
LogsS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref LogsS3Bucket
PolicyDocument:
Statement:
- Effect: Allow
Principal:
AWS: !Sub arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOriginAccessIdentity}
Action: "s3:GetObject"
Resource: !Sub "arn:aws:s3:::${LogsS3Bucket}/*"
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
Website:
Value: !GetAtt CloudFrontDistribution.DomainName