I need to update the tags for a AWS cloudformation stack only.
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?
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. 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. 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)