91 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import boto3
 | 
						|
from botocore.exceptions import ClientError
 | 
						|
 | 
						|
 | 
						|
################################################################################################
 | 
						|
#
 | 
						|
# Configuration Parameters
 | 
						|
#
 | 
						|
################################################################################################
 | 
						|
 | 
						|
 | 
						|
# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom,
 | 
						|
# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials)
 | 
						|
 | 
						|
# changed to use us-east, to be able to use AWS Educate Classroom
 | 
						|
region = 'us-east-1'
 | 
						|
availabilityZone = 'us-east-1a'
 | 
						|
# region = 'eu-central-1'
 | 
						|
# availabilityZone = 'eu-central-1b'
 | 
						|
 | 
						|
# AMI ID of Amazon Linux 2 image 64-bit x86 in us-east-1 (can be retrieved, e.g., at
 | 
						|
# https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:)
 | 
						|
imageId = 'ami-0d5eff06f840b45e9'
 | 
						|
# for eu-central-1, AMI ID of Amazon Linux 2 would be:
 | 
						|
# imageId = 'ami-0cc293023f983ed53'
 | 
						|
 | 
						|
# potentially change instanceType to t2.micro for "free tier" if using a regular account
 | 
						|
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) |