简体   繁体   English

aws cli cloudformation update-stack 设置标签?

[英]aws cli cloudformation update-stack set tags?

I need to update the tags for a AWS cloudformation stack only.我只需要更新 AWS cloudformation 堆栈的标签。

Unfortunately you cannot do:不幸的是你不能这样做:

aws cloudformation update-stack --tags <tags>

Does anyone know the best process for this?有谁知道这方面的最佳流程?

If i do:如果我做:

aws cloudformation update-stack --use-previous-template --template-body <JSON blob>

Will the template body be added to the previous template, overwriting previous values?模板主体是否会添加到之前的模板中,覆盖之前的值?

Is it possible to download the live cloudformation JSON?是否可以下载实时 cloudformation JSON?

Not sure about the updating only the tags via cli. 不确定仅通过cli更新标签。 However its very easy to do this in the aws console. 但是,在aws控制台中非常容易做到这一点。

updating the stack by providing a new JSON template will override the previous template. 通过提供新的JSON模板更新堆栈将覆盖先前的模板。

you can download the template by using the dashboard (CFN->template tab) or by using the cli command get-template 您可以使用仪表板(CFN->模板选项卡)或使用cli命令get-template下载模板

Here is the way I did it:这是我做的方式:

You can update the tags only by get the information from the api and piping back into your update_stack request.您只能通过从 api 获取信息并将管道返回到您的 update_stack 请求来更新标签。 Right before you update the stack, you append the the tags list/array with the new dict/object you want.在更新堆栈之前,您将标签列表/数组附加到您想要的新字典/对象。

Here is an example using python.这是一个使用 python 的示例。 As long as you know the tags you want to add, this will update your stack, keep everything the same, but just update your tags list/array with added tags.只要您知道要添加的标签,这将更新您的堆栈,保持一切不变,但只需使用添加的标签更新您的标签列表/数组。

import boto3
import botocore.errorfactory
client = boto3.client("cloudformation")
response = client.describe_stacks(StackName="arn:aws:cloudformation:us-east-1:012345678901:stack/<stackname>/asdf1234-as12-df34-gh56-qwerty012345")
responsetemplate = client.get_template(StackName="arn:aws:cloudformation:us-east-1:012345678901:stack/<stackname>/asdf1234-as12-df34-gh56-qwerty012345")
response["Stacks"][0]["TemplateBody"] = responsetemplate["TemplateBody"]
response["Stacks"][0].pop("StackId", None)
response["Stacks"][0].pop("Description", None)
response["Stacks"][0].pop("CreationTime", None)
response["Stacks"][0].pop("StackStatus", None)
response["Stacks"][0].pop("EnableTerminationProtection", None)
response["Stacks"][0].pop("DriftInformation", None)
if "LastUpdatedTime" in response["Stacks"][0]:
    response["Stacks"][0].pop("LastUpdatedTime", None)

Tags={
            'Key': 'Namefirstest',
            'Value': 'somevalue'
        }
response["Stacks"][0]["Tags"].append(Tags)
# print(response["Stacks"][0].keys())


update = client.update_stack(**response["Stacks"][0])
print(update)

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

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