【问题标题】:What's the correct attribute to handle 'NatGatewayID'?处理“NatGatewayID”的正确属性是什么?
【发布时间】:2019-12-04 02:09:39
【问题描述】:

我在检索我的 AWS NATGateway 资源的元数据时遇到问题。我似乎找不到合适的属性来检索 ID。

尝试了各种属性,例如 NAT.id,我仍在检查此处的文档 [1] [2] [3] 以希望解决问题。

[1]https://boto3.amazonaws.com/v1/documentation/api/latest/guide/migration.html

[2]https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_nat_gateways

[3]https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/

import boto3

# Region your instances are in, e.g. 'us-east-1'
region = 'ap-southeast-1'

#instantiate
client = boto3.client('ec2',region)
ids = []

def lambda_handler(event, context):

#lists all the metadata of NAT resources having a TagKey:Schedule     
#Value:OfficeHours

    NATs = client.describe_nat_gateways(
        Filter=[
            {
                'Name': 'tag:Schedule',
                'Values': [
                    'OfficeHours',
                ],
            },
        ],
   )

    for NAT in NATs:
        print('deleted NAT gateways: ' + NAT.NatGatewayId)
#       ids.append(NAT.NatGatewayId)
#       client.delete_nat_gateway(NatGatewayId=ids)

一旦我检索到元数据:NatGatewayID,我应该能够通过 lambda 删除这些资源。

【问题讨论】:

  • 只是补充一下,我确实在顶部包含了 import boto3

标签: python amazon-web-services aws-lambda


【解决方案1】:

来自问题中的 boto 文档:

Response Syntax

{
    'NatGateways': [
        {
            'CreateTime': datetime(2015, 1, 1),
            'DeleteTime': datetime(2015, 1, 1),
            'FailureCode': 'string',
            'FailureMessage': 'string',
            'NatGatewayAddresses': [
                {
                    'AllocationId': 'string',
                    'NetworkInterfaceId': 'string',
                    'PrivateIp': 'string',
                    'PublicIp': 'string'
                },
            ],
            'NatGatewayId': 'string',
            'ProvisionedBandwidth': {
                'ProvisionTime': datetime(2015, 1, 1),
                'Provisioned': 'string',
                'RequestTime': datetime(2015, 1, 1),
                'Requested': 'string',
                'Status': 'string'
            },
            'State': 'pending'|'failed'|'available'|'deleting'|'deleted',
            'SubnetId': 'string',
            'VpcId': 'string',
            'Tags': [
                {
                    'Key': 'string',
                    'Value': 'string'
                },
            ]
        },
    ],
    'NextToken': 'string'
}

响应为dict,其中包含NatGateways 的列表。由于响应是dict,因此无法使用object.property 表示法访问响应的属性;而是object['property']

这个循环应该可以工作:

for NAT in NATs['NatGateways']:
   print('deleted NAT gateways: ' + NAT['NatGatewayId'])
...

【讨论】:

    【解决方案2】:
            After a bit of tweaking, I was able to fix the issue. Here's the code:
    
            import boto3
    
            # Region your instances are in, e.g. 'us-east-1'
            region = 'ap-southeast-1'
    
            #instantiate
            client = boto3.client('ec2',region)
            ids = []
    
            def lambda_handler(event, context):
    
                NATs = client.describe_nat_gateways(
                    Filter=[
                        {
                            'Name': 'tag:Schedule',
                            'Values': [
                                'OfficeHours',
                            ],
                        },
                    ],
                )
                for NAT in NATs['NatGateways']:
                    print('deleted NAT gateways: ' + NAT['NatGatewayId'])
            #       ids.append(NAT['NatGatewayId'])
            #       client.delete_nat_gateway(NatGatewayId=ids)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 2020-08-06
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2010-10-22
      相关资源
      最近更新 更多