Guest User

Untitled

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import boto3
  2.  
  3. regions = [
  4. 'us-east-2',
  5. 'us-east-1',
  6. 'us-west-1',
  7. 'us-west-2',
  8. 'ap-south-1',
  9. 'ap-northeast-2',
  10. 'ap-southeast-1',
  11. 'ap-southeast-2',
  12. 'ap-northeast-1',
  13. #'cn-north-1,
  14. 'ca-central-1',
  15. 'eu-central-1',
  16. 'eu-west-1',
  17. 'eu-west-2',
  18. 'eu-west-3',
  19. 'sa-east-1',
  20. ]
  21.  
  22. for region in regions:
  23. ec2 = boto3.client(
  24. 'ec2',
  25. aws_access_key_id='no',
  26. aws_secret_access_key='no',
  27. region_name=region)
  28.  
  29. response = ec2.describe_instances()
  30. instances = []
  31. if response['Reservations']:
  32. instances = response['Reservations'][0]['Instances']
  33. for image in instances:
  34. if image['State']['Name'] == 'stopped':
  35. response = ec2.modify_instance_attribute(
  36. InstanceId=image['InstanceId'],
  37. DisableApiTermination={'Value': False},
  38. )
  39. print(response)
  40.  
  41. instance_ids = [
  42. instance['InstanceId'] for instance in instances
  43. if instance['State']['Name'] == 'stopped'
  44. ]
  45. print(instance_ids)
  46. if instance_ids:
  47. response = ec2.terminate_instances(InstanceIds=instance_ids)
  48. print(response)
Add Comment
Please, Sign In to add comment