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.
 
 
 
 
 

78 lines
2.3 KiB

import boto3
from botocore.exceptions import ClientError
################################################################################################
#
# Configuration Parameters
#
################################################################################################
region = 'eu-central-1'
availabilityZone = 'eu-central-1b'
imageId = 'ami-0cc293023f983ed53'
instanceType = 't3.nano'
keyName = 'srieger-pub'
################################################################################################
#
# boto3 code
#
################################################################################################
client = boto3.setup_default_session(region_name=region)
ec2Client = boto3.client("ec2")
ec2Resource = boto3.resource('ec2')
rdsClient = boto3.client("rds")
response = ec2Client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
subnet_id = ec2Client.describe_subnets(
Filters=[
{
'Name': 'availability-zone', 'Values': [availabilityZone]
}
])['Subnets'][0]['SubnetId']
print("Deleting old instance...")
print("------------------------------------")
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-rds']}])
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("Deleting old DB instance...")
print("------------------------------------")
try:
response = rdsClient.delete_db_instance(
DBInstanceIdentifier='tug-of-war-rds-db1',
SkipFinalSnapshot=True,
DeleteAutomatedBackups=True
)
except ClientError as e:
print(e)
waiter = rdsClient.get_waiter('db_instance_deleted')
waiter.wait(DBInstanceIdentifier='tug-of-war-rds-db1')
#time.sleep(5)
print("Delete old security group...")
print("------------------------------------")
try:
response = ec2Client.delete_security_group(GroupName='tug-of-war-rds')
except ClientError as e:
print(e)