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

2024-10-02 16:35:19 发布

您现在位置:Python中文网/ 问答频道 /正文

检索网关资源的元数据时遇到问题。我似乎找不到合适的属性来检索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)

一旦我找回metadata:NatGatewayID,我应该可以通过lambda删除这些资源


Tags: lambdahttpscomclientids属性documentation资源
2条回答

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

相关问题 更多 >