-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.yaml
181 lines (171 loc) · 6.21 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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
reserve-rec-admin
SAM Template for Angular Admin Frontend
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
MemorySize: 128
Parameters:
Env:
Type: String
Description: Staging environment.
AWSRegion:
Type: String
Default: ca-central-1
Description: AWS Region.
DistOriginPath:
Type: String
Default: latest
Description: Cloudfront distribution origin path.
ApiGatewayId:
Type: String
Description: ID for rest API.
ApiStage:
Type: String
Default: api
Description: API stage.
# EnvDomainName:
# Type: String
# Default: dev-data.bcparks.ca
DomainCertificateArn:
Type: String
Default: arn:aws:acm:us-east-1:970582042516:certificate/ef1d2666-8278-4d20-bdff-e27b55b3246f
ApiCachePolicyId:
Type: String
Description: API cache policy ID.
Resources:
CloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: CloudFront access identity
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 /index.html
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: !Sub /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}/${AWS::StackName}/browser
- 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 ApiCachePolicyId
TargetOriginId: !Sub ${ApiGatewayId}
ViewerProtocolPolicy: redirect-to-https
# Simple CORS
ResponseHeadersPolicyId: 60669652-455b-4ae9-85a4-c4c02393f86c
- PathPattern: /*
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