Skip to content

Commit 990fab3

Browse files
authored
feat(ecs): add instance warmup period prop (#28194)
Closes #28190. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 073958f commit 990fab3

File tree

11 files changed

+395
-324
lines changed

11 files changed

+395
-324
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.default-capacity-provider.js.snapshot/CapacityProvidersDefaultTestDeployAssert30F9785A.assets.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.default-capacity-provider.js.snapshot/cdk.out

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.default-capacity-provider.js.snapshot/integ-default-capacity-provider.assets.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.default-capacity-provider.js.snapshot/integ-default-capacity-provider.template.json

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
"VpcPublicSubnet1Subnet5C2D37C4": {
1919
"Type": "AWS::EC2::Subnet",
2020
"Properties": {
21-
"VpcId": {
22-
"Ref": "Vpc8378EB38"
23-
},
2421
"AvailabilityZone": {
2522
"Fn::Select": [
2623
0,
@@ -44,21 +41,24 @@
4441
"Key": "Name",
4542
"Value": "integ-default-capacity-provider/Vpc/PublicSubnet1"
4643
}
47-
]
44+
],
45+
"VpcId": {
46+
"Ref": "Vpc8378EB38"
47+
}
4848
}
4949
},
5050
"VpcPublicSubnet1RouteTable6C95E38E": {
5151
"Type": "AWS::EC2::RouteTable",
5252
"Properties": {
53-
"VpcId": {
54-
"Ref": "Vpc8378EB38"
55-
},
5653
"Tags": [
5754
{
5855
"Key": "Name",
5956
"Value": "integ-default-capacity-provider/Vpc/PublicSubnet1"
6057
}
61-
]
58+
],
59+
"VpcId": {
60+
"Ref": "Vpc8378EB38"
61+
}
6262
}
6363
},
6464
"VpcPublicSubnet1RouteTableAssociation97140677": {
@@ -75,12 +75,12 @@
7575
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
7676
"Type": "AWS::EC2::Route",
7777
"Properties": {
78-
"RouteTableId": {
79-
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
80-
},
8178
"DestinationCidrBlock": "0.0.0.0/0",
8279
"GatewayId": {
8380
"Ref": "VpcIGWD7BA715C"
81+
},
82+
"RouteTableId": {
83+
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
8484
}
8585
},
8686
"DependsOn": [
@@ -102,15 +102,15 @@
102102
"VpcPublicSubnet1NATGateway4D7517AA": {
103103
"Type": "AWS::EC2::NatGateway",
104104
"Properties": {
105-
"SubnetId": {
106-
"Ref": "VpcPublicSubnet1Subnet5C2D37C4"
107-
},
108105
"AllocationId": {
109106
"Fn::GetAtt": [
110107
"VpcPublicSubnet1EIPD7E02669",
111108
"AllocationId"
112109
]
113110
},
111+
"SubnetId": {
112+
"Ref": "VpcPublicSubnet1Subnet5C2D37C4"
113+
},
114114
"Tags": [
115115
{
116116
"Key": "Name",
@@ -126,9 +126,6 @@
126126
"VpcPublicSubnet2Subnet691E08A3": {
127127
"Type": "AWS::EC2::Subnet",
128128
"Properties": {
129-
"VpcId": {
130-
"Ref": "Vpc8378EB38"
131-
},
132129
"AvailabilityZone": {
133130
"Fn::Select": [
134131
1,
@@ -152,21 +149,24 @@
152149
"Key": "Name",
153150
"Value": "integ-default-capacity-provider/Vpc/PublicSubnet2"
154151
}
155-
]
152+
],
153+
"VpcId": {
154+
"Ref": "Vpc8378EB38"
155+
}
156156
}
157157
},
158158
"VpcPublicSubnet2RouteTable94F7E489": {
159159
"Type": "AWS::EC2::RouteTable",
160160
"Properties": {
161-
"VpcId": {
162-
"Ref": "Vpc8378EB38"
163-
},
164161
"Tags": [
165162
{
166163
"Key": "Name",
167164
"Value": "integ-default-capacity-provider/Vpc/PublicSubnet2"
168165
}
169-
]
166+
],
167+
"VpcId": {
168+
"Ref": "Vpc8378EB38"
169+
}
170170
}
171171
},
172172
"VpcPublicSubnet2RouteTableAssociationDD5762D8": {
@@ -183,12 +183,12 @@
183183
"VpcPublicSubnet2DefaultRoute97F91067": {
184184
"Type": "AWS::EC2::Route",
185185
"Properties": {
186-
"RouteTableId": {
187-
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
188-
},
189186
"DestinationCidrBlock": "0.0.0.0/0",
190187
"GatewayId": {
191188
"Ref": "VpcIGWD7BA715C"
189+
},
190+
"RouteTableId": {
191+
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
192192
}
193193
},
194194
"DependsOn": [
@@ -210,15 +210,15 @@
210210
"VpcPublicSubnet2NATGateway9182C01D": {
211211
"Type": "AWS::EC2::NatGateway",
212212
"Properties": {
213-
"SubnetId": {
214-
"Ref": "VpcPublicSubnet2Subnet691E08A3"
215-
},
216213
"AllocationId": {
217214
"Fn::GetAtt": [
218215
"VpcPublicSubnet2EIP3C605A87",
219216
"AllocationId"
220217
]
221218
},
219+
"SubnetId": {
220+
"Ref": "VpcPublicSubnet2Subnet691E08A3"
221+
},
222222
"Tags": [
223223
{
224224
"Key": "Name",
@@ -234,9 +234,6 @@
234234
"VpcPrivateSubnet1Subnet536B997A": {
235235
"Type": "AWS::EC2::Subnet",
236236
"Properties": {
237-
"VpcId": {
238-
"Ref": "Vpc8378EB38"
239-
},
240237
"AvailabilityZone": {
241238
"Fn::Select": [
242239
0,
@@ -260,21 +257,24 @@
260257
"Key": "Name",
261258
"Value": "integ-default-capacity-provider/Vpc/PrivateSubnet1"
262259
}
263-
]
260+
],
261+
"VpcId": {
262+
"Ref": "Vpc8378EB38"
263+
}
264264
}
265265
},
266266
"VpcPrivateSubnet1RouteTableB2C5B500": {
267267
"Type": "AWS::EC2::RouteTable",
268268
"Properties": {
269-
"VpcId": {
270-
"Ref": "Vpc8378EB38"
271-
},
272269
"Tags": [
273270
{
274271
"Key": "Name",
275272
"Value": "integ-default-capacity-provider/Vpc/PrivateSubnet1"
276273
}
277-
]
274+
],
275+
"VpcId": {
276+
"Ref": "Vpc8378EB38"
277+
}
278278
}
279279
},
280280
"VpcPrivateSubnet1RouteTableAssociation70C59FA6": {
@@ -291,21 +291,18 @@
291291
"VpcPrivateSubnet1DefaultRouteBE02A9ED": {
292292
"Type": "AWS::EC2::Route",
293293
"Properties": {
294-
"RouteTableId": {
295-
"Ref": "VpcPrivateSubnet1RouteTableB2C5B500"
296-
},
297294
"DestinationCidrBlock": "0.0.0.0/0",
298295
"NatGatewayId": {
299296
"Ref": "VpcPublicSubnet1NATGateway4D7517AA"
297+
},
298+
"RouteTableId": {
299+
"Ref": "VpcPrivateSubnet1RouteTableB2C5B500"
300300
}
301301
}
302302
},
303303
"VpcPrivateSubnet2Subnet3788AAA1": {
304304
"Type": "AWS::EC2::Subnet",
305305
"Properties": {
306-
"VpcId": {
307-
"Ref": "Vpc8378EB38"
308-
},
309306
"AvailabilityZone": {
310307
"Fn::Select": [
311308
1,
@@ -329,21 +326,24 @@
329326
"Key": "Name",
330327
"Value": "integ-default-capacity-provider/Vpc/PrivateSubnet2"
331328
}
332-
]
329+
],
330+
"VpcId": {
331+
"Ref": "Vpc8378EB38"
332+
}
333333
}
334334
},
335335
"VpcPrivateSubnet2RouteTableA678073B": {
336336
"Type": "AWS::EC2::RouteTable",
337337
"Properties": {
338-
"VpcId": {
339-
"Ref": "Vpc8378EB38"
340-
},
341338
"Tags": [
342339
{
343340
"Key": "Name",
344341
"Value": "integ-default-capacity-provider/Vpc/PrivateSubnet2"
345342
}
346-
]
343+
],
344+
"VpcId": {
345+
"Ref": "Vpc8378EB38"
346+
}
347347
}
348348
},
349349
"VpcPrivateSubnet2RouteTableAssociationA89CAD56": {
@@ -360,12 +360,12 @@
360360
"VpcPrivateSubnet2DefaultRoute060D2087": {
361361
"Type": "AWS::EC2::Route",
362362
"Properties": {
363-
"RouteTableId": {
364-
"Ref": "VpcPrivateSubnet2RouteTableA678073B"
365-
},
366363
"DestinationCidrBlock": "0.0.0.0/0",
367364
"NatGatewayId": {
368365
"Ref": "VpcPublicSubnet2NATGateway9182C01D"
366+
},
367+
"RouteTableId": {
368+
"Ref": "VpcPrivateSubnet2RouteTableA678073B"
369369
}
370370
}
371371
},
@@ -383,11 +383,11 @@
383383
"VpcVPCGWBF912B6E": {
384384
"Type": "AWS::EC2::VPCGatewayAttachment",
385385
"Properties": {
386-
"VpcId": {
387-
"Ref": "Vpc8378EB38"
388-
},
389386
"InternetGatewayId": {
390387
"Ref": "VpcIGWD7BA715C"
388+
},
389+
"VpcId": {
390+
"Ref": "Vpc8378EB38"
391391
}
392392
}
393393
},
@@ -627,8 +627,6 @@
627627
"ASG46ED3070": {
628628
"Type": "AWS::AutoScaling::AutoScalingGroup",
629629
"Properties": {
630-
"MaxSize": "1",
631-
"MinSize": "1",
632630
"LaunchTemplate": {
633631
"LaunchTemplateId": {
634632
"Ref": "ASGLaunchTemplate0CA92847"
@@ -640,6 +638,8 @@
640638
]
641639
}
642640
},
641+
"MaxSize": "1",
642+
"MinSize": "1",
643643
"Tags": [
644644
{
645645
"Key": "Name",
@@ -792,12 +792,6 @@
792792
"Code": {
793793
"ZipFile": "import boto3, json, os, time\n\necs = boto3.client('ecs')\nautoscaling = boto3.client('autoscaling')\n\n\ndef lambda_handler(event, context):\n print(json.dumps(dict(event, ResponseURL='...')))\n cluster = os.environ['CLUSTER']\n snsTopicArn = event['Records'][0]['Sns']['TopicArn']\n lifecycle_event = json.loads(event['Records'][0]['Sns']['Message'])\n instance_id = lifecycle_event.get('EC2InstanceId')\n if not instance_id:\n print('Got event without EC2InstanceId: %s', json.dumps(dict(event, ResponseURL='...')))\n return\n\n instance_arn = container_instance_arn(cluster, instance_id)\n print('Instance %s has container instance ARN %s' % (lifecycle_event['EC2InstanceId'], instance_arn))\n\n if not instance_arn:\n return\n\n task_arns = container_instance_task_arns(cluster, instance_arn)\n\n if task_arns:\n print('Instance ARN %s has task ARNs %s' % (instance_arn, ', '.join(task_arns)))\n\n while has_tasks(cluster, instance_arn, task_arns):\n time.sleep(10)\n\n try:\n print('Terminating instance %s' % instance_id)\n autoscaling.complete_lifecycle_action(\n LifecycleActionResult='CONTINUE',\n **pick(lifecycle_event, 'LifecycleHookName', 'LifecycleActionToken', 'AutoScalingGroupName'))\n except Exception as e:\n # Lifecycle action may have already completed.\n print(str(e))\n\n\ndef container_instance_arn(cluster, instance_id):\n \"\"\"Turn an instance ID into a container instance ARN.\"\"\"\n arns = ecs.list_container_instances(cluster=cluster, filter='ec2InstanceId==' + instance_id)['containerInstanceArns']\n if not arns:\n return None\n return arns[0]\n\ndef container_instance_task_arns(cluster, instance_arn):\n \"\"\"Fetch tasks for a container instance ARN.\"\"\"\n arns = ecs.list_tasks(cluster=cluster, containerInstance=instance_arn)['taskArns']\n return arns\n\ndef has_tasks(cluster, instance_arn, task_arns):\n \"\"\"Return True if the instance is running tasks for the given cluster.\"\"\"\n instances = ecs.describe_container_instances(cluster=cluster, containerInstances=[instance_arn])['containerInstances']\n if not instances:\n return False\n instance = instances[0]\n\n if instance['status'] == 'ACTIVE':\n # Start draining, then try again later\n set_container_instance_to_draining(cluster, instance_arn)\n return True\n\n task_count = None\n\n if task_arns:\n # Fetch details for tasks running on the container instance\n tasks = ecs.describe_tasks(cluster=cluster, tasks=task_arns)['tasks']\n if tasks:\n # Consider any non-stopped tasks as running\n task_count = sum(task['lastStatus'] != 'STOPPED' for task in tasks) + instance['pendingTasksCount']\n\n if not task_count:\n # Fallback to instance task counts if detailed task information is unavailable\n task_count = instance['runningTasksCount'] + instance['pendingTasksCount']\n\n print('Instance %s has %s tasks' % (instance_arn, task_count))\n\n return task_count > 0\n\ndef set_container_instance_to_draining(cluster, instance_arn):\n ecs.update_container_instances_state(\n cluster=cluster,\n containerInstances=[instance_arn], status='DRAINING')\n\n\ndef pick(dct, *keys):\n \"\"\"Pick a subset of a dict.\"\"\"\n return {k: v for k, v in dct.items() if k in keys}\n"
794794
},
795-
"Role": {
796-
"Fn::GetAtt": [
797-
"ASGDrainECSHookFunctionServiceRoleC12963BB",
798-
"Arn"
799-
]
800-
},
801795
"Environment": {
802796
"Variables": {
803797
"CLUSTER": {
@@ -806,6 +800,12 @@
806800
}
807801
},
808802
"Handler": "index.lambda_handler",
803+
"Role": {
804+
"Fn::GetAtt": [
805+
"ASGDrainECSHookFunctionServiceRoleC12963BB",
806+
"Arn"
807+
]
808+
},
809809
"Runtime": "python3.9",
810810
"Tags": [
811811
{
@@ -839,15 +839,15 @@
839839
"ASGDrainECSHookFunctionTopicD6FC59F7": {
840840
"Type": "AWS::SNS::Subscription",
841841
"Properties": {
842-
"Protocol": "lambda",
843-
"TopicArn": {
844-
"Ref": "ASGLifecycleHookDrainHookTopicA8AD4ACB"
845-
},
846842
"Endpoint": {
847843
"Fn::GetAtt": [
848844
"ASGDrainECSHookFunction5F24CF4D",
849845
"Arn"
850846
]
847+
},
848+
"Protocol": "lambda",
849+
"TopicArn": {
850+
"Ref": "ASGLifecycleHookDrainHookTopicA8AD4ACB"
851851
}
852852
}
853853
},
@@ -914,9 +914,9 @@
914914
"AutoScalingGroupName": {
915915
"Ref": "ASG46ED3070"
916916
},
917-
"LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING",
918917
"DefaultResult": "CONTINUE",
919918
"HeartbeatTimeout": 300,
919+
"LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING",
920920
"NotificationTargetARN": {
921921
"Ref": "ASGLifecycleHookDrainHookTopicA8AD4ACB"
922922
},
@@ -940,6 +940,7 @@
940940
"Ref": "ASG46ED3070"
941941
},
942942
"ManagedScaling": {
943+
"InstanceWarmupPeriod": 301,
943944
"Status": "ENABLED",
944945
"TargetCapacity": 100
945946
},

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.default-capacity-provider.js.snapshot/integ.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)