Repository navigation
Expand file tree
/
Copy pathtemplate.yaml
More file actions
114 lines (105 loc) · 3.64 KB
/
Copy pathtemplate.yaml
File metadata and controls
114 lines (105 loc) · 3.64 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
AWSTemplateFormatVersion: '2010-09-09'
Description: Deploy a static website using S3 and CloudFront
Parameters:
DomainName:
Type: String
AcmCertificate:
Type: String
Resources:
# The S3 bucket that will receive the app static files
NextBucket:
Type: AWS::S3::Bucket
# Origin access identity (OAI) that will be used by the policy to allow CloudFront to access the S3 bucket
NextOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: Origin Access Identity for Next static resources in S3 bucket
# Policy allowing CloudFront to get objects from the S3 bucket
NextBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref NextBucket
PolicyDocument:
Id: next-s3-bucket-policy
Version: 2012-10-17
Statement:
- Action:
- s3:GetObject
Effect: Allow
Principal:
CanonicalUser: !GetAtt NextOriginAccessIdentity.S3CanonicalUserId
Resource: !Sub arn:aws:s3:::${NextBucket}/*
# A custom cache policy for our static files : minimum 1 minute of caching, max 1 year
NextServerCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: next-cf-cache-policy
DefaultTTL: 60 # 1 minute
MinTTL: 60 # 1 minute
MaxTTL: 31536000 # one year
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: all
EnableAcceptEncodingBrotli: true
EnableAcceptEncodingGzip: true
# The CloudFront Distribution
Distribution:
Type: AWS::CloudFront::Distribution
DependsOn:
- NextBucket
- NextOriginAccessIdentity
Properties:
DistributionConfig:
Origins:
- Id: next-s3-origin
DomainName: !GetAtt NextBucket.RegionalDomainName
S3OriginConfig:
OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${NextOriginAccessIdentity}
Enabled: true
HttpVersion: http2and3
DefaultRootObject: 'index'
CustomErrorResponses:
# when there's a 403 error in the S3 origin, it means that the file is not found
- ErrorCode: 403
ResponseCode: 404
ResponsePagePath: /404
DefaultCacheBehavior:
TargetOriginId: next-s3-origin
CachePolicyId: !Ref NextServerCachePolicy # we're using our custom cache policy
ForwardedValues:
QueryString: false
Cookies:
Forward: none
Compress: true
AllowedMethods:
- GET
- HEAD
ViewerProtocolPolicy: redirect-to-https
PriceClass: PriceClass_All
ViewerCertificate:
AcmCertificateArn: !Ref AcmCertificate
SslSupportMethod: sni-only
Aliases:
- !Ref DomainName
# Alias from our domain name to the CloudFront distribution
DNSName:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: !Join ['', [!Ref DomainName, .]]
RecordSets:
- Name: !Ref DomainName
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2 # see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget-1.html#cfn-route53-aliastarget-hostedzoneid
DNSName: !GetAtt Distribution.DomainName
Outputs:
NextBucket:
Value: !Ref NextBucket
DistributionId:
Value: !Ref Distribution