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.
33 lines
1.1 KiB
33 lines
1.1 KiB
import boto3
|
|
from botocore.exceptions import ClientError
|
|
|
|
region = 'eu-central-1'
|
|
|
|
client = boto3.setup_default_session(region_name=region)
|
|
ec2Client = boto3.client("ec2")
|
|
ec2Resource = boto3.resource('ec2')
|
|
|
|
response = ec2Client.describe_vpcs()
|
|
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
|
|
|
|
print("Deleting old instance...")
|
|
print("------------------------------------")
|
|
|
|
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}])
|
|
print(response)
|
|
reservations = response['Reservations']
|
|
for reservation in reservations:
|
|
for instance in reservation['Instances']:
|
|
if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
|
|
response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
|
|
print(response)
|
|
instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
|
|
instanceToTerminate.wait_until_terminated()
|
|
|
|
print("Delete old security group...")
|
|
print("------------------------------------")
|
|
|
|
try:
|
|
response = ec2Client.delete_security_group(GroupName='tug-of-war')
|
|
except ClientError as e:
|
|
print(e)
|