Guest User

Untitled

a guest
Nov 25th, 2017
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import boto3
  2. import sys
  3. import logging
  4. import datetime
  5. import time
  6.  
  7. # session gets it's credentials from ~/.aws/configure
  8. session = boto3.Session(profile_name='myprofile')
  9. ec2 = session.resource('ec2', region_name='eu-west-1')
  10. client = session.client('ec2', region_name='eu-west-1')
  11. iam = session.client('iam', region_name='eu-west-1')
  12. s3 = session.resource('s3', region_name='eu-west-1')
  13.  
  14. if ec2 is None:
  15. print("I could not connect to AWS with the specified credentials") # Tell me if you can't connect
  16.  
  17.  
  18. x = client.describe_snapshots(OwnerIds=['self'])
  19. count_not_correct = 0
  20. count_correct = 0
  21. count_no_tags = 0
  22.  
  23. snaps = x['Snapshots']
  24. tags = x['Snapshots'][0]['Tags'][0]['Value']
  25.  
  26. for snap in snaps:
  27. try:
  28. n = 0
  29. #print(snap['Tags'])
  30. for tag in snap['Tags']:
  31.  
  32. if tag['Value'] == 'mytag' or tag['Value'] == 'yourtag':
  33. print('----> SUCCESS. Snapshot {} has the correct tags {}'
  34. .format(snap['SnapshotId'], snap['Tags']))
  35. n += 1
  36.  
  37. if n == 0:
  38. print('Snaphot {} has tags, but not correct ones'.format(snap['SnapshotId']))
  39. count_not_correct += 1
  40. # client.create_tags(
  41. # DryRun=False,
  42. # Resources=[
  43. # snap['SnapshotId'],
  44. # ],
  45. # Tags=[
  46. # {
  47. # 'Key': 'teamDL',
  48. # 'Value': 'email@me.com'
  49. # },
  50. # {
  51. # 'Key': 'environment',
  52. # 'Value': 'p'
  53. # },
  54. # {
  55. # 'Key': 'systemCode',
  56. # 'Value': 'codexxx'
  57. # }
  58. # ]
  59. # )
  60. # print('Tagged {}'.format(snap['SnapshotId']))
  61. # time.sleep(2)
  62. else:
  63. count_correct += 1
  64.  
  65. except:
  66. print('Snapshot {} doesnt have tags. Applying some now ....'.format(snap['SnapshotId']))
  67. count_no_tags += 1
  68. # client.create_tags(
  69. # DryRun=False,
  70. # Resources=[
  71. # snap['SnapshotId'],
  72. # ],
  73. # Tags=[
  74. # {
  75. # 'Key': 'teamDL',
  76. # 'Value': 'email@me.com'
  77. # },
  78. # {
  79. # 'Key': 'environment',
  80. # 'Value': 'p'
  81. # },
  82. # {
  83. # 'Key': 'systemCode',
  84. # 'Value': 'codexxx'
  85. # }
  86. # ]
  87. # )
  88. # print('Tagged {}'.format(snap['SnapshotId']))
  89. # time.sleep(2)
  90.  
  91.  
  92.  
  93. print('Snapshots with correct tags = {}'.format(count_correct))
  94. print('Snapshots with incorrect tags = {}'.format(count_not_correct))
  95. print('Snapshots with no tags = {}'.format(count_no_tags))
  96. total = count_correct + count_no_tags + count_not_correct
  97. print('Total snapshots scanned = {}'.format(total))
  98.  
  99. ################ BUCKETS ###########################
Add Comment
Please, Sign In to add comment