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.

64 lines
1.9 KiB

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