Repository navigation
Expand file tree
/
Copy pathcloudformation.yaml
More file actions
664 lines (586 loc) · 22.8 KB
/
Copy pathcloudformation.yaml
File metadata and controls
664 lines (586 loc) · 22.8 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
AWSTemplateFormatVersion: "2010-09-09"
Description: >-
asyncdb over three AZs: an auto scaling group behind an ALB, pulling from ECR, and a second group
of three etcd nodes that find each other with DescribeInstances, for them to partition the
keyspace through
Parameters:
InstanceType:
Type: String
Default: t3.micro
BaseAmi:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ecs/optimized-ami/amazon-linux-2023/recommended/image_id
Description: >-
The image both tiers launch from, resolved at deploy time from the public parameter AWS keeps
current. It is the ECS-optimised Amazon Linux 2023 image, taken for the Docker daemon already
installed and running on it and not for ECS, and it carries neither tier's container: a
database instance pulls asyncdb:$VERSION and an etcd instance pulls etcd:$ETCD_VERSION, both
from this account's own registry.
Version:
Type: AWS::SSM::Parameter::Value<String>
Default: /asyncdb/version
Description: >-
The image tag to run, resolved at deploy time from the SSM parameter the build writes after it
pushes to ECR. The repository's version file is what that build reads, so the tag deployed here
is the one CI actually published rather than whatever happens to be checked out.
EtcdVersion:
Type: AWS::SSM::Parameter::Value<String>
Default: /asyncdb/etcd
Description: >-
The etcd tag to run, the same arrangement as Version: the repository's etcd-version file is
what the build mirrors out of quay.io into this account's registry, and this is the parameter
it writes afterwards. The tier pulls out of this account's registry and never out of quay.io,
so the mirror is how the tag reaches an instance at all.
Nodes:
Type: String
Default: "6"
AllowedValues: ["3", "4", "6", "9"]
Description: >-
How many database instances the group runs. The group balances them over the subnets it
spans, so this count is the whole of the arrangement inside a zone: six over three zones is
three copies of the keyspace each split in half, and nine is the same three copies split in
three. It moves three at a time, one per zone, so that no zone is left holding a larger share
than the others. Four is the exception, for chaos/nodes-removed: a shrink from six that leaves
one zone both of its nodes.
Zones:
Type: String
Default: "3"
AllowedValues: ["2", "3"]
Description: >-
How many availability zones the database tier spans, which is how many copies of the keyspace
there are: a zone holds exactly one. An instance reads its own zone out of IMDS, so this is
the subnets the group is given and nothing else. The etcd tier always spans three, so taking a
copy away here does not touch quorum.
Mappings:
# MaxSize is one above the desired capacity, so that a replacement can launch before the instance
# it replaces goes. CloudFormation cannot add one to a parameter, so the pairs are written out.
Capacity:
"3":
Max: "4"
"4":
Max: "5"
"6":
Max: "7"
"9":
Max: "10"
Conditions:
ThreeZones: !Equals [!Ref Zones, "3"]
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
InternetGateway:
Type: AWS::EC2::InternetGateway
Ipv6CidrBlock:
Type: AWS::EC2::VPCCidrBlock
Properties:
VpcId: !Ref VPC
AmazonProvidedIpv6CidrBlock: true
EgressOnlyInternetGateway:
Type: AWS::EC2::EgressOnlyInternetGateway
Properties:
VpcId: !Ref VPC
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.10.0/24
AvailabilityZone: !Select [0, !GetAZs ""]
MapPublicIpOnLaunch: false
PublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.11.0/24
AvailabilityZone: !Select [1, !GetAZs ""]
MapPublicIpOnLaunch: false
PublicSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.12.0/24
AvailabilityZone: !Select [2, !GetAZs ""]
MapPublicIpOnLaunch: false
PrivateSubnet1:
Type: AWS::EC2::Subnet
DependsOn: Ipv6CidrBlock
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.0/24
Ipv6CidrBlock: !Select [0, !Cidr [!Select [0, !GetAtt VPC.Ipv6CidrBlocks], 3, 64]]
AssignIpv6AddressOnCreation: true
AvailabilityZone: !Select [0, !GetAZs ""]
MapPublicIpOnLaunch: false
PrivateSubnet2:
Type: AWS::EC2::Subnet
DependsOn: Ipv6CidrBlock
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.1.0/24
Ipv6CidrBlock: !Select [1, !Cidr [!Select [0, !GetAtt VPC.Ipv6CidrBlocks], 3, 64]]
AssignIpv6AddressOnCreation: true
AvailabilityZone: !Select [1, !GetAZs ""]
MapPublicIpOnLaunch: false
PrivateSubnet3:
Type: AWS::EC2::Subnet
DependsOn: Ipv6CidrBlock
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.2.0/24
Ipv6CidrBlock: !Select [2, !Cidr [!Select [0, !GetAtt VPC.Ipv6CidrBlocks], 3, 64]]
AssignIpv6AddressOnCreation: true
AvailabilityZone: !Select [2, !GetAZs ""]
MapPublicIpOnLaunch: false
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
PublicRoute:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
PrivateRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationIpv6CidrBlock: "::/0"
EgressOnlyInternetGatewayId: !Ref EgressOnlyInternetGateway
PublicSubnetRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet1
RouteTableId: !Ref PublicRouteTable
PublicSubnetRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet2
RouteTableId: !Ref PublicRouteTable
PublicSubnetRouteTableAssociation3:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet3
RouteTableId: !Ref PublicRouteTable
PrivateSubnetRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet1
RouteTableId: !Ref PrivateRouteTable
PrivateSubnetRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet2
RouteTableId: !Ref PrivateRouteTable
PrivateSubnetRouteTableAssociation3:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet3
RouteTableId: !Ref PrivateRouteTable
ALBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Allow inbound HTTP from anywhere
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: "-1"
CidrIp: 0.0.0.0/0
- IpProtocol: "-1"
CidrIpv6: "::/0"
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Allow inbound traffic from application load balancer
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !Ref ALBSecurityGroup
SecurityGroupEgress:
- IpProtocol: "-1"
CidrIp: 0.0.0.0/0
- IpProtocol: "-1"
CidrIpv6: "::/0"
EtcdSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Allow etcd traffic
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 2379
ToPort: 2379
SourceSecurityGroupId: !Ref InstanceSecurityGroup
SecurityGroupEgress:
- IpProtocol: "-1"
CidrIp: 0.0.0.0/0
- IpProtocol: "-1"
CidrIpv6: "::/0"
InstanceApiIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref InstanceSecurityGroup
Description: The API port, which is how the other nodes of the cluster reach this one
IpProtocol: tcp
FromPort: 8080
ToPort: 8080
SourceSecurityGroupId: !Ref InstanceSecurityGroup
EtcdPeerIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EtcdSecurityGroup
Description: The peer port, which etcd nodes raft over
IpProtocol: tcp
FromPort: 2380
ToPort: 2380
SourceSecurityGroupId: !Ref EtcdSecurityGroup
EtcdClientIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EtcdSecurityGroup
Description: >-
The client port, from etcd itself, which is how a launching node asks the cluster to admit it
IpProtocol: tcp
FromPort: 2379
ToPort: 2379
SourceSecurityGroupId: !Ref EtcdSecurityGroup
InstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: [ec2.amazonaws.com]
Action: [sts:AssumeRole]
Policies:
- PolicyName: discovery
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: [ec2:DescribeInstances]
Resource: "*"
- PolicyName: logs
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: [logs:CreateLogStream, logs:PutLogEvents]
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:asyncdb:*"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref InstanceRole
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: !Ref BaseAmi
InstanceType: !Ref InstanceType
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: 30
VolumeType: gp3
DeleteOnTermination: true
IamInstanceProfile:
Name: !Ref InstanceProfile
SecurityGroupIds:
- !Ref InstanceSecurityGroup
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: asyncdb
UserData:
Fn::Base64: !Sub |-
#! /bin/bash
systemctl enable --now docker
mkdir -p /var/lib/asyncdb
REGION=eu-west-2
# The subnet has no IPv4 route out, so every call to AWS goes over IPv6 to a dual stack endpoint.
export AWS_USE_DUALSTACK_ENDPOINT=true
[ -f /etc/amazon/ssm/amazon-ssm-agent.json ] || cp /etc/amazon/ssm/amazon-ssm-agent.json.template /etc/amazon/ssm/amazon-ssm-agent.json
python3 -c "import json; f = '/etc/amazon/ssm/amazon-ssm-agent.json'; c = json.load(open(f)); c.setdefault('Agent', {}).update({'Region': '$REGION', 'UseDualStackEndpoint': True}); json.dump(c, open(f, 'w'), indent = 2)"
systemctl restart amazon-ssm-agent
REGISTRY_URL=332187735950.dkr-ecr.eu-west-2.on.aws
VERSION=${Version}
IMAGE=$REGISTRY_URL/asyncdb:$VERSION
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $REGISTRY_URL
docker pull $IMAGE
TOKEN=$(curl -s -X PUT http://169.254.169.254/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
PRIVATE_IP=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4)
ZONE=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone)
INSTANCE_ID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)
VPC=${VPC}
for attempt in $(seq 12); do
ASYNCDB_ETCD=$(aws ec2 describe-instances --region $REGION \
--filters Name=tag:Name,Values=etcd Name=vpc-id,Values=$VPC Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[].PrivateIpAddress' --output text \
| tr '\t' '\n' | grep . | sort | sed -e 's|^|http://|' -e 's|$|:2379|' | paste -sd,)
[ -n "$ASYNCDB_ETCD" ] && break
sleep 5
done
# A stopped container drains for two of the target group's failed checks and one more, and
# Docker is told to wait longer than that before it kills it.
docker run -d --restart always -p 80:80 -p 8080:8080 \
--stop-timeout 30 \
--log-driver awslogs \
--log-opt awslogs-region=$REGION \
--log-opt awslogs-endpoint=https://logs.$REGION.api.aws \
--log-opt awslogs-group=asyncdb \
--log-opt awslogs-stream=${AWS::StackName}/asyncdb-$INSTANCE_ID \
--log-opt mode=non-blocking \
-v /var/lib/asyncdb:/var/lib/asyncdb \
-e ASYNCDB_ETCD=$ASYNCDB_ETCD \
-e ASYNCDB_NODE=http://$PRIVATE_IP:8080 \
-e ASYNCDB_ZONE=$ZONE \
-e ASYNCDB_DRAIN=15 \
$IMAGE
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Sub "${AWS::StackName}-alb"
Scheme: internet-facing
Subnets:
- !Ref PublicSubnet1
- !Ref PublicSubnet2
- !Ref PublicSubnet3
SecurityGroups:
- !Ref ALBSecurityGroup
# A target that goes from the network sends no reset, so a request already on a connection to
# it waits this long for the 504. It stays above a node's own unacknowledged_timeout_seconds,
# which is what a request passing over a copy that has gone spends.
LoadBalancerAttributes:
- Key: idle_timeout.timeout_seconds
Value: "15"
ALBTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
VpcId: !Ref VPC
Port: 80
Protocol: HTTP
TargetType: instance
HealthCheckPath: /asyncdb/health
# A node that cannot order a write answers this 503, and how long it then goes on being sent
# requests is these numbers and nothing else — the smallest the load balancer takes. The
# defaults are 30 seconds and five checks to come back, which is two and a half minutes of a
# node that is whole again being left out of the load balancer.
HealthCheckIntervalSeconds: 5
HealthCheckTimeoutSeconds: 4
UnhealthyThresholdCount: 2
HealthyThresholdCount: 2
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: "30"
ALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref ApplicationLoadBalancer
Port: 80
Protocol: HTTP
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ALBTargetGroup
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: [PrivateRoute]
Properties:
VPCZoneIdentifier: !If
- ThreeZones
- - !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
- - !Ref PrivateSubnet1
- !Ref PrivateSubnet2
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumber
DesiredCapacity: !Ref Nodes
MinSize: "1"
MaxSize: !FindInMap [Capacity, !Ref Nodes, Max]
HealthCheckType: EC2
HealthCheckGracePeriod: 200
TargetGroupARNs:
- !Ref ALBTargetGroup
EtcdRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: [ec2.amazonaws.com]
Action: [sts:AssumeRole]
Policies:
- PolicyName: discovery
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: [ec2:DescribeInstances]
Resource: "*"
- PolicyName: logs
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: [logs:CreateLogStream, logs:PutLogEvents]
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:asyncdb:*"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
EtcdInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref EtcdRole
EtcdLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: !Ref BaseAmi
InstanceType: !Ref InstanceType
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: 30
VolumeType: gp3
DeleteOnTermination: true
IamInstanceProfile:
Name: !Ref EtcdInstanceProfile
SecurityGroupIds:
- !Ref EtcdSecurityGroup
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: etcd
UserData:
Fn::Base64: !Sub |-
#! /bin/bash
systemctl enable --now docker
mkdir -p /var/lib/etcd
REGION=eu-west-2
# The subnet has no IPv4 route out, so every call to AWS goes over IPv6 to a dual stack endpoint.
export AWS_USE_DUALSTACK_ENDPOINT=true
[ -f /etc/amazon/ssm/amazon-ssm-agent.json ] || cp /etc/amazon/ssm/amazon-ssm-agent.json.template /etc/amazon/ssm/amazon-ssm-agent.json
python3 -c "import json; f = '/etc/amazon/ssm/amazon-ssm-agent.json'; c = json.load(open(f)); c.setdefault('Agent', {}).update({'Region': '$REGION', 'UseDualStackEndpoint': True}); json.dump(c, open(f, 'w'), indent = 2)"
systemctl restart amazon-ssm-agent
REGISTRY_URL=332187735950.dkr-ecr.eu-west-2.on.aws
IMAGE=$REGISTRY_URL/etcd:${EtcdVersion}
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $REGISTRY_URL
docker pull $IMAGE
VPC=${VPC}
SIZE=3
TOKEN=$(curl -s -X PUT http://169.254.169.254/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
SELF=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4)
NAME=etcd-$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)
discover() {
aws ec2 describe-instances --region $REGION \
--filters Name=tag:Name,Values=etcd Name=vpc-id,Values=$VPC Name=instance-state-name,Values=pending,running \
--query 'Reservations[].Instances[].[InstanceId,PrivateIpAddress]' --output text \
| awk 'NF == 2 { print "etcd-" $1 "=http://" $2 ":2380" }' | sort | paste -sd,
}
etcdctl() {
docker run --rm --network host $IMAGE /usr/local/bin/etcdctl --endpoints=$PEERS "$@"
}
for attempt in $(seq 60); do
CLUSTER=$(discover)
[ $(echo $CLUSTER | tr ',' ' ' | wc -w) -ge $SIZE ] && break
sleep 5
done
ENTRIES=$(echo $CLUSTER | tr ',' '\n')
PEERS=$(echo "$ENTRIES" | grep -v "^$NAME=" | sed -e 's|.*=||' -e 's|2380$|2379|' | paste -sd,)
SELFENTRY=$NAME=http://$SELF:2380
STATE=new
INITIAL=$CLUSTER
MEMBERS=
for attempt in $(seq 12); do
[ -z "$PEERS" ] && break
MEMBERS=$(etcdctl member list) && break
MEMBERS=
sleep 5
done
if [ -n "$MEMBERS" ]; then
MEMBERS=$(echo "$MEMBERS" | tr -d ' ')
echo "$MEMBERS" | while IFS=, read -r ID STATUS MEMBER PEER REST; do
echo "$ENTRIES" | grep -qx "$MEMBER=$PEER" || etcdctl member remove $ID
done
if ! echo "$MEMBERS" | awk -F, '{ print $3 "=" $4 }' | grep -qx "$SELFENTRY"; then
for attempt in $(seq 12); do
ADDED=$(etcdctl member add $NAME --peer-urls=http://$SELF:2380) && break
ADDED=
sleep 5
done
INITIAL=$(echo "$ADDED" | grep '^ETCD_INITIAL_CLUSTER=' | cut -d'"' -f2)
[ -z "$INITIAL" ] && exit 1
STATE=existing
fi
fi
docker run -d --restart always --name etcd -p 2379:2379 -p 2380:2380 \
--log-driver awslogs \
--log-opt awslogs-region=$REGION \
--log-opt awslogs-endpoint=https://logs.$REGION.api.aws \
--log-opt awslogs-group=asyncdb \
--log-opt awslogs-stream=${AWS::StackName}/$NAME \
--log-opt mode=non-blocking \
-v /var/lib/etcd:/etcd-data \
$IMAGE /usr/local/bin/etcd \
--name $NAME \
--data-dir /etcd-data \
--advertise-client-urls http://$SELF:2379 \
--listen-client-urls http://0.0.0.0:2379 \
--initial-advertise-peer-urls http://$SELF:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-cluster $INITIAL \
--initial-cluster-state $STATE \
--initial-cluster-token asyncdb
EtcdAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: [PrivateRoute, EtcdClientIngress, EtcdPeerIngress]
Properties:
VPCZoneIdentifier:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
LaunchTemplate:
LaunchTemplateId: !Ref EtcdLaunchTemplate
Version: !GetAtt EtcdLaunchTemplate.LatestVersionNumber
DesiredCapacity: 3
MinSize: "1"
MaxSize: "3"
HealthCheckType: EC2
HealthCheckGracePeriod: 300
Outputs:
Url:
Description: >-
The public address of the cluster. Every node answers for every key, so this one URL is the
whole API and the UI.
Value: !Sub "http://${ApplicationLoadBalancer.DNSName}"