You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.3 KiB

  1. import boto3
  2. from botocore.exceptions import ClientError
  3. ################################################################################################
  4. #
  5. # Configuration Parameters
  6. #
  7. ################################################################################################
  8. region = 'eu-central-1'
  9. availabilityZone = 'eu-central-1b'
  10. imageId = 'ami-0cc293023f983ed53'
  11. instanceType = 't3.nano'
  12. keyName = 'srieger-pub'
  13. ################################################################################################
  14. #
  15. # boto3 code
  16. #
  17. ################################################################################################
  18. client = boto3.setup_default_session(region_name=region)
  19. ec2Client = boto3.client("ec2")
  20. ec2Resource = boto3.resource('ec2')
  21. rdsClient = boto3.client("rds")
  22. response = ec2Client.describe_vpcs()
  23. vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  24. subnet_id = ec2Client.describe_subnets(
  25. Filters=[
  26. {
  27. 'Name': 'availability-zone', 'Values': [availabilityZone]
  28. }
  29. ])['Subnets'][0]['SubnetId']
  30. print("Deleting old instance...")
  31. print("------------------------------------")
  32. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-rds']}])
  33. print(response)
  34. reservations = response['Reservations']
  35. for reservation in reservations:
  36. for instance in reservation['Instances']:
  37. if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
  38. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  39. print(response)
  40. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  41. instanceToTerminate.wait_until_terminated()
  42. print("Deleting old DB instance...")
  43. print("------------------------------------")
  44. try:
  45. response = rdsClient.delete_db_instance(
  46. DBInstanceIdentifier='tug-of-war-rds-db1',
  47. SkipFinalSnapshot=True,
  48. DeleteAutomatedBackups=True
  49. )
  50. except ClientError as e:
  51. print(e)
  52. waiter = rdsClient.get_waiter('db_instance_deleted')
  53. waiter.wait(DBInstanceIdentifier='tug-of-war-rds-db1')
  54. #time.sleep(5)
  55. print("Delete old security group...")
  56. print("------------------------------------")
  57. try:
  58. response = ec2Client.delete_security_group(GroupName='tug-of-war-rds')
  59. except ClientError as e:
  60. print(e)