处理“ NatGatewayID”的正确属性是什么?

时间:2019-10-01 09:49:34

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

我在检索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删除这些资源。

2 个答案:

答案 0 :(得分:0)

问题中的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'])
...

答案 1 :(得分:0)

        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)