Created
November 5, 2020 02:06
-
-
Save georgepaoli/a7043f23d8d69fde159745368cfd8336 to your computer and use it in GitHub Desktop.
Detach EC2 from ASG for SPOT interruptions events
This file contains 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
import boto3 | |
def lambda_handler(event, context): | |
instanceId = event['detail']['instance-id'] | |
asgName = 'xxx' | |
ec2client = boto3.client('ec2') | |
describeTags = ec2client.describe_tags(Filters=[{'Name': 'resource-id','Values':[instanceId]}]) | |
for tag in describeTags['Tags']: | |
if tag['Key'] == 'aws:autoscaling:groupName': | |
asgName = tag['Value'] | |
asgclient = boto3.client('autoscaling') | |
asgclient.detach_instances( | |
InstanceIds=[ | |
instanceId, | |
], | |
AutoScalingGroupName=asgName, | |
ShouldDecrementDesiredCapacity=False | |
) | |
print("Detaching success instance", instanceId, " from ASG: ", asgName) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment