简体   繁体   English

使用 CloudFormation 引用资源中的标签

[英]Referencing tags in resources with CloudFormation

How can I reference a common set of tags in cloudFormation without declaring tags in each resource if possible?如果可能,如何在不声明每个资源中的标签的情况下引用 cloudFormation 中的一组通用标签?

For example, with Terraform you can create a locals.tf file and have a block for tags to be applied across your deployed resources common_tags .例如,使用 Terraform 您可以创建一个locals.tf文件,并为要在部署的资源中应用的标签提供一个块common_tags Example:例子:

locals {
  common_tags = {
    project = "asigra_errors_processing"
    environment = "dev"
    author = "zimcanit"
    }
}

By referencing the tags with tags = local.common_tags , I could assign tags to an s3 bucket as shown below, with just one line of code:通过使用tags = local.common_tags引用标签,我可以将标签分配给 s3 存储桶,如下所示,只需一行代码:

resource "aws_s3_bucket" "asigra_s3" {
    bucket = "logged-asigra-errors-2022"
    region = var.aws_region
    tags = local.common_tags
}

Is it possible to do the above, but with CloudFormation?是否可以使用 CloudFormation 执行上述操作?

I resolved it by using the cli and update-stack command with the --tags option to push tags written in a tags.json file stored locally to my cloudFormation stack in AWS.我通过使用带有 --tags 选项的 cli 和 update-stack 命令来解决它,以将在 tags.json 文件中写入的标签推送到我在 AWS 中的 cloudFormation 堆栈中。

aws cloudformation update-stack --stack-name infrastructure --template-body  file://<insert filepath to cloudformation template file here> --tags file://<insert filepath to json file containing tags to be propogated across deployed resources>

Reference AWS documentation: https://docs.aws.amazon.com/cli/latest/reference/cloudformation/update-stack.html#update-stack参考 AWS 文档: https://docs.aws.amazon.com/cli/latest/reference/cloudformation/update-stack.html#update-stack

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

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