AWS lambda 删除默认 VPC
AWS lambda to delete default VPC
云新手,任何人都可以帮助更正此代码
此模块用于列出区域并通过 lambda 函数删除完整的默认 vpc。
测试时出现以下错误:
模块 'lambda function' 中的语法错误:unindent 与任何外部缩进级别都不匹配
请帮忙解决这个问题
删除了其他功能,如 vpc、sc,因为块在 post 中看起来很大,只是为了理解添加了 igw..
需要帮助
def lambda_handler(event, context):
# TODO implement
#for looping across the regions
regionList=[]
region=boto3.client('ec2')
regions=region.describe_regions()
#print('the total region in aws are : ',len(regions['Regions']))
for r in range(0,len(regions['Regions'])):
regionaws=regions['Regions'][r]['RegionName']
regionList.append(regionaws)
#print(regionList)
#regionsl=['us-east-1']
#sending regions as a parameter to the remove_default_vps function
res=remove_default_vpcs(regionList)
return {
'status':res
}
def get_default_vpcs(client):
vpc_list = []
vpcs = client.describe_vpcs(
Filters=[
{
'Name' : 'isDefault',
'Values' : [
'true',
],
},
]
)
vpcs_str = json.dumps(vpcs)
resp = json.loads(vpcs_str)
data = json.dumps(resp['Vpcs'])
vpcs = json.loads(data)
for vpc in vpcs:
vpc_list.append(vpc['VpcId'])
return vpc_list
def del_igw(ec2, vpcid):
""" Detach and delete the internet-gateway """
vpc_resource = ec2.Vpc(vpcid)
igws = vpc_resource.internet_gateways.all()
if igws:
for igw in igws:
try:
print("Detaching and Removing igw-id: ", igw.id) if (VERBOSE == 1) else ""
igw.detach_from_vpc(
VpcId=vpcid
)
igw.delete(
)
except boto3.exceptions.Boto3Error as e:
print(e)
def remove_default_vpcs():
for region in res:
try:
client = boto3.client('ec2', region_name = region)
ec2 = boto3.resource('ec2', region_name = region)
vpcs = get_default_vpcs(client)
except boto3.exceptions.Boto3Error as e:
print(e)
exit(1)
else:
for vpc in vpcs:
print("\n" + "\n" + "REGION:" + region + "\n" + "VPC Id:" + vpc)
del_igw(ec2, vpc)
print(completed)
在我看来是代码缩进问题。请试试这个
def lambda_handler(event, context):
# TODO implement
#for looping across the regions
regionList=[]
region=boto3.client('ec2')
regions=region.describe_regions()
#print('the total region in aws are : ',len(regions['Regions']))
for r in range(0,len(regions['Regions'])):
regionaws=regions['Regions'][r]['RegionName']
regionList.append(regionaws)
#print(regionList)
#regionsl=['us-east-1']
#sending regions as a parameter to the remove_default_vps function
res=remove_default_vpcs(regionList)
return {
'status':res
}
def get_default_vpcs(client):
vpc_list = []
vpcs = client.describe_vpcs(
Filters=[
{
'Name' : 'isDefault',
'Values' : [
'true',
],
},
]
)
vpcs_str = json.dumps(vpcs)
resp = json.loads(vpcs_str)
data = json.dumps(resp['Vpcs'])
vpcs = json.loads(data)
for vpc in vpcs:
vpc_list.append(vpc['VpcId'])
return vpc_list
def del_igw(ec2, vpcid):
""" Detach and delete the internet-gateway """
vpc_resource = ec2.Vpc(vpcid)
igws = vpc_resource.internet_gateways.all()
if igws:
for igw in igws:
try:
print("Detaching and Removing igw-id: ", igw.id) if (VERBOSE == 1) else ""
igw.detach_from_vpc(
VpcId=vpcid
)
igw.delete(
)
except boto3.exceptions.Boto3Error as e:
print(e)
def remove_default_vpcs():
for region in res:
try:
client = boto3.client('ec2', region_name = region)
ec2 = boto3.resource('ec2', region_name = region)
vpcs = get_default_vpcs(client)
except boto3.exceptions.Boto3Error as e:
print(e)
exit(1)
else:
for vpc in vpcs:
print("\n" + "\n" + "REGION:" + region + "\n" + "VPC Id:" + vpc)
del_igw(ec2, vpc)
print(completed)
云新手,任何人都可以帮助更正此代码
此模块用于列出区域并通过 lambda 函数删除完整的默认 vpc。
测试时出现以下错误: 模块 'lambda function' 中的语法错误:unindent 与任何外部缩进级别都不匹配
请帮忙解决这个问题 删除了其他功能,如 vpc、sc,因为块在 post 中看起来很大,只是为了理解添加了 igw..
需要帮助
def lambda_handler(event, context):
# TODO implement
#for looping across the regions
regionList=[]
region=boto3.client('ec2')
regions=region.describe_regions()
#print('the total region in aws are : ',len(regions['Regions']))
for r in range(0,len(regions['Regions'])):
regionaws=regions['Regions'][r]['RegionName']
regionList.append(regionaws)
#print(regionList)
#regionsl=['us-east-1']
#sending regions as a parameter to the remove_default_vps function
res=remove_default_vpcs(regionList)
return {
'status':res
}
def get_default_vpcs(client):
vpc_list = []
vpcs = client.describe_vpcs(
Filters=[
{
'Name' : 'isDefault',
'Values' : [
'true',
],
},
]
)
vpcs_str = json.dumps(vpcs)
resp = json.loads(vpcs_str)
data = json.dumps(resp['Vpcs'])
vpcs = json.loads(data)
for vpc in vpcs:
vpc_list.append(vpc['VpcId'])
return vpc_list
def del_igw(ec2, vpcid):
""" Detach and delete the internet-gateway """
vpc_resource = ec2.Vpc(vpcid)
igws = vpc_resource.internet_gateways.all()
if igws:
for igw in igws:
try:
print("Detaching and Removing igw-id: ", igw.id) if (VERBOSE == 1) else ""
igw.detach_from_vpc(
VpcId=vpcid
)
igw.delete(
)
except boto3.exceptions.Boto3Error as e:
print(e)
def remove_default_vpcs():
for region in res:
try:
client = boto3.client('ec2', region_name = region)
ec2 = boto3.resource('ec2', region_name = region)
vpcs = get_default_vpcs(client)
except boto3.exceptions.Boto3Error as e:
print(e)
exit(1)
else:
for vpc in vpcs:
print("\n" + "\n" + "REGION:" + region + "\n" + "VPC Id:" + vpc)
del_igw(ec2, vpc)
print(completed)
在我看来是代码缩进问题。请试试这个
def lambda_handler(event, context):
# TODO implement
#for looping across the regions
regionList=[]
region=boto3.client('ec2')
regions=region.describe_regions()
#print('the total region in aws are : ',len(regions['Regions']))
for r in range(0,len(regions['Regions'])):
regionaws=regions['Regions'][r]['RegionName']
regionList.append(regionaws)
#print(regionList)
#regionsl=['us-east-1']
#sending regions as a parameter to the remove_default_vps function
res=remove_default_vpcs(regionList)
return {
'status':res
}
def get_default_vpcs(client):
vpc_list = []
vpcs = client.describe_vpcs(
Filters=[
{
'Name' : 'isDefault',
'Values' : [
'true',
],
},
]
)
vpcs_str = json.dumps(vpcs)
resp = json.loads(vpcs_str)
data = json.dumps(resp['Vpcs'])
vpcs = json.loads(data)
for vpc in vpcs:
vpc_list.append(vpc['VpcId'])
return vpc_list
def del_igw(ec2, vpcid):
""" Detach and delete the internet-gateway """
vpc_resource = ec2.Vpc(vpcid)
igws = vpc_resource.internet_gateways.all()
if igws:
for igw in igws:
try:
print("Detaching and Removing igw-id: ", igw.id) if (VERBOSE == 1) else ""
igw.detach_from_vpc(
VpcId=vpcid
)
igw.delete(
)
except boto3.exceptions.Boto3Error as e:
print(e)
def remove_default_vpcs():
for region in res:
try:
client = boto3.client('ec2', region_name = region)
ec2 = boto3.resource('ec2', region_name = region)
vpcs = get_default_vpcs(client)
except boto3.exceptions.Boto3Error as e:
print(e)
exit(1)
else:
for vpc in vpcs:
print("\n" + "\n" + "REGION:" + region + "\n" + "VPC Id:" + vpc)
del_igw(ec2, vpc)
print(completed)