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.

76 lines
2.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import time
  2. import boto3
  3. from botocore.exceptions import ClientError
  4. ################################################################################################
  5. #
  6. # Configuration Parameters
  7. #
  8. ################################################################################################
  9. region = 'eu-central-1'
  10. vpc_id = 'vpc-eedd4187'
  11. # if you only have one VPC, vpc_id can be retrieved using:
  12. #
  13. # response = ec2Client.describe_vpcs()
  14. # vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  15. ################################################################################################
  16. #
  17. # boto3 code
  18. #
  19. ################################################################################################
  20. client = boto3.setup_default_session(region_name=region)
  21. ec2Client = boto3.client("ec2")
  22. ec2Resource = boto3.resource('ec2')
  23. elbv2Client = boto3.client('elbv2')
  24. print("Deleting load balancer and deps...")
  25. print("------------------------------------")
  26. try:
  27. response = elbv2Client.describe_load_balancers(Names=['tug-of-war-loadbalancer'])
  28. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  29. response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn)
  30. except ClientError as e:
  31. print(e)
  32. time.sleep(5)
  33. try:
  34. response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
  35. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  36. response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn)
  37. except ClientError as e:
  38. print(e)
  39. print("Deleting old instances...")
  40. print("------------------------------------")
  41. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}])
  42. print(response)
  43. reservations = response['Reservations']
  44. for reservation in reservations:
  45. for instance in reservation['Instances']:
  46. if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
  47. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  48. print(response)
  49. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  50. instanceToTerminate.wait_until_terminated()
  51. print("Delete old security group...")
  52. print("------------------------------------")
  53. try:
  54. response = ec2Client.delete_security_group(GroupName='tug-of-war')
  55. except ClientError as e:
  56. print(e)