-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverless-cloudfront.yml
161 lines (149 loc) · 5.2 KB
/
serverless-cloudfront.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
service: cf-nostr-image-services-${sls:stage}
provider:
name: aws
runtime: nodejs16.x
region: us-east-1
architecture: x86_64
timeout: 29
package:
individually: true
patterns:
- '!serverless.yml'
- '!serverless-cloudfront.yml'
- '!variables.sample.yml'
- '!variables.yml'
custom:
S3_BUCKET: ${file(./variables.yml):S3_BUCKET}
CF_CUSTOM_DOMAIN: ${file(./variables.yml):CF_CUSTOM_DOMAIN}
ROUTE53_ZONE_ID: ${file(./variables.yml):ROUTE53_ZONE_ID}
resources:
Resources:
CertificateNstr:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: ${self:custom.CF_CUSTOM_DOMAIN}
ValidationMethod: DNS
DomainValidationOptions:
- DomainName: ${self:custom.CF_CUSTOM_DOMAIN}
HostedZoneId: ${self:custom.ROUTE53_ZONE_ID}
Route53Nstr:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneId: ${self:custom.ROUTE53_ZONE_ID}
RecordSets:
- Name: ${self:custom.CF_CUSTOM_DOMAIN}
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2 # Cloudfront hardcoded zone id
DNSName: !GetAtt CloudFrontDistributionNstr.DomainName
CloudFrontOriginIdentityNstr:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: 'origin identity'
CloudFrontDistributionNstr:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
DefaultRootObject: index.html
Aliases:
- ${self:custom.CF_CUSTOM_DOMAIN}
ViewerCertificate:
AcmCertificateArn: !Ref CertificateNstr
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2021
PriceClass: PriceClass_100
Enabled: true
Origins:
- DomainName: !GetAtt S3BucketNstr.DomainName
Id: { Ref: S3BucketNstr }
S3OriginConfig:
OriginAccessIdentity: !Join
- ''
- - origin-access-identity/cloudfront/
- !Ref CloudFrontOriginIdentityNstr
DefaultCacheBehavior:
TargetOriginId: { Ref: S3BucketNstr }
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
ForwardedValues:
QueryString: false
Cookies:
Forward: none
AllowedMethods:
- GET
- HEAD
- OPTIONS
ViewerProtocolPolicy: redirect-to-https
CacheBehaviors:
- PathPattern: /images/*
TargetOriginId: { Ref: S3BucketNstr }
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
ForwardedValues:
QueryString: true
Cookies:
Forward: none
AllowedMethods:
- GET
- HEAD
- OPTIONS
ViewerProtocolPolicy: redirect-to-https
- PathPattern: /*
TargetOriginId: { Ref: S3BucketNstr }
#CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
ForwardedValues:
QueryString: true
Cookies:
Forward: none
AllowedMethods:
- GET
- HEAD
- OPTIONS
ViewerProtocolPolicy: redirect-to-https
# create S3 bucket
S3BucketNstr:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.S3_BUCKET}
# intelligent tiering
IntelligentTieringConfigurations:
- Id: 1
Status: Enabled
Tierings:
- AccessTier: ARCHIVE_ACCESS
Days: 90
- Id: tier2
Status: Enabled
Tierings:
- AccessTier: DEEP_ARCHIVE_ACCESS
Days: 180
S3BucketPolicy: # Allow uploaded files to be public and downloadable
Type: AWS::S3::BucketPolicy
Properties:
Bucket: { Ref: S3BucketNstr }
PolicyDocument:
Statement:
- Action:
- 's3:GetObject'
Effect: 'Allow'
Resource: { Fn::Join: ['', [{ Fn::GetAtt: [S3BucketNstr, Arn] }, '/*']] } # arn:aws:s3:::bucketName/*
Principal:
AWS: !Sub 'arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOriginIdentityNstr}'
Outputs:
S3BucketNstr:
Value: { Ref: S3BucketNstr }
Export:
Name: S3BucketNstr
CloudFrontDistributionNstr:
Value: { Ref: CloudFrontDistributionNstr }
Export:
Name: CloudFrontDistributionNstr
# export distribution domain name
CloudFrontDistributionDomainNameNstr:
Value: !GetAtt CloudFrontDistributionNstr.DomainName
Export:
Name: CloudFrontDistributionDomainNameNstr
CloudFrontOriginIdentityNstr:
Value: { Ref: CloudFrontOriginIdentityNstr }
Export:
Name: CloudFrontOriginIdentityNstr