简体   繁体   English

Boto3 python 脚本 AMI 备份 create_image function 不接受 TagSpecifications

[英]Boto3 python script AMI backup create_image function doesn't accept TagSpecifications

The script I have created is working and it creates AMI backups of instances that uses certain Tags.我创建的脚本正在运行,它会创建使用某些标签的实例的 AMI 备份。 (it works except when I add the TagSpecifications part) My biggest issue now is that the Snapshots that are created for the AMI doesn't have any Tag name at all. (除了我添加TagSpecifications部分外它有效)我现在最大的问题是为 AMI 创建的快照根本没有任何标签名称。

The error that I get is:我得到的错误是:

Unknown parameter in input: "TagSpecifications", must be one of: BlockDeviceMappings, Description, DryRun, InstanceId, Name, NoReboot输入中的未知参数:“TagSpecifications”,必须是以下之一:BlockDeviceMappings、Description、DryRun、InstanceId、Name、NoReboot

It appears in the documentation but it is driving me crazy: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_image它出现在文档中,但它让我发疯: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_image

Any idea what it could be?知道它可能是什么吗?

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'
                    },
                ]
            },
        ]
    )

create_image does not take InstanceId parameter since your instance is already Instance object. create_image不采用InstanceId参数,因为您的instance已经是Instance object。

Also for AMI, you need image , not snapshot :同样对于 AMI,您需要image ,而不是snapshot

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

The command works .该命令有效 I verified it on my EC2 instance.我在我的 EC2 实例上验证了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM