简体   繁体   English

AWS Lambda删除默认VPC

[英]AWS lambda to delete default VPC

New to cloud, Can anyone help to correct this code 云新手,任何人都可以帮助纠正此代码

This module is to list the regions and delete the complete default vpc via a lambda function. 该模块将列出区域并通过lambda函数删除完整的默认vpc。

Getting below error while testing this: Syntax error in module 'lambda function': unindent does not match any outer indentation level 测试此错误时遇到以下错误:模块“ lambda函数”中的语法错误:unindent与任何外部缩进级别都不匹配

Please help on this Removed other function like vpc, sc as the block looks very big here in the post just added the igw for understanding.. 请帮助此已删除的其他功能,例如vpc,sc,因为该块在此处刚刚添加了igw,以供理解。

Need assistance 需要帮助

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)

It looks to me a code indentation issue. 在我看来,这是代码缩进的问题。 Please try with this 请尝试这个

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM