2
votes

I have a terraform resource that looks like the following

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

I want to override it to remove the tags and have it look like this

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

Basically removing the tags.

Is there a way to do this in an override file as described here? https://www.terraform.io/docs/providers/aws/r/instance.html

The above is an example. In general I really want to know if I can remove a property in an override.

1

1 Answers

2
votes

Yes, Terraform should be able to when you remove an attribute from your resource. For example, assume I had already run terraform apply with the following .tf file:

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

Now, if I change the .tf file to:

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

and run terraform plan, I should see output like this:

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

This indicates that terraform wants to modify the instance by removing the Name tag. If I run terraform apply, the tag will be removed.

If you want to remove the tag in an override file (override.tf, for example), you would explicitly set tags to an empty map:

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

Note these specific examples will only work if your account in us-east-1 still has EC2-Classic support.