Created
May 15, 2020 04:44
-
-
Save ManojNair/8f2d83f40323fcb1138ef595f3c05590 to your computer and use it in GitHub Desktop.
Clean-up EC2 Instances - Lambda Function
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
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