首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS Lambda Boto describe_volumes

AWS Lambda Boto describe_volumes
EN

Stack Overflow用户
提问于 2017-02-13 19:48:32
回答 1查看 1.9K关注 0票数 0

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

代码语言:javascript
复制
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']

我一直收到以下错误,但不知道原因:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-13 19:59:57

volumes并不是一个卷的小块。

代码语言:javascript
复制
>>> volumes.keys()
['ResponseMetadata', u'Volumes']

因此,您需要遍历volumes['Volumes']。试试这个:

代码语言:javascript
复制
for volume in volumes['Volumes']:
    print "Looking at volume %s" % volume['VolumeId']

输出

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42212334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档