Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- import sys
- import logging
- import datetime
- import time
- # session gets it's credentials from ~/.aws/configure
- session = boto3.Session(profile_name='myprofile')
- ec2 = session.resource('ec2', region_name='eu-west-1')
- client = session.client('ec2', region_name='eu-west-1')
- iam = session.client('iam', region_name='eu-west-1')
- s3 = session.resource('s3', region_name='eu-west-1')
- if ec2 is None:
- print("I could not connect to AWS with the specified credentials") # Tell me if you can't connect
- x = client.describe_snapshots(OwnerIds=['self'])
- count_not_correct = 0
- count_correct = 0
- count_no_tags = 0
- snaps = x['Snapshots']
- tags = x['Snapshots'][0]['Tags'][0]['Value']
- for snap in snaps:
- try:
- n = 0
- #print(snap['Tags'])
- for tag in snap['Tags']:
- if tag['Value'] == 'mytag' or tag['Value'] == 'yourtag':
- print('----> SUCCESS. Snapshot {} has the correct tags {}'
- .format(snap['SnapshotId'], snap['Tags']))
- n += 1
- if n == 0:
- print('Snaphot {} has tags, but not correct ones'.format(snap['SnapshotId']))
- count_not_correct += 1
- # client.create_tags(
- # DryRun=False,
- # Resources=[
- # snap['SnapshotId'],
- # ],
- # Tags=[
- # {
- # 'Key': 'teamDL',
- # 'Value': 'email@me.com'
- # },
- # {
- # 'Key': 'environment',
- # 'Value': 'p'
- # },
- # {
- # 'Key': 'systemCode',
- # 'Value': 'codexxx'
- # }
- # ]
- # )
- # print('Tagged {}'.format(snap['SnapshotId']))
- # time.sleep(2)
- else:
- count_correct += 1
- except:
- print('Snapshot {} doesnt have tags. Applying some now ....'.format(snap['SnapshotId']))
- count_no_tags += 1
- # client.create_tags(
- # DryRun=False,
- # Resources=[
- # snap['SnapshotId'],
- # ],
- # Tags=[
- # {
- # 'Key': 'teamDL',
- # 'Value': 'email@me.com'
- # },
- # {
- # 'Key': 'environment',
- # 'Value': 'p'
- # },
- # {
- # 'Key': 'systemCode',
- # 'Value': 'codexxx'
- # }
- # ]
- # )
- # print('Tagged {}'.format(snap['SnapshotId']))
- # time.sleep(2)
- print('Snapshots with correct tags = {}'.format(count_correct))
- print('Snapshots with incorrect tags = {}'.format(count_not_correct))
- print('Snapshots with no tags = {}'.format(count_no_tags))
- total = count_correct + count_no_tags + count_not_correct
- print('Total snapshots scanned = {}'.format(total))
- ################ BUCKETS ###########################
Add Comment
Please, Sign In to add comment