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.

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