Skip to content

Instantly share code, notes, and snippets.

@georgepaoli
Created November 5, 2020 02:06
Show Gist options
  • Save georgepaoli/a7043f23d8d69fde159745368cfd8336 to your computer and use it in GitHub Desktop.
Save georgepaoli/a7043f23d8d69fde159745368cfd8336 to your computer and use it in GitHub Desktop.
Detach EC2 from ASG for SPOT interruptions events
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