-
Notifications
You must be signed in to change notification settings - Fork 23
/
wp.stack.yml
389 lines (371 loc) · 11.8 KB
/
wp.stack.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
VPCStack:
Type: String
Description: VPC Stack Name
ECSStack:
Type: String
Description: ECS Stack Name
ECRStack:
Type: String
Description: ECR Stack Name
RDSStack:
Type: String
Description: RDS Stack Name
DesiredCount:
Type: Number
Default: '2'
Description: Desired task count
Path:
Type: String
Default: '/service1'
Description: Service path
Version:
Type: String
Default: 'latest'
Description: Service version
ListenerRulePriority:
Type: Number
Default: 2
TLD:
Type: String
Description: TLD name needed by Route53 to perform DNS (example.com)
Default: ''
Domain:
Type: String
Description: Domain name for your api (api.example.com)
Default: ''
SSL:
Type: String
Description: SSL Arn for your Domain
Default: ''
Conditions:
UseCustomDomain: !And
- !Not [!Equals [!Ref TLD, '']]
- !Not [!Equals [!Ref Domain, '']]
- !Not [!Equals [!Ref SSL, '']]
Resources:
#
# Wordpress media files bucket
#
MediaBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: 'index.html'
ErrorDocument: '404.html'
#
# CloudFront CDN
#
CDN:
Type: AWS::CloudFront::Distribution
DependsOn: MediaBucket
Properties:
DistributionConfig:
Aliases: !If [UseCustomDomain, [!Ref Domain], !Ref "AWS::NoValue"]
Enabled: true
PriceClass: 'PriceClass_All'
DefaultCacheBehavior:
TargetOriginId: !Ref MediaBucket
ViewerProtocolPolicy: !If [UseCustomDomain, "redirect-to-https", "allow-all"]
MinTTL: 0
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
ForwardedValues:
QueryString: false
Cookies:
Forward: none
CacheBehaviors:
-
TargetOriginId: !Ref MediaBucket
PathPattern: '/wp-content/*'
ViewerProtocolPolicy: !If [UseCustomDomain, "redirect-to-https", "allow-all"]
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
ForwardedValues:
QueryString: false
Cookies:
Forward: none
-
TargetOriginId: 'wp-alb'
PathPattern: '/wp-json/*'
MaxTTL: 60000
DefaultTTL: 10000
ViewerProtocolPolicy: !If [UseCustomDomain, "redirect-to-https", "allow-all"]
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
ForwardedValues:
QueryString: false
Cookies:
Forward: none
Origins:
-
Id: !Ref MediaBucket
DomainName: !Sub '${MediaBucket}.s3-website-${AWS::Region}.amazonaws.com'
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: 'http-only'
-
Id: 'wp-alb'
DomainName:
Fn::ImportValue:
!Sub "${ECSStack}-LoadBalancerUrl"
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: !If [UseCustomDomain, "https-only", "http-only"]
Restrictions:
GeoRestriction:
RestrictionType: 'none'
ViewerCertificate:
SslSupportMethod: !If [UseCustomDomain, "sni-only", !Ref "AWS::NoValue"]
MinimumProtocolVersion: !If [UseCustomDomain, "TLSv1", !Ref "AWS::NoValue"]
AcmCertificateArn: !If [UseCustomDomain, !Ref SSL, !Ref "AWS::NoValue"]
CloudFrontDefaultCertificate: !If [UseCustomDomain, !Ref "AWS::NoValue", true]
#
# Route53 DNS record set to map our domain to our CDN
#
DomainDNS:
Type: AWS::Route53::RecordSetGroup
Condition: UseCustomDomain
Properties:
HostedZoneName: !Sub '${TLD}.'
RecordSets:
-
Name: !Ref Domain
Type: 'A'
AliasTarget:
HostedZoneId: 'Z2FDTNDATAQYW2' # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html#cfn-route53-aliastarget-hostedzoneid
DNSName: !GetAtt CDN.DomainName
CloudWatchLogsGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Ref AWS::StackName
RetentionInDays: 365
# This IAM Role grants the service access to register/unregister with the
# Application Load Balancer (ALB). It is based on the default documented here:
# http://docs.aws.amazon.com/AmazonECS/latest/developerguide/service_IAM_role.html
ServiceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ecs-service-${AWS::StackName}
Path: /
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "ecs.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
Policies:
- PolicyName: !Sub ecs-service-${AWS::StackName}
PolicyDocument:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"ec2:AuthorizeSecurityGroupIngress",
"ec2:Describe*",
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:Describe*",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer",
"elasticloadbalancing:DeregisterTargets",
"elasticloadbalancing:DescribeTargetGroups",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:RegisterTargets"
],
"Resource": "*"
}]
}
TaskRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ecs-task-${AWS::StackName}
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: !Sub ecs-service-${AWS::StackName}
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:*
Resource:
- !Sub 'arn:aws:s3:::${MediaBucket}'
- !Sub 'arn:aws:s3:::${MediaBucket}/*'
TaskDefinition:
Type: AWS::ECS::TaskDefinition
DependsOn:
- CDN
- MediaBucket
Properties:
Family: !Ref AWS::StackName
TaskRoleArn: !GetAtt TaskRole.Arn
ContainerDefinitions:
- Name: !Ref AWS::StackName
Essential: true
Image: !Sub
- "${URL}:${Version}"
- {
URL: { "Fn::ImportValue" : {"Fn::Sub": "${ECRStack}-Service1RepositoryUrl" } },
Version: !Ref Version
}
Memory: 128
PortMappings:
- ContainerPort: 80
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref AWS::StackName
awslogs-region: !Ref AWS::Region
Environment:
- Name: AWS_REGION
Value: !Ref AWS::Region
- Name: AWS_ACCOUNT_ID
Value: !Ref AWS::AccountId
- Name: WORDPRESS_DB_HOST
Value:
Fn::ImportValue:
!Sub "${RDSStack}-RDSClusterEndpoint"
- Name: WORDPRESS_DB_USER
Value:
Fn::ImportValue:
!Sub "${RDSStack}-DatabaseUsername"
- Name: WORDPRESS_DB_PASSWORD
Value:
Fn::ImportValue:
!Sub "${RDSStack}-DatabasePassword"
- Name: WORDPRESS_CONFIG_EXTRA
Value: !Sub
- |
define( 'AS3CF_AWS_USE_EC2_IAM_ROLE', true );
define( 'AS3CF_SETTINGS', serialize( array(
'bucket' => '${MediaBucket}',
'copy-to-s3' => true,
'serve-from-s3' => true,
'domain' => 'cloudfront',
'cloudfront' => '${DomainName}',
'enable-object-prefix' => true,
'object-prefix' => 'wp-content/uploads/',
'force-https' => ${ForceHttps},
'remove-local-file' => true
) ) );
define( 'WP_HOME', '${CMSUrl}' );
define( 'WP_SITEURL', '${CMSUrl}' );
- {
MediaBucket: !Ref MediaBucket,
DomainName: !If [UseCustomDomain, !Ref Domain, !GetAtt CDN.DomainName],
ForceHttps: !If [UseCustomDomain, 'true', 'false'],
CMSUrl: !If [UseCustomDomain, { "Fn::ImportValue" : {"Fn::Sub": "${ECSStack}-CustomDomainUrl" } }, {"Fn::Sub": ["http://${Url}", {"Url": { "Fn::ImportValue" : {"Fn::Sub": "${ECSStack}-LoadBalancerUrl" } }}]}]
}
- Name: WORDPRESS_DEBUG
Value: '1'
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
VpcId:
Fn::ImportValue:
!Sub "${VPCStack}-VPCID"
Port: 80
Protocol: HTTP
Matcher:
HttpCode: 200-399
HealthCheckIntervalSeconds: 10
HealthCheckPath: '/'
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 2
TargetGroupAttributes:
- Key: 'stickiness.enabled'
Value: 'true'
- Key: 'stickiness.type'
Value: 'lb_cookie'
ListenerRuleHttp:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
ListenerArn:
Fn::ImportValue:
!Sub "${ECSStack}-LoadBalancerListenerHttp"
Priority: !Ref ListenerRulePriority
Conditions:
- Field: path-pattern
Values:
- !Ref Path
Actions:
- TargetGroupArn: !Ref TargetGroup
Type: forward
ListenerRuleHttps:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Condition: UseCustomDomain
Properties:
ListenerArn:
Fn::ImportValue:
!Sub "${ECSStack}-LoadBalancerListenerHttps"
Priority: !Ref ListenerRulePriority
Conditions:
- Field: path-pattern
Values:
- !Ref Path
Actions:
- TargetGroupArn: !Ref TargetGroup
Type: forward
Service:
Type: AWS::ECS::Service
Properties:
Cluster:
Fn::ImportValue:
!Sub "${ECSStack}-ECSCluster"
Role: !Ref ServiceRole
DesiredCount: !Ref DesiredCount
TaskDefinition: !Ref TaskDefinition
DeploymentConfiguration:
MinimumHealthyPercent: 50
LoadBalancers:
- ContainerName: !Ref AWS::StackName
ContainerPort: 80
TargetGroupArn: !Ref TargetGroup
Outputs:
Service:
Value: !Ref 'Service'
Export:
Name: !Sub '${AWS::StackName}-Service'
TaskDefinition:
Value: !Ref 'TaskDefinition'
Export:
Name: !Sub '${AWS::StackName}-TaskDefinition'
CloudWatchLogsGroup:
Value: !Ref 'CloudWatchLogsGroup'
Export:
Name: !Sub '${AWS::StackName}-CloudWatchLogsGroup'
CDNUrl:
Value: !If [UseCustomDomain, !Ref Domain, !GetAtt CDN.DomainName]
Export:
Name: !Sub '${AWS::StackName}-CDNUrl'
CDN:
Value: !Ref CDN
Export:
Name: !Sub '${AWS::StackName}-CDN'