-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwordpress-aws-ecs-fargate.yaml
350 lines (327 loc) · 11.5 KB
/
wordpress-aws-ecs-fargate.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
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
---
AWSTemplateFormatVersion: "2010-09-09"
Description: "Deploy Wordpress based on AWS ALB + AWS ECS Fargate + AWS RDS Serverless mySQL + EFS"
Parameters:
ProjectName:
Description: Please specify a unique string to identify this Wordpress installation for various purposes (billing, technical, etc)
Type: String
MinLength: 8
ConstraintDescription: "Length of the unique ID should be at least 10 characters and it may contains only characters and numbers."
WordpressDomains:
Description: "Comma-delimited list of domain names (for example, domain.com,www.domain.com). Please ensure each entry is a valid domain name."
Type: List<String>
AllowedPattern: '^(?:[a-zA-Z0-9-]{1,63}\.?)+(?:[a-zA-Z]{2,})+$'
ConstraintDescription: "Each domain name must be a valid domain format, without protocol or paths."
WordpressFromEmail:
Description: "Email address for Wordpress Email From field"
Type: String
AllowedPattern: '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
ConstraintDescription: "The email address must be in a standard email format (e.g., [email protected])."
ResponsibleForDNSRecordsEmail:
Description: "Please provide the email address of the person responsible for DNS record modifications in the Wordpress domain names."
Type: String
AllowedPattern: '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
ConstraintDescription: "The email address must be in a standard email format (e.g., [email protected])."
DBPerformanceMinCapacityACU:
Description: "This is the minimum capacity of the Aurora Serverless DB cluster, expressed in Aurora Capacity Units (ACUs). Each ACU is a combination of processing and memory capacity."
Type: Number
Default: 1
DBPerformanceMaxCapacityACU:
Description: "This is the maximum capacity of the Aurora Serverless DB cluster in ACUs. The database will automatically scale up to this level as needed. Each ACU is a combination of processing and memory capacity."
Type: Number
Default: 4
Resources:
FileSystem:
Type: AWS::EFS::FileSystem
Properties:
Encrypted: true
FileSystemTags:
- Key: Name
Value: Wordpress
PerformanceMode: generalPurpose
ThroughputMode: bursting
MountTargetSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: FileSystem Security Group
VpcId: !Sub '{{resolve:ssm:/wordpress/vpc-id:1}}'
GroupName: Wordpress-EFS-SG
SecurityGroupIngress:
- IpProtocol: "tcp"
FromPort: 2049
ToPort: 2049
CidrIp: !Sub '{{resolve:ssm:/wordpress/vpc-cidr:1}}'
MountTarget1:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystem
SubnetId: !Sub '{{resolve:ssm:/wordpress/vpc-private-subnet-id-0:1}}'
SecurityGroups:
- !Ref MountTargetSecurityGroup
MountTarget2:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystem
SubnetId: !Sub '{{resolve:ssm:/wordpress/vpc-private-subnet-id-1:1}}'
SecurityGroups:
- !Ref MountTargetSecurityGroup
AccessPoint:
Type: AWS::EFS::AccessPoint
Properties:
FileSystemId: !Ref FileSystem
PosixUser:
Uid: "1000"
Gid: "1000"
RootDirectory:
CreationInfo:
OwnerGid: "1000"
OwnerUid: "1000"
Permissions: "0777"
Path: "/bitnami"
DBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: RDS Security Group
VpcId: !Sub '{{resolve:ssm:/wordpress/vpc-id:1}}'
GroupName: Wordpress-RDS-SG
SecurityGroupIngress:
- IpProtocol: "tcp"
FromPort: 3306
ToPort: 3306
CidrIp: !Sub '{{resolve:ssm:/wordpress/vpc-cidr:1}}'
DBCredentials:
Type: 'AWS::SecretsManager::Secret'
Properties:
Name: !Join ['', ['wordpress-db-', !Ref ProjectName]]
Description: 'This is a credentials for Aurora MySQL serverless database'
GenerateSecretString:
SecretStringTemplate: !Sub '{"username": "${WordpressName}"}'
GenerateStringKey: 'password'
PasswordLength: 16
ExcludeCharacters: '"@/\'
WordpressAuroraDBCluster:
Type: AWS::RDS::DBCluster
Properties:
Engine: aurora-mysql
EngineMode: serverless
DBClusterIdentifier: !Ref ProjectName
MasterUsername: !Ref ProjectName
MasterUserPassword: !Join ['', ['{{resolve:secretsmanager:', !Ref DBCredentials, '::password}}']]
DatabaseName: !Ref ProjectName
ScalingConfiguration:
AutoPause: true
MinCapacity: !Ref DBPerformanceMinCapacityACU
MaxCapacity: !Ref DBPerformanceMaxCapacityACU
DBSubnetGroupName: !Sub '{{resolve:ssm:/wordpress/vpc-db-subnet-group:1}}'
VpcSecurityGroupIds:
- !Ref DBSecurityGroup
ALBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: ALB Security Group
VpcId: !Sub '{{resolve:ssm:/wordpress/vpc-id:1}}'
GroupName: Wordpress-ALB-SG
SecurityGroupIngress:
- IpProtocol: "tcp"
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: "tcp"
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
WordPressALB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: wof-load-balancer
SecurityGroups:
- !Ref ALBSecurityGroup
Subnets:
- !Sub '{{resolve:ssm:/wordpress/vpc-public-subnet-id-0:1}}'
- !Sub '{{resolve:ssm:/wordpress/vpc-public-subnet-id-1:1}}'
WordPressTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: WordPressTargetGroup
TargetType: ip
Port: 8080
Protocol: HTTP
HealthCheckPort: 8080
VpcId: !Sub '{{resolve:ssm:/wordpress/vpc-id:1}}'
WordPressALBHttpsListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref WordPressALB
Protocol: HTTPS
Port: 443
Certificates:
- CertificateArn: !ImportValue "wp-pototskyy-net-SslCertificateArn"
DefaultActions:
- Type: forward
TargetGroupArn: !Ref WordPressTargetGroup
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: "wordpress"
WordPressServiceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Svc-WordPress-on-Fargate"
GroupName: "Svc-WordPress-on-Fargate"
VpcId: !Sub '{{resolve:ssm:/wordpress/vpc-id:1}}'
WordPressServiceSGIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref WordPressServiceSecurityGroup
IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: "0.0.0.0/0"
WordPressService:
Type: AWS::ECS::Service
Properties:
Cluster: !Ref ECSCluster
ServiceName: wof-efs-rw-service
TaskDefinition: !Ref ECSTaskDefinition
LoadBalancers:
- TargetGroupArn: !Ref WordPressTargetGroup
ContainerName: wordpress
ContainerPort: 8080
DesiredCount: 1
LaunchType: FARGATE
DeploymentConfiguration:
MaximumPercent: 100
MinimumHealthyPercent: 0
NetworkConfiguration:
AwsvpcConfiguration:
Subnets:
- !Sub '{{resolve:ssm:/wordpress/vpc-private-subnet-id-0:1}}'
- !Sub '{{resolve:ssm:/wordpress/vpc-private-subnet-id-1:1}}'
SecurityGroups:
- !Ref WordPressServiceSecurityGroup
AssignPublicIp: DISABLED
ECSTaskDefinition:
Type: "AWS::ECS::TaskDefinition"
Properties:
Family: "wof-tutorial"
NetworkMode: "awsvpc"
ContainerDefinitions:
- Name: "wordpress"
Image: "bitnami/wordpress"
Essential: true
Environment:
- Name: "MARIADB_HOST"
Value: !GetAtt WordpressAuroraDBCluster.Endpoint.Address
- Name: "WORDPRESS_DATABASE_USER"
Value: !Ref ProjectName
- Name: "WORDPRESS_DATABASE_PASSWORD"
Value: !Join ['', ['{{resolve:secretsmanager:', !Ref DBCredentials, '::password}}']]
- Name: "WORDPRESS_DATABASE_NAME"
Value: !Ref ProjectName
- Name: "WORDPRESS_PASSWORD"
Value: "mysuperpassword"
- Name: "PHP_MEMORY_LIMIT"
Value: "512M"
- Name: "enabled"
Value: "false"
- Name: "ALLOW_EMPTY_PASSWORD"
Value: "yes"
PortMappings:
- ContainerPort: 8080
Protocol: "tcp"
MountPoints:
- SourceVolume: "wordpress"
ContainerPath: "/bitnami/wordpress"
RequiresCompatibilities:
- "FARGATE"
Cpu: "1024"
Memory: "3072"
Volumes:
- Name: "wordpress"
EFSVolumeConfiguration:
FileSystemId: !Ref FileSystem
TransitEncryption: "ENABLED"
AuthorizationConfig:
AccessPointId: !Ref AccessPoint
IAM: "DISABLED"
ECSServiceScalableTarget:
Type: "AWS::ApplicationAutoScaling::ScalableTarget"
DependsOn:
- ECSTaskDefinition
- WordPressService
Properties:
MaxCapacity: 4
MinCapacity: 1
ResourceId: !Sub "service/wordpress/wof-efs-rw-service"
RoleARN: !GetAtt ScalingRole.Arn
ScalableDimension: "ecs:service:DesiredCount"
ServiceNamespace: "ecs"
ECSServiceScalingPolicy:
Type: "AWS::ApplicationAutoScaling::ScalingPolicy"
Properties:
PolicyName: "cpu75-target-tracking-scaling-policy"
PolicyType: "TargetTrackingScaling"
ScalingTargetId: !Ref ECSServiceScalableTarget
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: "ECSServiceAverageCPUUtilization"
ScaleInCooldown: 60
ScaleOutCooldown: 60
TargetValue: 75.0
ScalingRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "application-autoscaling.amazonaws.com"
Action: "sts:AssumeRole"
Policies:
- PolicyName: "ScalingPolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "ecs:UpdateService"
- "ecs:DescribeServices"
Resource: "*" # Adjust as needed
WordpressCloudFront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
Comment: WordPress CloudFront Distribution
Aliases: !Ref WordpressDomains
PriceClass: PriceClass_100
DefaultCacheBehavior:
TargetOriginId: "WordPressALBOrigin"
ViewerProtocolPolicy: redirect-to-https
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- POST
- PATCH
- DELETE
CachedMethods:
- GET
- HEAD
ForwardedValues:
QueryString: false
Cookies:
Forward: none
CachePolicyId: "658327ea-f89d-4fab-a63d-7e88639e58f6"
Origins:
- DomainName: !GetAtt WordPressALB.DNSName
Id: "WordPressALBOrigin"
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
ViewerCertificate:
AcmCertificateArn: !ImportValue "wp-pototskyy-net-SslCertificateArn"
SslSupportMethod: sni-only