AWS Lambda删除默认VPC

时间:2018-11-30 07:06:11

标签: python amazon-web-services lambda boto3 vpc

云新手,任何人都可以帮助纠正此代码

该模块将列出区域并通过lambda函数删除完整的默认vpc。

测试此错误时出现以下错误: 模块“ lambda函数”中的语法错误:unindent与任何外部缩进级别都不匹配

请对此提供帮助  删除了其他功能,例如vpc,sc,因为该块在此处的位置看起来非常大,只是添加了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)

1 个答案:

答案 0 :(得分:2)

在我看来,这是代码缩进的问题。请尝试这个

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)
相关问题