forked from symfony/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudformation.yaml
204 lines (190 loc) · 5.36 KB
/
cloudformation.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
AWSTemplateFormatVersion: '2010-09-09'
Description: ECS Symfony
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "Network Configuration"
Parameters:
- VPC
- PublicSubnet
- PrivateSubnet
-
Label:
default: "Docker images"
Parameters:
- WebAppImage
- DBImage
Parameters:
VPC:
Type: AWS::EC2::VPC::Id
Description: VPC where the tasks will be deployed.
PublicSubnet:
Type: AWS::EC2::Subnet::Id
Description: Public subnet.
PrivateSubnet:
Type: AWS::EC2::Subnet::Id
Description: Private subnet.
WebAppImage:
Type: String
Description: Docker image URL for the web application.
DBImage:
Type: String
Default: docker.io/mysql
Description: Docker image URL for the database.
Resources:
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: MySymfonyCluster
WebAppTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: WebAppTask
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
Cpu: '256'
Memory: '512'
ExecutionRoleArn: !Sub 'arn:aws:iam::${AWS::AccountId}:role/ecsTaskExecutionRole'
ContainerDefinitions:
- Name: WebApp
Image: !Ref WebAppImage
PortMappings:
- ContainerPort: 8000
Environment:
- Name: COMPOSER_ALLOW_SUPERUSER
Value: "1"
- Name: DATABASE_URL
Value: !Sub "mysql://symfony:[email protected]:3306/symfony?serverVersion=8&charset=utf8mb4"
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref WebAppTaskLogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: DBTask
WebAppService:
Type: AWS::ECS::Service
Properties:
Cluster: !Ref ECSCluster
TaskDefinition: !Ref WebAppTaskDefinition
DesiredCount: 1
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
Subnets:
- !Ref PublicSubnet
SecurityGroups:
- !Ref WebAppSecurityGroup
AssignPublicIp: ENABLED
ServiceRegistries:
- RegistryArn: !GetAtt WebAppCloudMapService.Arn
DBTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: DBTask
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
Cpu: '512'
Memory: '1GB'
ExecutionRoleArn: !Sub 'arn:aws:iam::${AWS::AccountId}:role/ecsTaskExecutionRole'
ContainerDefinitions:
- Name: DB
Image: !Ref DBImage
PortMappings:
- ContainerPort: 3306
Environment:
- Name: MYSQL_DATABASE
Value: symfony
- Name: MYSQL_ROOT_PASSWORD
Value: root
- Name: MYSQL_USER
Value: symfony
- Name: MYSQL_PASSWORD
Value: symfony
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref DBTaskLogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: DBTask
DBService:
Type: AWS::ECS::Service
Properties:
Cluster: !Ref ECSCluster
TaskDefinition: !Ref DBTaskDefinition
DesiredCount: 1
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
Subnets:
- !Ref PrivateSubnet
SecurityGroups:
- !Ref DBSecurityGroup
ServiceRegistries:
- RegistryArn: !GetAtt DBCloudMapService.Arn
DBTaskLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: "/ecs/DBTask"
WebAppTaskLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: "/ecs/WebAppTask"
WebAppSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for WebAppService
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 8000
ToPort: 8000
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: "-1"
FromPort: 0
ToPort: 65535
CidrIp: 0.0.0.0/0
DBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for DBService
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref WebAppSecurityGroup # Only allow the web app
SecurityGroupEgress:
- IpProtocol: "-1"
FromPort: 0
ToPort: 65535
CidrIp: 0.0.0.0/0
WebAppCloudMapService:
Type: AWS::ServiceDiscovery::Service
Properties:
Name: WebAppService
Description: Discovery service for the Symfony web app
NamespaceId: !Ref CloudMapNamespace
DnsConfig:
DnsRecords:
- TTL: 60
Type: A
DBCloudMapService:
Type: AWS::ServiceDiscovery::Service
Properties:
Name: DBService
Description: Discovery service for the MySQL db
NamespaceId: !Ref CloudMapNamespace
DnsConfig:
DnsRecords:
- TTL: 60
Type: A
CloudMapNamespace:
Type: AWS::ServiceDiscovery::PrivateDnsNamespace
Properties:
Name: myapp.local
Vpc: !Ref VPC