Skip to content

Instantly share code, notes, and snippets.

@PartTimeLegend
Last active July 20, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PartTimeLegend/b5de8b455d059e15742a7976dce75708 to your computer and use it in GitHub Desktop.
Save PartTimeLegend/b5de8b455d059e15742a7976dce75708 to your computer and use it in GitHub Desktop.
import boto3
ec2Client = boto3.client('ec2')
ec2Resource = boto3.resource('ec2')
for instance in ec2Resource.instances.all():
for tag in instance.tags:
if tag['Key'] == 'Name':
list_of_snapshots = []
check_value = []
check_value.append(tag['Value'] + '*')
snapshotsByTag = ec2Client.describe_snapshots(Filters=[{'Name':'tag:Name', 'Values': [check_value[0]]}])['Snapshots']
snapshotsByDescription = ec2Client.describe_snapshots(Filters=[{'Name':'description', 'Values': [check_value[0]]}])['Snapshots']
for tag in instance.tags:
if tag['Key'] == 'Environment':
for snapshot in snapshotsByTag:
if snapshot:
list_of_snapshots.append(snapshot['SnapshotId'])
ec2Client.create_tags(Resources=[snapshot['SnapshotId']], Tags=[{'Key':'Environment', 'Value':tag['Value']}], DryRun=False)
for snapshot in snapshotsByDescription:
if snapshot:
ec2Client.create_tags(Resources=[snapshot['SnapshotId']], Tags=[{'Key':'Environment', 'Value':tag['Value']}], DryRun=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment