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.

90 lines
2.9 KiB

  1. import boto3
  2. from botocore.exceptions import ClientError
  3. ################################################################################################
  4. #
  5. # Configuration Parameters
  6. #
  7. ################################################################################################
  8. # place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom,
  9. # Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials)
  10. # changed to use us-east, to be able to use AWS Educate Classroom
  11. region = 'us-east-1'
  12. availabilityZone = 'us-east-1a'
  13. # region = 'eu-central-1'
  14. # availabilityZone = 'eu-central-1b'
  15. # AMI ID of Amazon Linux 2 image 64-bit x86 in us-east-1 (can be retrieved, e.g., at
  16. # https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:)
  17. imageId = 'ami-0d5eff06f840b45e9'
  18. # for eu-central-1, AMI ID of Amazon Linux 2 would be:
  19. # imageId = 'ami-0cc293023f983ed53'
  20. # potentially change instanceType to t2.micro for "free tier" if using a regular account
  21. instanceType = 't3.nano'
  22. keyName = 'srieger-pub'
  23. ################################################################################################
  24. #
  25. # boto3 code
  26. #
  27. ################################################################################################
  28. client = boto3.setup_default_session(region_name=region)
  29. ec2Client = boto3.client("ec2")
  30. ec2Resource = boto3.resource('ec2')
  31. rdsClient = boto3.client("rds")
  32. response = ec2Client.describe_vpcs()
  33. vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  34. subnet_id = ec2Client.describe_subnets(
  35. Filters=[
  36. {
  37. 'Name': 'availability-zone', 'Values': [availabilityZone]
  38. }
  39. ])['Subnets'][0]['SubnetId']
  40. print("Deleting old instance...")
  41. print("------------------------------------")
  42. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-rds']}])
  43. print(response)
  44. reservations = response['Reservations']
  45. for reservation in reservations:
  46. for instance in reservation['Instances']:
  47. if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
  48. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  49. print(response)
  50. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  51. instanceToTerminate.wait_until_terminated()
  52. print("Deleting old DB instance...")
  53. print("------------------------------------")
  54. try:
  55. response = rdsClient.delete_db_instance(
  56. DBInstanceIdentifier='tug-of-war-rds-db1',
  57. SkipFinalSnapshot=True,
  58. DeleteAutomatedBackups=True
  59. )
  60. except ClientError as e:
  61. print(e)
  62. waiter = rdsClient.get_waiter('db_instance_deleted')
  63. waiter.wait(DBInstanceIdentifier='tug-of-war-rds-db1')
  64. #time.sleep(5)
  65. print("Delete old security group...")
  66. print("------------------------------------")
  67. try:
  68. response = ec2Client.delete_security_group(GroupName='tug-of-war-rds')
  69. except ClientError as e:
  70. print(e)