【发布时间】:2017-02-10 22:37:27
【问题描述】:
我正在为 AWS lambda 使用脚本 https://github.com/xombiemp/ec2-take-snapshots-lambda/blob/master/ec2-take-snapshots-lambda.py。
我想创建快照并使用 EBS 卷的相同标签“名称”设置标签“名称”,但在 boto 3 的文档中找不到如何获取 EBS 卷的标签值。
【问题讨论】:
我正在为 AWS lambda 使用脚本 https://github.com/xombiemp/ec2-take-snapshots-lambda/blob/master/ec2-take-snapshots-lambda.py。
我想创建快照并使用 EBS 卷的相同标签“名称”设置标签“名称”,但在 boto 3 的文档中找不到如何获取 EBS 卷的标签值。
【问题讨论】:
以下代码显示了如何在 boto3 中查找与卷关联的 Name 标记的示例。
import boto3
ec2 = boto3.resource('ec2')
vol = ec2.Volume(id='vol-1234567890123456')
name = None
for tag in vol.tags:
if tag['Key'] == 'Name':
name = tag.get('Value')
如果卷有与之关联的Name 标签,则变量name 将在循环后包含该标签值。然后,您可以使用该值通过create_tags 在快照上创建标签。
【讨论】: