【问题标题】:AWS Lambda Boto describe_volumesAWS Lambda Boto describe_volumes
【发布时间】:2017-08-23 11:48:51
【问题描述】:

我是 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']

我不断收到以下错误,无法弄清楚原因:

String indices must be integers, not str: TypeError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 22, in lambda_handler
    print "Looking at volume %s" % volume['VolumeId']
TypeError: string indices must be integers, not str

【问题讨论】:

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


    【解决方案1】:

    volumes 不是卷的字典。

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

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

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

    输出

    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
    

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2019-05-12
      • 2016-06-17
      • 2020-10-05
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多