|
9 | 9 | "ECSAMI": { |
10 | 10 | "Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>", |
11 | 11 | "Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id" |
| 12 | + }, |
| 13 | + "EtcdAmiId": { |
| 14 | + "Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>", |
| 15 | + "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" |
12 | 16 | } |
13 | 17 | }, |
14 | 18 | "Resources": { |
|
34 | 38 | "Type": "AWS::EC2::Subnet", |
35 | 39 | "Properties": { |
36 | 40 | "VpcId": { "Ref": "VPC" }, |
37 | | - "CidrBlock": "10.0.1.0/24", |
| 41 | + "CidrBlock": "10.0.0.0/24", |
38 | 42 | "AvailabilityZone": { "Fn::Select": [ 0, { "Fn::GetAZs": "" } ] }, |
39 | 43 | "MapPublicIpOnLaunch": true |
40 | 44 | } |
|
43 | 47 | "Type": "AWS::EC2::Subnet", |
44 | 48 | "Properties": { |
45 | 49 | "VpcId": { "Ref": "VPC" }, |
46 | | - "CidrBlock": "10.0.2.0/24", |
| 50 | + "CidrBlock": "10.0.1.0/24", |
47 | 51 | "AvailabilityZone": { "Fn::Select": [ 1, { "Fn::GetAZs": "" } ] }, |
48 | 52 | "MapPublicIpOnLaunch": true |
49 | 53 | } |
|
52 | 56 | "Type": "AWS::EC2::Subnet", |
53 | 57 | "Properties": { |
54 | 58 | "VpcId": { "Ref": "VPC" }, |
55 | | - "CidrBlock": "10.0.3.0/24", |
| 59 | + "CidrBlock": "10.0.2.0/24", |
56 | 60 | "AvailabilityZone": { "Fn::Select": [ 2, { "Fn::GetAZs": "" } ] }, |
57 | 61 | "MapPublicIpOnLaunch": true |
58 | 62 | } |
|
118 | 122 | "Type": "AWS::EC2::SecurityGroup", |
119 | 123 | "Properties": { |
120 | 124 | "VpcId": { "Ref": "VPC" }, |
121 | | - "GroupDescription": "Allow inbound traffic from application load balancer", |
| 125 | + "GroupDescription": "Allow inbound traffic from application load balancer", |
122 | 126 | "SecurityGroupIngress": [ |
123 | 127 | { |
124 | 128 | "IpProtocol": "tcp", |
|
135 | 139 | ] |
136 | 140 | } |
137 | 141 | }, |
| 142 | + "EtcdLoadBalancerSecurityGroup": { |
| 143 | + "Type": "AWS::EC2::SecurityGroup", |
| 144 | + "Properties": { |
| 145 | + "VpcId": { "Ref": "VPC" }, |
| 146 | + "GroupDescription": "Allow etcd traffic", |
| 147 | + "SecurityGroupIngress": [ |
| 148 | + { |
| 149 | + "IpProtocol": "tcp", |
| 150 | + "FromPort": 2379, |
| 151 | + "ToPort": 2380, |
| 152 | + "CidrIp": "0.0.0.0/0" |
| 153 | + } |
| 154 | + ] |
| 155 | + } |
| 156 | + }, |
| 157 | + "EtcdSecurityGroup": { |
| 158 | + "Type": "AWS::EC2::SecurityGroup", |
| 159 | + "Properties": { |
| 160 | + "VpcId": { "Ref": "VPC" }, |
| 161 | + "GroupDescription": "Allow etcd traffic", |
| 162 | + "SecurityGroupIngress": [ |
| 163 | + { |
| 164 | + "IpProtocol": "tcp", |
| 165 | + "FromPort": 2379, |
| 166 | + "ToPort": 2380, |
| 167 | + "CidrIp": "0.0.0.0/0" |
| 168 | + } |
| 169 | + ] |
| 170 | + } |
| 171 | + }, |
138 | 172 | "InstanceRole": { |
139 | 173 | "Type": "AWS::IAM::Role", |
140 | 174 | "Properties": { |
|
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": "EtcdAmiId" }, |
| 349 | + "InstanceType": { "Ref": "InstanceType" }, |
| 350 | + "SecurityGroupIds": [{ "Ref": "EtcdSecurityGroup" }], |
| 351 | + "UserData": { |
| 352 | + "Fn::Base64": { |
| 353 | + "Fn::Join": [ |
| 354 | + "\n", |
| 355 | + [ |
| 356 | + "#!/bin/bash", |
| 357 | + "yum install -y wget", |
| 358 | + "ETCD_VER=v3.5.10", |
| 359 | + "DOWNLOAD_URL=https://github.com/etcd-io/etcd/releases/download", |
| 360 | + "wget ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz", |
| 361 | + "tar xvf etcd-${ETCD_VER}-linux-amd64.tar.gz", |
| 362 | + "mv etcd-${ETCD_VER}-linux-amd64/etcd* /usr/local/bin/", |
| 363 | + "PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)", |
| 364 | + "DISCOVERY_URL='${DiscoveryTokenCustomResource.DiscoveryURL}'", |
| 365 | + "nohup etcd \\", |
| 366 | + " --name ${PRIVATE_IP} \\", |
| 367 | + " --initial-advertise-peer-urls http://${PRIVATE_IP}:2380 \\", |
| 368 | + " --listen-peer-urls http://${PRIVATE_IP}:2380 \\", |
| 369 | + " --listen-client-urls http://${PRIVATE_IP}:2379,http://127.0.0.1:2379 \\", |
| 370 | + " --advertise-client-urls http://${PRIVATE_IP}:2379 \\", |
| 371 | + " --discovery ${DISCOVERY_URL} &" |
| 372 | + ] |
| 373 | + ] |
| 374 | + } |
| 375 | + } |
| 376 | + } |
| 377 | + } |
| 378 | + }, |
| 379 | + "EtcdAutoScalingGroup": { |
| 380 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 381 | + "Properties": { |
| 382 | + "VPCZoneIdentifier": [ |
| 383 | + { "Ref": "PublicSubnet1" }, |
| 384 | + { "Ref": "PublicSubnet2" }, |
| 385 | + { "Ref": "PublicSubnet3" } |
| 386 | + ], |
| 387 | + "LaunchTemplate": { |
| 388 | + "LaunchTemplateId": { "Ref": "EtcdLaunchTemplate" }, |
| 389 | + "Version": { "Fn::GetAtt": ["EtcdLaunchTemplate", "LatestVersionNumber"] } |
| 390 | + }, |
| 391 | + "MinSize": "3", |
| 392 | + "MaxSize": "3", |
| 393 | + "DesiredCapacity": "3", |
| 394 | + "HealthCheckType": "EC2", |
| 395 | + "Tags": [ |
| 396 | + { |
| 397 | + "Key": "Name", |
| 398 | + "Value": "etcd-node", |
| 399 | + "PropagateAtLaunch": true |
| 400 | + } |
| 401 | + ] |
| 402 | + } |
| 403 | + }, |
| 404 | + "EtcdTargetGroup": { |
| 405 | + "Type": "AWS::ElasticLoadBalancingV2::TargetGroup", |
| 406 | + "Properties": { |
| 407 | + "VpcId": { "Ref": "VPC" }, |
| 408 | + "Port": 2379, |
| 409 | + "Protocol": "TCP", |
| 410 | + "TargetType": "instance", |
| 411 | + "HealthCheckProtocol": "TCP" |
| 412 | + } |
| 413 | + }, |
| 414 | + "EtcdNLB": { |
| 415 | + "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer", |
| 416 | + "Properties": { |
| 417 | + "Subnets": [ |
| 418 | + { "Ref": "PublicSubnet1" }, |
| 419 | + { "Ref": "PublicSubnet2" }, |
| 420 | + { "Ref": "PublicSubnet3" } |
| 421 | + ], |
| 422 | + "SecurityGroups": [{ "Ref": "EtcdLoadBalancerSecurityGroup" }], |
| 423 | + "Scheme": "internet-facing", |
| 424 | + "Type": "network" |
| 425 | + } |
| 426 | + }, |
| 427 | + "EtcdListener": { |
| 428 | + "Type": "AWS::ElasticLoadBalancingV2::Listener", |
| 429 | + "Properties": { |
| 430 | + "LoadBalancerArn": { "Ref": "EtcdNLB" }, |
| 431 | + "Port": 2379, |
| 432 | + "Protocol": "TCP", |
| 433 | + "DefaultActions": [ |
| 434 | + { |
| 435 | + "Type": "forward", |
| 436 | + "TargetGroupArn": { "Ref": "EtcdTargetGroup" } |
| 437 | + } |
| 438 | + ] |
| 439 | + } |
| 440 | + }, |
| 441 | + "EtcdASGAttachment": { |
| 442 | + "Type": "AWS::AutoScaling::LifecycleHook", |
| 443 | + "Properties": { |
| 444 | + "AutoScalingGroupName": { "Ref": "EtcdAutoScalingGroup" }, |
| 445 | + "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING", |
| 446 | + "DefaultResult": "CONTINUE", |
| 447 | + "HeartbeatTimeout": 300 |
| 448 | + } |
243 | 449 | } |
244 | 450 | } |
245 | 451 | } |
|
0 commit comments