【问题标题】:How to remove property in terraform override of resource.如何在资源的 terraform 覆盖中删除属性。
【发布时间】:2017-04-21 17:53:51
【问题描述】:

我有一个 terraform 资源,如下所示

resource "aws_instance" "web" {
    ami = "ami-408c7f28"
    tags = { Name = "hello World"}
}

我想覆盖它以删除标签并让它看起来像这样

resource "aws_instance" "web" {
    ami = "ami-408c7f28"
}

基本上去除标签。

有没有办法在此处描述的覆盖文件中执行此操作? https://www.terraform.io/docs/providers/aws/r/instance.html

以上是一个例子。一般来说,我真的很想知道是否可以删除覆盖中的属性。

【问题讨论】:

    标签: terraform


    【解决方案1】:

    是的,当您从资源中删除属性时,Terraform 应该能够。例如,假设我已经使用以下 .tf 文件运行 terraform apply

    resource "aws_instance" "web" {
      ami = "ami-408c7f28"
      instance_type = "m1.small"
      tags = { Name = "hello World"}
    }
    

    现在,如果我将 .tf 文件更改为:

    resource "aws_instance" "web" {
      ami = "ami-408c7f28"
      instance_type = "m1.small"
    }
    

    然后运行terraform plan,我应该会看到这样的输出:

    ~ aws_instance.web
        tags.%:    "1" => "0"
        tags.Name: "hello World" => ""
    

    这表示 terraform 想要通过移除 Name 标签来修改实例。如果我运行terraform apply,标签将被删除。

    如果您想删除override file 中的标签(例如,override.tf),您可以将标签显式设置为空映射:

    resource "aws_instance" "web" {
      ami = "ami-408c7f28"
      instance_type = "m1.small"
      tags = {}
    }
    

    请注意,仅当您在 us-east-1 中的帐户仍具有 EC2-Classic 支持时,这些特定示例才有效。

    【讨论】:

    • 当我第一次阅读您的问题时,我错过了您询问使用override files,特别是(我认为您粘贴了错误的链接)。您仍然可以使用覆盖来执行此操作,但您需要明确清除 tags 属性。例如,在 override.tf 中,您可以指定 tags = {} 以获得相同的效果。
    • 我认为我的用例需要更加精确。这在技术上回答了我的问题,但没有解决我的问题。赞一个。
    猜你喜欢
    • 1970-01-01
    • 2021-05-06
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2022-01-07
    • 2020-02-03
    • 2019-11-14
    相关资源
    最近更新 更多