-
Notifications
You must be signed in to change notification settings - Fork 2
/
phonebook-docker-swarm-cfn-template.json
392 lines (392 loc) · 13.4 KB
/
phonebook-docker-swarm-cfn-template.json
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
390
391
392
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This CloudFormation Template deploys Phonebook Application to five Docker Machines on EC2 Instances which are ready for Docker Swarm operations.\n",
"Parameters": {
"KeyPairName": {
"Type": "AWS::EC2::KeyPair::KeyName",
"Description": "Enter the name of your Key Pair for SSH connections.",
"ConstraintDescription": "Must be one of the existing EC2 KeyPair"
}
},
"Resources": {
"InstanceConnectPolicy": {
"Type": "AWS::IAM::ManagedPolicy",
"Properties": {
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["ec2-instance-connect:SendSSHPublicKey"],
"Resource": [
{
"Fn::Sub": "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*"
}
],
"Condition": {
"StringEquals": {
"ec2:osuser": "ec2-user"
}
}
},
{
"Effect": "Allow",
"Action": ["ec2:DescribeInstances"],
"Resource": "*"
}
]
}
}
},
"EC2InstanceConnect": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": ["ec2.amazonaws.com"]
},
"Action": ["sts:AssumeRole"]
}
]
},
"ManagedPolicyArns": [
{
"Ref": "InstanceConnectPolicy"
},
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess"
]
}
},
"EC2ConnectProfile": {
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Roles": [
{
"Ref": "EC2InstanceConnect"
}
]
}
},
"DockerMachinesSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Enable SSH and HTTP for Docker Machines",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": 80,
"ToPort": 80,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": 22,
"ToPort": 22,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": 2377,
"ToPort": 2377,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": 7946,
"ToPort": 7946,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "udp",
"FromPort": 7946,
"ToPort": 7946,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "udp",
"FromPort": 4789,
"ToPort": 4789,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": 8080,
"ToPort": 8080,
"CidrIp": "0.0.0.0/0"
}
]
}
},
"DockerSwarmManagerLT": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": "ami-0947d2ba12ee1ff75",
"InstanceType": "t2.micro",
"KeyName": {
"Ref": "KeyPairName"
},
"IamInstanceProfile": {
"Arn": {
"Fn::GetAtt": ["EC2ConnectProfile", "Arn"]
}
},
"SecurityGroupIds": [
{
"Fn::GetAtt": ["DockerMachinesSecurityGroup", "GroupId"]
}
],
"UserData": {
"Fn::Base64": {
"Fn::Sub": "#! /bin/bash\nyum update -y\namazon-linux-extras install docker -y\nsystemctl start docker\nsystemctl enable docker\nusermod -a -G docker ec2-user\ncurl -L \"https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)\" \\\n-o /usr/local/bin/docker-compose\nchmod +x /usr/local/bin/docker-compose\nyum install python3 -y\npip3 install ec2instanceconnectcli\neval \"$(mssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \\\n --region ${AWS::Region} ${DockerManager1} docker swarm join-token manager | grep -i 'docker')\"\n# uninstall aws cli version 1\nrm -rf /bin/aws\ncurl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\nunzip awscliv2.zip\n./aws/install\nyum install amazon-ecr-credential-helper -y\nmkdir -p /home/ec2-user/.docker\ncd /home/ec2-user/.docker\necho '{\"credsStore\": \"ecr-login\"}' > config.json\n"
}
}
}
}
},
"DockerSwarmWorkerLT": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": "ami-0947d2ba12ee1ff75",
"InstanceType": "t2.micro",
"KeyName": {
"Ref": "KeyPairName"
},
"IamInstanceProfile": {
"Arn": {
"Fn::GetAtt": ["EC2ConnectProfile", "Arn"]
}
},
"SecurityGroupIds": [
{
"Fn::GetAtt": ["DockerMachinesSecurityGroup", "GroupId"]
}
],
"UserData": {
"Fn::Base64": {
"Fn::Sub": "#! /bin/bash\nyum update -y\namazon-linux-extras install docker -y\nsystemctl start docker\nsystemctl enable docker\nusermod -a -G docker ec2-user\ncurl -L \"https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)\" \\\n-o /usr/local/bin/docker-compose\nchmod +x /usr/local/bin/docker-compose\nyum install python3 -y\npip3 install ec2instanceconnectcli\neval \"$(mssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \\\n --region ${AWS::Region} ${DockerManager1} docker swarm join-token worker | grep -i 'docker')\"\n# uninstall aws cli version 1\nrm -rf /bin/aws\ncurl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\nunzip awscliv2.zip\n./aws/install\nyum install amazon-ecr-credential-helper -y\nmkdir -p /home/ec2-user/.docker\ncd /home/ec2-user/.docker\necho '{\"credsStore\": \"ecr-login\"}' > config.json\n"
}
}
}
}
},
"DockerManager1": {
"Type": "AWS::EC2::Instance",
"DependsOn": [
"DockerMachinesSecurityGroup",
"InstanceConnectPolicy",
"EC2InstanceConnect"
],
"Properties": {
"ImageId": "ami-0947d2ba12ee1ff75",
"InstanceType": "t2.micro",
"KeyName": {
"Ref": "KeyPairName"
},
"IamInstanceProfile": {
"Ref": "EC2ConnectProfile"
},
"SecurityGroupIds": [
{
"Fn::GetAtt": ["DockerMachinesSecurityGroup", "GroupId"]
}
],
"Tags": [
{
"Key": "Name",
"Value": {
"Fn::Sub": "Docker Grand Master Manager 1st of ${AWS::StackName}"
}
}
],
"UserData": {
"Fn::Base64": {
"Fn::Sub": [
"#! /bin/bash\nyum update -y\nhostnamectl set-hostname Grand-Master\namazon-linux-extras install docker -y\nsystemctl start docker\nsystemctl enable docker\nusermod -a -G docker ec2-user\ncurl -L \"https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)\" \\\n-o /usr/local/bin/docker-compose\nchmod +x /usr/local/bin/docker-compose\ndocker swarm init\naws ecr get-login-password --region ${AWS::Region} | docker login --username AWS --password-stdin ${ECR_REGISTRY}\ndocker service create \\\n --name=viz \\\n --publish=8080:8080/tcp \\\n --constraint=node.role==manager \\\n --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \\\n dockersamples/visualizer\nyum install git -y\n# uninstall aws cli version 1\nrm -rf /bin/aws\ncurl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\nunzip awscliv2.zip\n./aws/install\nyum install amazon-ecr-credential-helper -y\nmkdir -p /home/ec2-user/.docker\ncd /home/ec2-user/.docker\necho '{\"credsStore\": \"ecr-login\"}' > config.json\naws ecr create-repository \\\n --repository-name ${APP_REPO_NAME} \\\n --image-scanning-configuration scanOnPush=false \\\n --image-tag-mutability MUTABLE \\\n --region ${AWS::Region}\ndocker build --force-rm -t \"${ECR_REGISTRY}/${APP_REPO_NAME}:latest\" ${GITHUB_REPO}\ndocker push \"${ECR_REGISTRY}/${APP_REPO_NAME}:latest\"\nmkdir -p /home/ec2-user/phonebook\ncd /home/ec2-user/phonebook\ncat << EOF | tee .env\nECR_REGISTRY=${ECR_REGISTRY}\nAPP_REPO_NAME=${APP_REPO_NAME}\nEOF\ncurl -o \"docker-compose.yml\" -L ${GIT_FILE_URL}docker-compose.yml\ncurl -o \"init.sql\" -L ${GIT_FILE_URL}init.sql\ndocker-compose config | docker stack deploy --with-registry-auth -c - phonebook\n",
{
"ECR_REGISTRY": {
"Fn::Sub": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com"
},
"APP_REPO_NAME": "infraform-repo/phonebook-app",
"GITHUB_REPO": "https://github.com/infraform/docker-swarm-elastic-container-app.git",
"GIT_FILE_URL": "https://raw.githubusercontent.com/infraform/docker-swarm-elastic-container-app/master/"
}
]
}
}
}
},
"DockerManager2": {
"Type": "AWS::EC2::Instance",
"DependsOn": ["DockerManager1"],
"Properties": {
"LaunchTemplate": {
"LaunchTemplateId": {
"Ref": "DockerSwarmManagerLT"
},
"Version": {
"Fn::GetAtt": ["DockerSwarmManagerLT", "LatestVersionNumber"]
}
},
"Tags": [
{
"Key": "Name",
"Value": {
"Fn::Sub": "Docker Manager 2nd of ${AWS::StackName}"
}
}
]
}
},
"DockerManager3": {
"Type": "AWS::EC2::Instance",
"DependsOn": ["DockerManager2"],
"Properties": {
"LaunchTemplate": {
"LaunchTemplateId": {
"Ref": "DockerSwarmManagerLT"
},
"Version": {
"Fn::GetAtt": ["DockerSwarmManagerLT", "LatestVersionNumber"]
}
},
"Tags": [
{
"Key": "Name",
"Value": {
"Fn::Sub": "Docker Manager 3rd of ${AWS::StackName}"
}
}
]
}
},
"DockerWorker1": {
"Type": "AWS::EC2::Instance",
"DependsOn": ["DockerManager1"],
"Properties": {
"LaunchTemplate": {
"LaunchTemplateId": {
"Ref": "DockerSwarmWorkerLT"
},
"Version": {
"Fn::GetAtt": ["DockerSwarmWorkerLT", "LatestVersionNumber"]
}
},
"Tags": [
{
"Key": "Name",
"Value": {
"Fn::Sub": "Docker Worker 1st of ${AWS::StackName}"
}
}
]
}
},
"DockerWorker2": {
"Type": "AWS::EC2::Instance",
"DependsOn": ["DockerWorker1"],
"Properties": {
"LaunchTemplate": {
"LaunchTemplateId": {
"Ref": "DockerSwarmWorkerLT"
},
"Version": {
"Fn::GetAtt": ["DockerSwarmWorkerLT", "LatestVersionNumber"]
}
},
"Tags": [
{
"Key": "Name",
"Value": {
"Fn::Sub": "Docker Worker 2nd of ${AWS::StackName}"
}
}
]
}
}
},
"Outputs": {
"1stDockerManagerDashboard": {
"Description": "Dashboard for Docker Swarm Cluster",
"Value": {
"Fn::Sub": [
"http://${PublicAddress}:8080",
{
"PublicAddress": {
"Fn::GetAtt": ["DockerManager1", "PublicDnsName"]
}
}
]
}
},
"1stDockerManagerDNSName": {
"Description": "Docker Manager 1st DNS Name",
"Value": {
"Fn::Sub": [
"${PublicAddress}",
{
"PublicAddress": {
"Fn::GetAtt": ["DockerManager1", "PublicDnsName"]
}
}
]
}
},
"2ndDockerManagerDNSName": {
"Description": "Docker Manager 2nd DNS Name",
"Value": {
"Fn::Sub": [
"${PublicAddress}",
{
"PublicAddress": {
"Fn::GetAtt": ["DockerManager2", "PublicDnsName"]
}
}
]
}
},
"3rdDockerManagerDNSName": {
"Description": "Docker Manager 3rd DNS Name",
"Value": {
"Fn::Sub": [
"${PublicAddress}",
{
"PublicAddress": {
"Fn::GetAtt": ["DockerManager3", "PublicDnsName"]
}
}
]
}
},
"1stDockerWorkerDNSName": {
"Description": "Docker Worker 1st DNS Name",
"Value": {
"Fn::Sub": [
"${PublicAddress}",
{
"PublicAddress": {
"Fn::GetAtt": ["DockerWorker1", "PublicDnsName"]
}
}
]
}
},
"2ndDockerWorkerDNSName": {
"Description": "Docker Worker 2nd DNS Name",
"Value": {
"Fn::Sub": [
"${PublicAddress}",
{
"PublicAddress": {
"Fn::GetAtt": ["DockerWorker2", "PublicDnsName"]
}
}
]
}
}
}
}