Skip to content

Instantly share code, notes, and snippets.

@ManojNair
Created May 15, 2020 04:44
Show Gist options
  • Save ManojNair/8f2d83f40323fcb1138ef595f3c05590 to your computer and use it in GitHub Desktop.
Save ManojNair/8f2d83f40323fcb1138ef595f3c05590 to your computer and use it in GitHub Desktop.
Clean-up EC2 Instances - Lambda Function
import json
import boto3
ec2_client = boto3.client('ec2')
client = boto3.client('resourcegroupstaggingapi')
def lambda_handler(event, context):
# TODO implement
ec2_instances = client.get_resources(
TagFilters=[
{
'Key': 'Purpose',
'Values': ['Demo', 'Test']
}
],
ResourceTypeFilters=[
'ec2:instance'
]
)
instances = []
for item in ec2_instances['ResourceTagMappingList']:
instances.append(item['ResourceARN'].split('/')[-1])
response = ec2_client.terminate_instances(InstanceIds=instances)
return {
'statusCode': 200,
'body': response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment