AWS Lambda Boto描述_体积

2024-04-19 05:31:56 发布

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

我是Python和Lambda的新手,我试图获得所有区域的in-use卷的列表。在

from datetime import datetime, date
import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')

    # Get list of regions
    regions = ec2.describe_regions().get('Regions',[] )

    # Iterate over regions
    for region in regions:
        print "Looking at region %s " % region['RegionName']
        reg=region['RegionName']

        # Connect to region
        ec2 = boto3.client('ec2',region_name=reg)

        # Get all in-use volumes    
        volumes = ec2.describe_volumes( Filters=[{'Name': 'status', 'Values': ['in-use']}])

        for volume in volumes:
            print "Looking at volume %s" % volume['VolumeId']

我一直收到以下错误,无法找出原因:

^{pr2}$

Tags: inimportclientforgetdatetimeuseboto3
1条回答
网友
1楼 · 发布于 2024-04-19 05:31:56

volumes不是卷的dict。在

>>> volumes.keys()
['ResponseMetadata', u'Volumes']

所以您需要遍历volumes['Volumes']。试试这个:

^{pr2}$

输出

Looking at region ap-south-1
Looking at volume vol-1234853ed7652bbb1
Looking at volume vol-00aac56781f21a83
Looking at region eu-west-2
Looking at region eu-west-1
Looking at region ap-northeast-2
Looking at region ap-northeast-1

相关问题 更多 >