【问题标题】:AWS - Create AMI Using EC2 Instance Tags To Name The AMIAWS - 使用 EC2 实例标签创建 AMI 来命名 AMI
【发布时间】:2017-11-11 12:17:56
【问题描述】:

我正在尝试做一些我认为很简单的事情,但我不是 Python 专家,所以我需要你的帮助,让我向你展示我正在尝试做的事情:

我有这部分代码,我已经找到并且运行良好,但我正在尝试对其进行一些更改:

AMIid = ec.create_image(InstanceId=instance['InstanceId'], Name="BKP - " + **instance['InstanceId']** + " from " + create_fmt, Description="Lambda created AMI of instance " + instance['InstanceId'] + " from " + create_fmt, NoReboot=True, DryRun=False)

我正在尝试将我的 EC2 实例中的标签“名称”添加到正在创建的新 AMI 的“名称”字段中,如下所示:

AMIid = ec.create_image(InstanceId=instance['InstanceId'], Name="BKP - " + **Tags=[Key:"Name",'Value']** + " from " + create_fmt, Description="Lambda created AMI of instance " + instance['InstanceId'] + " from " + create_fmt, NoReboot=True, DryRun=False)

关于如何做到这一点的任何想法?

编辑:我已经找到了解决方案,也许这不是最好的方法,但有效,按照我到目前为止所做的:

创建了一个变量来获取 EC2 标签名称:

nameTag = "".join([str(t.get('Value')) for t in instance['Tags'] if t['Key'] == 'Name'])

然后,在代码中包含“nameTag”变量:

AMIid = ec.create_image(InstanceId=instance['InstanceId'], Name="BKP - " + **nameTag** + " from " + create_fmt, Description="Lambda created AMI of instance " + instance['InstanceId'] + " from " + create_fmt, NoReboot=True, DryRun=False)

希望这可以帮助更多需要这样的人!

谢谢!

【问题讨论】:

    标签: amazon-web-services amazon-ami


    【解决方案1】:

    不确定这是否有用,因为这已经是 2 年的帖子了,但尽管如此

    details = ec.describe_instances(InstanceIds=[instance['InstanceId']])
                for tags in details['Reservations'][0]['Instances'][0]['Tags']:
                    if tags['Key'] == "Name":
                        instance['Name'] = tags['Value']
    
                print("Instance Name:" + instance['Name'])
    

    上面的脚本会给你实例的名字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-02
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多