如何删除资源的terraform覆盖中的属性。

时间:2016-12-06 22:54:33

标签: terraform

我有一个类似于以下

的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

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

1 个答案:

答案 0 :(得分:2)

是的,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支持时,这些具体示例才有效。

相关问题