-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtemplate.yaml
293 lines (260 loc) · 7.74 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
AWSTemplateFormatVersion: 2010-09-09
Description: WordPress on AWS Lambda!
Transform: AWS::Serverless-2016-10-31
Parameters:
VpcSecurityGroupIds:
Type: 'List<AWS::EC2::SecurityGroup::Id>'
Default: 'sg-53af6735'
VpcSubnetIds:
Type: 'List<AWS::EC2::Subnet::Id>'
Default: 'subnet-0dcd7a44,subnet-a8757ef0,subnet-b19c01d6'
StageName:
Type: String
Default: 'Prod'
CloudFrontPriceClass:
Type: String
Default: 'PriceClass_200'
Resources:
phpserver:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-phpserver
Description: WordPress Webserver
CodeUri: src/php
Runtime: provided
Handler: handler.php
MemorySize: 1344
Timeout: 30
Tracing: Active
Layers:
# - arn:aws:lambda:us-west-2:887080169480:layer:php73:2
- arn:aws:lambda:us-west-2:777160072469:layer:php73:11
Environment:
Variables:
S3_UPLOADS_BUCKET: !Ref assetsS3
Policies:
- VPCAccessPolicy: {}
- S3FullAccessPolicy:
BucketName: !Ref assetsS3
Events:
apiRoot:
Type: Api
Properties:
Path: '/'
Method: ANY
RestApiId: !Ref restapi
api:
Type: Api
Properties:
Path: '/{proxy+}'
Method: ANY
RestApiId: !Ref restapi
# Comment this out to run in public mode.
VpcConfig:
SecurityGroupIds: !Ref VpcSecurityGroupIds
SubnetIds: !Ref VpcSubnetIds
restapi:
Type: AWS::Serverless::Api
DeletionPolicy: "Retain"
Properties:
StageName: !Ref StageName
EndpointConfiguration: REGIONAL
# Accept binary uploads.
BinaryMediaTypes:
- "*~1*"
cloudfront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- Id: PhpServer
DomainName: !Join
- ''
- - !Ref restapi
- '.execute-api.'
- !Ref AWS::Region
- '.amazonaws.com'
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
OriginPath: !Join ['', ['/', !Ref StageName]]
- Id: Assets
DomainName: !Join
- ""
- - !Ref assetsS3
- ".s3-"
- !Ref AWS::Region
- ".amazonaws.com"
S3OriginConfig:
OriginAccessIdentity: !Join [ '', [ 'origin-access-identity/cloudfront/', !Ref cfOriginAccessIdentity ]]
Enabled: 'true'
IPV6Enabled: 'true'
HttpVersion: 'http2'
PriceClass: !Ref CloudFrontPriceClass
CustomErrorResponses:
- ErrorCode: 404
ErrorCachingMinTTL: 0
DefaultCacheBehavior:
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
## The origin id defined above
TargetOriginId: PhpServer
Compress: 'true'
## Defining if and how the QueryString and Cookies are forwarded to the origin which in this case is S3
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: whitelist
WhitelistedNames:
- 'comment_author_*'
- 'wordpress_*'
- 'wp-settings-*'
MinTTL: 0
DefaultTTL: 0
MaxTTL: 31536000
ViewerProtocolPolicy: redirect-to-https
CacheBehaviors:
- PathPattern: "*.jpg"
AllowedMethods:
- GET
- HEAD
- OPTIONS
DefaultTTL: 300
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: 'none'
TargetOriginId: Assets
ViewerProtocolPolicy: redirect-to-https
- PathPattern: "*.png"
AllowedMethods:
- GET
- HEAD
- OPTIONS
DefaultTTL: 300
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: 'none'
TargetOriginId: Assets
ViewerProtocolPolicy: redirect-to-https
- PathPattern: "*.jpeg"
AllowedMethods:
- GET
- HEAD
- OPTIONS
DefaultTTL: 300
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: 'none'
TargetOriginId: Assets
ViewerProtocolPolicy: redirect-to-https
- PathPattern: "*.gif"
AllowedMethods:
- GET
- HEAD
- OPTIONS
DefaultTTL: 300
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: 'none'
TargetOriginId: Assets
ViewerProtocolPolicy: redirect-to-https
- PathPattern: "*.css"
AllowedMethods:
- GET
- HEAD
- OPTIONS
DefaultTTL: 300
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: 'none'
TargetOriginId: Assets
ViewerProtocolPolicy: redirect-to-https
- PathPattern: "*.js"
AllowedMethods:
- GET
- HEAD
- OPTIONS
DefaultTTL: 300
ForwardedValues:
QueryString: 'true'
Cookies:
Forward: 'none'
TargetOriginId: Assets
ViewerProtocolPolicy: redirect-to-https
- PathPattern: "/wp-admin/*"
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
## The origin id defined above
TargetOriginId: PhpServer
Compress: 'true'
## Defining if and how the QueryString and Cookies are forwarded to the origin which in this case is S3
ForwardedValues:
QueryString: 'true'
Headers:
- "User-Agent"
Cookies:
Forward: whitelist
WhitelistedNames:
- 'comment_author_*'
- 'wordpress_*'
- 'wp-settings-*'
MinTTL: 0
DefaultTTL: 0
MaxTTL: 31536000
ViewerProtocolPolicy: redirect-to-https
assetsS3:
Type: AWS::S3::Bucket
assetsS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref assetsS3
PolicyDocument:
Statement:
- Sid: CloudFrontOrigin
Effect: Allow
Principal:
CanonicalUser: !GetAtt cfOriginAccessIdentity.S3CanonicalUserId
Action:
- s3:GetObject
Resource: !Join
- ''
- - 'arn:aws:s3:::'
- !Ref assetsS3
- '/*'
cfOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: The Origin Access Identity to allow CloudFront to serve static files from the assets bucket (WordPress on Lambda)
Outputs:
RestApiDomainName:
Value: !Join
- ''
- - !Ref restapi
- '.execute-api.'
- !Ref AWS::Region
- '.amazonaws.com'
CloudFrontDistributionId:
Value: !Ref cloudfront
CloudFrontDistributionDomainName:
Value: !GetAtt cloudfront.DomainName
AssetsBucketName:
Value: !Ref assetsS3