【问题标题】:Boto3 python script AMI backup create_image function doesn't accept TagSpecificationsBoto3 python 脚本 AMI 备份 create_image 函数不接受 TagSpecifications
【发布时间】:2021-03-23 10:13:26
【问题描述】:

我创建的脚本正在运行,它会创建使用某些标签的实例的 AMI 备份。 (除非我添加 TagSpecifications 部分,否则它有效) 我现在最大的问题是为 AMI 创建的快照根本没有任何标签名称。

我得到的错误是:

输入中的未知参数:“TagSpecifications”,必须是以下之一:BlockDeviceMappings、Description、DryRun、InstanceId、Name、NoReboot

它出现在文档中,但让我抓狂:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_image

知道它可能是什么吗?

import boto3
import datetime

today = datetime.date.today()

session = boto3.Session(region_name="us-east-2")
ec2 = session.resource('ec2')

instances = ec2.instances.filter(
    Filters=[{'Name': 'tag:Backup', 'Values': ['Yes']}])


for instance in instances:
    instance_name = ''
    for tag in instance.tags:
        if tag["Key"] == 'Name':
            instance_name = tag["Value"]
    print(instance.id, "{0}-{1}".format(instance_name, today.strftime("%Y-%m-%d")))
    instance.create_image(
        Name="{0}-{1}".format(instance_name, today.strftime("%Y-%m-%d")),
        InstanceId=instance.id,
        Description='Quarterly-Backup-AMI',
        NoReboot=True,
        TagSpecifications=[
            {
                'ResourceType': 'snapshot',
                'Tags': [
                    {
                        'Key': 'Name',
                        'Value': 'Testing123123'
                    },
                ]
            },
        ]
    )

【问题讨论】:

    标签: python amazon-ec2 boto3 amazon-ami


    【解决方案1】:

    create_image 不采用InstanceId 参数,因为您的instance 已经是Instance 对象。

    对于 AMI,您需要 image,而不是 snapshot

        instance.create_image(
            Name="dddd",
            Description='Quarterly-Backup-AMI',
            NoReboot=True,
            TagSpecifications=[
                {
                    'ResourceType': 'image',
                    'Tags': [
                        {
                            'Key': 'Name',
                            'Value': 'Testing123123'
                        },
                    ]
                },
            ]
        )
    

    命令有效。我在我的 EC2 实例上验证了它。

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多