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.

71 lines
2.2 KiB

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