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.

57 lines
1.9 KiB

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. region = 'eu-central-1'
  5. client = boto3.setup_default_session(region_name=region)
  6. ec2Client = boto3.client("ec2")
  7. ec2Resource = boto3.resource('ec2')
  8. response = ec2Client.describe_vpcs()
  9. vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  10. elbv2Client = boto3.client('elbv2')
  11. print("Deleting load balancer and deps...")
  12. print("------------------------------------")
  13. try:
  14. response = elbv2Client.describe_load_balancers(Names=['tug-of-war-loadbalancer'])
  15. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  16. response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn)
  17. except ClientError as e:
  18. print(e)
  19. time.sleep(5)
  20. try:
  21. response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
  22. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  23. response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn)
  24. except ClientError as e:
  25. print(e)
  26. print("Deleting old instances...")
  27. print("------------------------------------")
  28. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}])
  29. print(response)
  30. reservations = response['Reservations']
  31. for reservation in reservations:
  32. for instance in reservation['Instances']:
  33. if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
  34. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  35. print(response)
  36. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  37. instanceToTerminate.wait_until_terminated()
  38. print("Delete old security group...")
  39. print("------------------------------------")
  40. try:
  41. response = ec2Client.delete_security_group(GroupName='tug-of-war')
  42. except ClientError as e:
  43. print(e)