-
Notifications
You must be signed in to change notification settings - Fork 19
Container Instance Draining in Amazon ECS #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
praveenraghav01
wants to merge
14
commits into
master
Choose a base branch
from
ecs_instance_draining
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f86025b
added cli var for ecs instance draining
faaa517
Lambda code for ecs instance draining
94518d0
ECS instance draining
70bfc6b
remove unwanted return
94c9637
remove unwanted var
f949c58
remove unwanted var
500f9a0
remove template
089cc41
remove unwanted var
fb7fd44
update var name
02168e4
remove unwanted test
7e383e7
remove heartbeat timeout
44aab41
Merge branch 'master' into ecs_instance_draining
praveenraghav01 d5a975c
added down scaling policy
praveenraghav01 34a2637
added awscli v2
praveenraghav01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import json | ||
| import time | ||
| import boto3 | ||
| import os | ||
|
|
||
| ECS = boto3.client('ecs') | ||
| ASG = boto3.client('autoscaling') | ||
| SNS = boto3.client('sns') | ||
|
|
||
| def find_ecs_instance_info(instance_id,cluster_name): | ||
| paginator = ECS.get_paginator('list_container_instances') | ||
| for list_resp in paginator.paginate(cluster=cluster_name): | ||
| arns = list_resp['containerInstanceArns'] | ||
| desc_resp = ECS.describe_container_instances(cluster=cluster_name, | ||
| containerInstances=arns) | ||
| for container_instance in desc_resp['containerInstances']: | ||
| if container_instance['ec2InstanceId'] != instance_id: | ||
| continue | ||
| print('Found instance: id=%s, arn=%s, status=%s, runningTasksCount=%s' % | ||
| (instance_id, container_instance['containerInstanceArn'], | ||
| container_instance['status'], container_instance['runningTasksCount'])) | ||
| return (container_instance['containerInstanceArn'], | ||
| container_instance['status'], container_instance['runningTasksCount']) | ||
| return None, None, 0 | ||
|
|
||
| def instance_has_running_tasks(instance_id,cluster_name): | ||
| (instance_arn, container_status, running_tasks) = find_ecs_instance_info(instance_id,cluster_name) | ||
| if instance_arn is None: | ||
| print('Could not find instance ID %s. Letting autoscaling kill the instance.' % | ||
| (instance_id)) | ||
| return False | ||
| if container_status != 'DRAINING': | ||
| print('Setting container instance %s (%s) to DRAINING' % | ||
| (instance_id, instance_arn)) | ||
| ECS.update_container_instances_state(cluster=cluster_name, | ||
| containerInstances=[instance_arn], | ||
| status='DRAINING') | ||
| return running_tasks > 0 | ||
|
|
||
| def lambda_handler(event, context): | ||
| msg = json.loads(event['Records'][0]['Sns']['Message']) | ||
| print("Event: ", msg) | ||
| if 'LifecycleTransition' not in msg.keys() or \ | ||
| msg['LifecycleTransition'].find('autoscaling:EC2_INSTANCE_TERMINATING') == -1: | ||
| print('Exiting since the lifecycle transition is not EC2_INSTANCE_TERMINATING.') | ||
| return | ||
| if instance_has_running_tasks(msg['EC2InstanceId'], msg['NotificationMetadata']): | ||
| print('Tasks are still running on instance %s; posting msg to SNS topic %s' % | ||
| (msg['EC2InstanceId'], event['Records'][0]['Sns']['TopicArn'])) | ||
| time.sleep(5) | ||
| sns_resp = SNS.publish(TopicArn=event['Records'][0]['Sns']['TopicArn'], | ||
| Message=json.dumps(msg), | ||
| Subject='Publishing SNS msg to invoke Lambda again.') | ||
| print('Posted msg %s to SNS topic.' % (sns_resp['MessageId'])) | ||
| else: | ||
| print('No tasks are running on instance %s; setting lifecycle to complete' % | ||
| (msg['EC2InstanceId'])) | ||
| ASG.complete_lifecycle_action(LifecycleHookName=msg['LifecycleHookName'], | ||
| AutoScalingGroupName=msg['AutoScalingGroupName'], | ||
| LifecycleActionResult='CONTINUE', | ||
| InstanceId=msg['EC2InstanceId']) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.