|
34 | 34 | "Type": "AWS::EC2::Subnet", |
35 | 35 | "Properties": { |
36 | 36 | "VpcId": { "Ref": "VPC" }, |
37 | | - "CidrBlock": "10.0.1.0/24", |
| 37 | + "CidrBlock": "10.0.0.0/24", |
38 | 38 | "AvailabilityZone": { "Fn::Select": [ 0, { "Fn::GetAZs": "" } ] }, |
39 | 39 | "MapPublicIpOnLaunch": true |
40 | 40 | } |
|
43 | 43 | "Type": "AWS::EC2::Subnet", |
44 | 44 | "Properties": { |
45 | 45 | "VpcId": { "Ref": "VPC" }, |
46 | | - "CidrBlock": "10.0.2.0/24", |
| 46 | + "CidrBlock": "10.0.1.0/24", |
47 | 47 | "AvailabilityZone": { "Fn::Select": [ 1, { "Fn::GetAZs": "" } ] }, |
48 | 48 | "MapPublicIpOnLaunch": true |
49 | 49 | } |
|
52 | 52 | "Type": "AWS::EC2::Subnet", |
53 | 53 | "Properties": { |
54 | 54 | "VpcId": { "Ref": "VPC" }, |
55 | | - "CidrBlock": "10.0.3.0/24", |
| 55 | + "CidrBlock": "10.0.2.0/24", |
56 | 56 | "AvailabilityZone": { "Fn::Select": [ 2, { "Fn::GetAZs": "" } ] }, |
57 | 57 | "MapPublicIpOnLaunch": true |
58 | 58 | } |
|
118 | 118 | "Type": "AWS::EC2::SecurityGroup", |
119 | 119 | "Properties": { |
120 | 120 | "VpcId": { "Ref": "VPC" }, |
121 | | - "GroupDescription": "Allow inbound traffic from application load balancer", |
| 121 | + "GroupDescription": "Allow inbound traffic from application load balancer", |
122 | 122 | "SecurityGroupIngress": [ |
| 123 | + { |
| 124 | + "IpProtocol": "tcp", |
| 125 | + "FromPort": 22, |
| 126 | + "ToPort": 22, |
| 127 | + "CidrIp": "0.0.0.0/0" |
| 128 | + }, |
123 | 129 | { |
124 | 130 | "IpProtocol": "tcp", |
125 | 131 | "FromPort": 80, |
|
135 | 141 | ] |
136 | 142 | } |
137 | 143 | }, |
| 144 | + "EtcdSecurityGroup": { |
| 145 | + "Type": "AWS::EC2::SecurityGroup", |
| 146 | + "Properties": { |
| 147 | + "VpcId": { "Ref": "VPC" }, |
| 148 | + "GroupDescription": "Allow etcd traffic", |
| 149 | + "SecurityGroupIngress": [ |
| 150 | + { |
| 151 | + "IpProtocol": "tcp", |
| 152 | + "FromPort": 22, |
| 153 | + "ToPort": 22, |
| 154 | + "CidrIp": "0.0.0.0/0" |
| 155 | + }, |
| 156 | + { |
| 157 | + "IpProtocol": "tcp", |
| 158 | + "FromPort": 2379, |
| 159 | + "ToPort": 2380, |
| 160 | + "CidrIp": "0.0.0.0/0" |
| 161 | + } |
| 162 | + ], |
| 163 | + "SecurityGroupEgress": [ |
| 164 | + { |
| 165 | + "IpProtocol": "-1", |
| 166 | + "CidrIp": "0.0.0.0/0" |
| 167 | + } |
| 168 | + ] |
| 169 | + } |
| 170 | + }, |
138 | 171 | "InstanceRole": { |
139 | 172 | "Type": "AWS::IAM::Role", |
140 | 173 | "Properties": { |
|
170 | 203 | "Name": { "Ref": "InstanceProfile" } |
171 | 204 | }, |
172 | 205 | "SecurityGroupIds": [ { "Ref": "InstanceSecurityGroup" } ], |
| 206 | + "KeyName": "asyncdb", |
173 | 207 | "UserData": { |
174 | 208 | "Fn::Base64": { |
175 | 209 | "Fn::Join": [ |
176 | | - "", |
| 210 | + "\n", |
177 | 211 | [ |
178 | | - "#! /bin/bash\n", |
179 | | - "sudo yum update\n", |
180 | | - "sudo yum -y install unzip\n", |
181 | | - "curl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\n", |
182 | | - "unzip awscliv2.zip\n", |
183 | | - "./aws/install\n", |
184 | | - "REGISTRY_URL=332187735950.dkr.ecr.eu-west-2.amazonaws.com\n", |
185 | | - "VERSION=0.0.2\n", |
186 | | - "IMAGE=$REGISTRY_URL/asyncdb:$VERSION\n", |
187 | | - "aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin $REGISTRY_URL\n", |
188 | | - "docker pull $IMAGE\n", |
189 | | - "docker run -d -p 80:80 $IMAGE\n" |
| 212 | + "#! /bin/bash", |
| 213 | + "sudo yum update", |
| 214 | + "sudo yum -y install unzip", |
| 215 | + "curl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"", |
| 216 | + "unzip awscliv2.zip", |
| 217 | + "./aws/install", |
| 218 | + "REGISTRY_URL=332187735950.dkr.ecr.eu-west-2.amazonaws.com", |
| 219 | + "VERSION=0.0.2", |
| 220 | + "IMAGE=$REGISTRY_URL/asyncdb:$VERSION", |
| 221 | + "aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin $REGISTRY_URL", |
| 222 | + "docker pull $IMAGE", |
| 223 | + "docker run -d -p 80:80 $IMAGE" |
190 | 224 | ] |
191 | 225 | ] |
192 | 226 | } |
|
199 | 233 | "Properties": { |
200 | 234 | "Name": "ClusterALB", |
201 | 235 | "Scheme": "internet-facing", |
202 | | - "Subnets": [{ "Ref": "PublicSubnet1" }, { "Ref": "PublicSubnet2" }, { "Ref": "PublicSubnet3" }], |
| 236 | + "Subnets": [ |
| 237 | + { "Ref": "PublicSubnet1" }, |
| 238 | + { "Ref": "PublicSubnet2" }, |
| 239 | + { "Ref": "PublicSubnet3" } |
| 240 | + ], |
203 | 241 | "SecurityGroups": [{ "Ref": "ALBSecurityGroup" }] |
204 | 242 | } |
205 | 243 | }, |
|
230 | 268 | "AutoScalingGroup": { |
231 | 269 | "Type": "AWS::AutoScaling::AutoScalingGroup", |
232 | 270 | "Properties": { |
233 | | - "VPCZoneIdentifier": [ { "Ref": "PublicSubnet1" }, { "Ref": "PublicSubnet2" }, { "Ref": "PublicSubnet3" } ], |
| 271 | + "VPCZoneIdentifier": [ |
| 272 | + { "Ref": "PublicSubnet1" }, |
| 273 | + { "Ref": "PublicSubnet2" }, |
| 274 | + { "Ref": "PublicSubnet3" } |
| 275 | + ], |
234 | 276 | "LaunchTemplate": { |
235 | 277 | "LaunchTemplateId": { "Ref": "LaunchTemplate" }, |
236 | 278 | "Version": { "Fn::GetAtt": [ "LaunchTemplate", "LatestVersionNumber" ] } |
|
240 | 282 | "MaxSize": "4", |
241 | 283 | "TargetGroupARNs":[{ "Ref":"ALBTargetGroup" } ] |
242 | 284 | } |
| 285 | + }, |
| 286 | + "DiscoveryTokenLambdaRole": { |
| 287 | + "Type": "AWS::IAM::Role", |
| 288 | + "Properties": { |
| 289 | + "AssumeRolePolicyDocument": { |
| 290 | + "Version": "2012-10-17", |
| 291 | + "Statement": [ |
| 292 | + { |
| 293 | + "Effect": "Allow", |
| 294 | + "Principal": { |
| 295 | + "Service": "lambda.amazonaws.com" |
| 296 | + }, |
| 297 | + "Action": "sts:AssumeRole" |
| 298 | + } |
| 299 | + ] |
| 300 | + }, |
| 301 | + "ManagedPolicyArns": [ |
| 302 | + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 303 | + ] |
| 304 | + } |
| 305 | + }, |
| 306 | + "DiscoveryTokenLambda": { |
| 307 | + "Type": "AWS::Lambda::Function", |
| 308 | + "Properties": { |
| 309 | + "Handler": "index.handler", |
| 310 | + "Role": { "Fn::GetAtt": ["DiscoveryTokenLambdaRole", "Arn"] }, |
| 311 | + "Runtime": "python3.9", |
| 312 | + "Timeout": 30, |
| 313 | + "Code": { |
| 314 | + "ZipFile": { |
| 315 | + "Fn::Join": [ |
| 316 | + "\n", |
| 317 | + [ |
| 318 | + "import urllib.request", |
| 319 | + "import cfnresponse", |
| 320 | + "def handler(event, context):", |
| 321 | + " try:", |
| 322 | + " if event['RequestType'] in ('Create','Update'):", |
| 323 | + " url = \"https://discovery.etcd.io/new?size=3\"", |
| 324 | + " token = urllib.request.urlopen(url).read().decode().strip()", |
| 325 | + " cfnresponse.send(event, context, cfnresponse.SUCCESS,", |
| 326 | + " { 'DiscoveryURL': token })", |
| 327 | + " else:", |
| 328 | + " cfnresponse.send(event, context, cfnresponse.SUCCESS, {})", |
| 329 | + " except Exception as e:", |
| 330 | + " print(\"Error:\", e)", |
| 331 | + " cfnresponse.send(event, context, cfnresponse.FAILED, {})" |
| 332 | + ] |
| 333 | + ] |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | + }, |
| 338 | + "DiscoveryTokenCustomResource": { |
| 339 | + "Type": "Custom::EtcdDiscovery", |
| 340 | + "Properties": { |
| 341 | + "ServiceToken": { "Fn::GetAtt": ["DiscoveryTokenLambda", "Arn"] } |
| 342 | + } |
| 343 | + }, |
| 344 | + "EtcdLaunchTemplate": { |
| 345 | + "Type": "AWS::EC2::LaunchTemplate", |
| 346 | + "Properties": { |
| 347 | + "LaunchTemplateData": { |
| 348 | + "ImageId": { "Ref": "ECSAMI" }, |
| 349 | + "InstanceType": { "Ref": "InstanceType" }, |
| 350 | + "SecurityGroupIds": [{ "Ref": "EtcdSecurityGroup" }], |
| 351 | + "KeyName": "asyncdb", |
| 352 | + "UserData": { |
| 353 | + "Fn::Base64": { |
| 354 | + "Fn::Join": [ |
| 355 | + "\n", |
| 356 | + [ |
| 357 | + "#! /bin/bash", |
| 358 | + "PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)", |
| 359 | + "PUBLIC_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)", |
| 360 | + "INSTANCE=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)", |
| 361 | + { "Fn::Sub": ["DISCOVERY_URL='${DiscoveryTokenCustomResource.DiscoveryURL}'", {}] }, |
| 362 | + "docker run -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \\", |
| 363 | + " --name etcd quay.io/coreos/etcd:v2.3.8 \\", |
| 364 | + " -name ${INSTANCE} \\", |
| 365 | + " -advertise-client-urls http://${PUBLIC_IP}:2379,http://${PUBLIC_IP}:4001 \\", |
| 366 | + " -listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \\", |
| 367 | + " -initial-advertise-peer-urls http://${PUBLIC_IP}:2380 \\", |
| 368 | + " -listen-peer-urls http://0.0.0.0:2380 \\", |
| 369 | + " -discovery ${DISCOVERY_URL} \\" |
| 370 | + ] |
| 371 | + ] |
| 372 | + } |
| 373 | + } |
| 374 | + } |
| 375 | + } |
| 376 | + }, |
| 377 | + "EtcdAutoScalingGroup": { |
| 378 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 379 | + "Properties": { |
| 380 | + "VPCZoneIdentifier": [ |
| 381 | + { "Ref": "PublicSubnet1" }, |
| 382 | + { "Ref": "PublicSubnet2" }, |
| 383 | + { "Ref": "PublicSubnet3" } |
| 384 | + ], |
| 385 | + "LaunchTemplate": { |
| 386 | + "LaunchTemplateId": { "Ref": "EtcdLaunchTemplate" }, |
| 387 | + "Version": { "Fn::GetAtt": ["EtcdLaunchTemplate", "LatestVersionNumber"] } |
| 388 | + }, |
| 389 | + "MinSize": "3", |
| 390 | + "MaxSize": "3", |
| 391 | + "DesiredCapacity": "3", |
| 392 | + "HealthCheckType": "EC2", |
| 393 | + "Tags": [ |
| 394 | + { |
| 395 | + "Key": "Name", |
| 396 | + "Value": "etcd-node", |
| 397 | + "PropagateAtLaunch": true |
| 398 | + } |
| 399 | + ] |
| 400 | + } |
243 | 401 | } |
244 | 402 | } |
245 | 403 | } |
|
0 commit comments