-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebsite_bucket_and_cf.yaml
206 lines (186 loc) · 6.09 KB
/
website_bucket_and_cf.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
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
CertificateArn:
Type: 'String'
CustomErrorResponsePagePath:
Type: 'String'
Default: ''
Debug:
Type: 'String'
Default: 'false'
AllowedValues: ['true', 'false']
Domain:
Type: 'String'
WebACLId:
Type: 'String'
Default: 'none'
Conditions:
EnableDebug: !Equals [!Ref Debug, 'true']
EnableWAF: !Not [!Equals [!Ref WebACLId, 'none']]
HasCustomErrorResponsePagePath: !Not [!Equals [!Ref CustomErrorResponsePagePath, '']]
Resources:
OriginAccessIdentity:
Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Sub "${Domain} OAI"
WebsiteBucket:
Type: 'AWS::S3::Bucket'
Properties:
AccessControl: 'Private'
BucketEncryption:
ServerSideEncryptionConfiguration:
-
ServerSideEncryptionByDefault:
SSEAlgorithm: 'AES256'
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
Tags:
- Key: Domain
Value: !Ref Domain
WebsiteBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref WebsiteBucket
PolicyDocument:
Statement:
-
Action: 's3:GetObject'
Effect: 'Allow'
Resource: !Sub "arn:aws:s3:::${WebsiteBucket}/*"
Principal:
CanonicalUser: !GetAtt OriginAccessIdentity.S3CanonicalUserId
EdgeLambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
EdgeLambdaVersion201912211027:
Type: "AWS::Lambda::Version"
Properties:
FunctionName:
Ref: EdgeLambdaFunction
EdgeLambdaFunction:
Type: 'AWS::Lambda::Function'
# Edge Lambda functions can't be deleted until hours after their CloudFront distribution is deleted
# https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-delete-replicas.html
DeletionPolicy: Retain
Properties:
Role: !GetAtt EdgeLambdaExecutionRole.Arn
Code:
ZipFile: |
const path = require('path');
exports.handler = async (event) => {
const { request } = event.Records[0].cf;
const { uri } = request;
const extension = path.extname(uri);
if (extension && extension.length > 0) {
return request;
}
const last_character = uri.slice(-1);
var newUri;
if (last_character === "/") {
newUri = `${uri}index.html`;
} else {
newUri = `${uri}/index.html`;
}
console.log(`Rewriting ${uri} to ${newUri}...`);
request.uri = newUri;
return request;
};
Runtime: nodejs20.x
Timeout: 5
Handler: index.handler
MemorySize: 128
CloudFrontDistribution:
Type: 'AWS::CloudFront::Distribution'
Properties:
DistributionConfig:
Aliases:
- !Ref Domain
- !Sub "www.${Domain}"
CustomErrorResponses: !If
- HasCustomErrorResponsePagePath
-
-
ErrorCode: 403
ResponseCode: 404
ResponsePagePath: !Ref CustomErrorResponsePagePath
-
ErrorCode: 404
ResponseCode: 404
ResponsePagePath: !Ref CustomErrorResponsePagePath
- !Ref 'AWS::NoValue'
DefaultCacheBehavior:
Compress: true
ForwardedValues:
Cookies:
Forward: 'none'
QueryString: true
LambdaFunctionAssociations:
-
EventType: 'viewer-request'
LambdaFunctionARN: !Join [':', [!GetAtt EdgeLambdaFunction.Arn, !GetAtt EdgeLambdaVersion201912211027.Version]]
DefaultTTL: !If [EnableDebug, 0, !Ref 'AWS::NoValue']
MaxTTL: !If [EnableDebug, 0, !Ref 'AWS::NoValue']
MinTTL: !If [EnableDebug, 0, !Ref 'AWS::NoValue']
TargetOriginId: 'WebsiteBucket'
ViewerProtocolPolicy: 'redirect-to-https'
DefaultRootObject: 'index.html'
Enabled: true
HttpVersion: 'http2'
Origins:
-
DomainName: !Join [ "", [!Ref WebsiteBucket, ".s3.amazonaws.com" ] ]
Id: 'WebsiteBucket'
S3OriginConfig:
OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${OriginAccessIdentity}"
PriceClass: 'PriceClass_All'
ViewerCertificate:
AcmCertificateArn: !Ref CertificateArn
SslSupportMethod: 'sni-only'
WebACLId: !If [EnableWAF, !Ref WebACLId, !Ref 'AWS::NoValue']
Tags:
- Key: Domain
Value: !Ref Domain
AliasDns:
Type: 'AWS::Route53::RecordSetGroup'
Properties:
HostedZoneName: !Sub "${Domain}."
RecordSets:
-
Name: !Sub "${Domain}."
Type: 'A'
AliasTarget:
# http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html#cfn-route53-aliastarget-hostedzoneid
HostedZoneId: 'Z2FDTNDATAQYW2'
DNSName: !GetAtt CloudFrontDistribution.DomainName
-
Name: !Sub "www.${Domain}."
TTL: '300'
Type: 'CNAME'
ResourceRecords:
- !GetAtt CloudFrontDistribution.DomainName
Outputs:
WebsiteBucket:
Value: !Ref WebsiteBucket
WebsiteBucketDns:
Value: !GetAtt WebsiteBucket.DomainName
CloudFrontDistributionDomain:
Value: !GetAtt CloudFrontDistribution.DomainName
CloudFrontDistributionId:
Value: !Ref CloudFrontDistribution