I've created a lampda function for stopping ec2 instances with a specific tag, python code is presented below. Main task of this solution is to stop all instances with a tag "name: purpose, value: temp". When I execute this script all ec2 instances are being stopped. I suppose that something is wrong with this following filter instances = ec2.instances.filter(Filters=[{'Name': 'tag:purpose', 'Values': ['temp']}])
.
Function code below:
import boto3
def lambda_handler(event, context):
client = boto3.client('ec2')
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
for region in ec2_regions:
ec2 = boto3.resource('ec2',region_name=region)
instances = ec2.instances.filter(Filters=[{'Name': 'tag:purpose', 'Values': ['temp']}])
RunningInstances = [instance.id for instance in instances]
for i in RunningInstances:
stoppingInstances = ec2.instances.stop(i)