Skip to content

Instantly share code, notes, and snippets.

@GMKBabu
Forked from fideloper/tags.py
Created December 31, 2019 06:01
Show Gist options
  • Save GMKBabu/43d2e3c85ca41aa707dd4a9436cf2a29 to your computer and use it in GitHub Desktop.
Save GMKBabu/43d2e3c85ca41aa707dd4a9436cf2a29 to your computer and use it in GitHub Desktop.
Change many aws instances tags (boto3)
import boto3
import sys
ec2 = boto3.client('ec2')
# Grab where backup retention is 14 days so we can reduce it to 7
instances = ec2.describe_instances(Filters=[{'Name': 'tag:Retention', 'Values': ['14']}])
ids = []
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
ids.append(instance['InstanceId'])
print "Changing tags for %d instances" % len(ids)
ec2.create_tags(
Resources=ids,
Tags=[
{
'Key': 'Retention',
'Value': '7'
}
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment