5.6K
I had to rename a bunch of instances in AWS to meet the new standard so I had to make a script.
The script is a simple Python 3 script that uses boto3. Change the DryRun variable to True if you want to test first.
Without further ado, here is the script.
import boto3 ec2 = boto3.resource('ec2') # You can filter the instances here, e.g. running, stopped, VPC etc... instances = ec2.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['running', 'stopping', 'stopped']}, {'Name': 'vpc-id', 'Values': ['vpc-7abcdef']}])
Now, for each case just append the code below.
# Create a new tag with 'Application' as a key and 'SQL' as value on all instances that match for instance in instances: instance.create_tags(DryRun=False, Tags=[{ 'Key': 'Application', 'Value': 'SQL' }]
# Modify a tag. This example will change the value to 'Web' for all Keys that equal 'Application' # If the Key 'Application' doesn't exist, it'll be added for instance in instances: instance.create_tags(DryRun=False, Tags=[{ 'Key': 'Application', 'Value': 'Web' }]
# Delete a value from a tag for instance in instances: instance.create_tags(DryRun=False, Tags=[{ 'Key': 'Application', 'Value': '' }]
# Delete a key/value pair for instance in instances: instance.delete_tags(DryRun=False, Tags=[{ 'Key': 'Application', 'Value': 'SQL'
# Delete a key for instance in instances: instance.delete_tags(DryRun=False, Tags=[{ 'Key': 'Application' }])
# WARNING!!!!! Delete all tags for instance in instances: instance.delete_tags()