##Came across this code to delete the default VPC using Boto3:
import boto3
import cfnresponse
import threading
import logging
ec2 = boto3.client('ec2')
def getdefaultvpc():
vpcs = ec2.describe_vpcs(
Filters=[
{
'Name': 'is-default',
'Values': [
'true',
]
},
]
)
if len(vpcs['Vpcs']) == 0:
exit(0)
return vpcs['Vpcs'][0]
def deleteigws(vpcid):
igws = ec2.describe_internet_gateways(
Filters=[
{
'Name': 'attachment.vpc-id',
'Values': [
vpcid,
]
},
]
)
if len(igws['InternetGateways']) > 0:
igwid = igws['InternetGateways'][0]['InternetGatewayId']
attachstate = igws['InternetGateways'][0]['Attachments'][0]['State']
ec2.detach_internet_gateway(
InternetGatewayId=igwid,
VpcId=vpcid
)
while attachstate != 'detached':
igws = ec2.describe_internet_gateways(
InternetGatewayIds=[
igwid
]
)
if len(igws['InternetGateways'][0]['Attachments']) > 0:
attachstate = igws['InternetGateways'][0]['Attachments'][0]['State']
else:
attachstate = 'detached'
ec2.delete_internet_gateway(
InternetGatewayId=igwid
)
def deletesubnets(vpcid):
subnets = ec2.describe_subnets(
Filters=[
{
'Name': 'vpc-id',
'Values': [
vpcid,
]
},
]
)
for subnet in subnets['Subnets']:
ec2.delete_subnet(
SubnetId=subnet['SubnetId']
)
def main():
vpcinfo = getdefaultvpc()
deleteigws(vpcinfo['VpcId'])
deletesubnets(vpcinfo['VpcId'])
ec2.delete_vpc(
VpcId=vpcinfo['VpcId']
)
def timeout(event, context):
logging.error('Execution is about to time out, sending failure response to CloudFormation')
cfnresponse.send(event, context, cfnresponse.FAILED, {}, None)
def lambda_handler(event, context):
timer = threading.Timer((context.get_remaining_time_in_millis() / 1000.00) - 0.5, timeout, args=[event, context])
timer.start()
status = cfnresponse.SUCCESS
try:
if event['RequestType'] == 'Create':
main()
except Exception as e:
logging.error('Exception: %s' % e, exc_info=True)
status = cfnresponse.FAILED
finally:
timer.cancel()
cfnresponse.send(event, context, status, {}, None)`enter code here`