Terraform: aws_instance tags don't get created or updated

Created on 20 Apr 2016  ยท  10Comments  ยท  Source: hashicorp/terraform

Problem

AWS Instance tags never get applied, even after repeated terraform apply. terraform plan always reports that the tags are empty and need to be set:

$ terraform apply
...
$ terraform plan
...
~ module.bootstrap.aws_instance.instance
    tags.#: "" => "<computed>"

~ module.agents.aws_instance.instance.<n>
    tags.#: "" => "<computed>"

~ module.masters.aws_instance.instance.<n>
    tags.#: "" => "<computed>"

Background

Terraform Version: Terraform v0.6.14
OS: OSX

Affected Resources: aws_instance

Configuration

This is the primary module being used with a few different roles:

variable "prefix" {}
variable "subnet_id" {}
variable "role" {}
variable "count" {}
variable "type" {}
variable "storage_size" {}
variable "storage_type" {}
variable "amis" {
  default = {
    "us-west-2" = "ami-4f7f8a2f" # coreOS stable HVM
  }
}
variable "datacenter" {}
variable "region" {}
variable "availability_zone" {}
variable "route53_zone_id" {}
variable "vpc_id" {}
variable "security_group" {}
variable "ssh_key" {}
variable "dns_domain" {}

## Instance name
resource "template_file" "iname" {
  count = "${var.count}"
  template = "${role}${count}"
  vars {
    role = "${var.prefix}${var.role}"
    count = "${format("%02d", count.index)}"
  }
}

## Cloud Config
resource "template_file" "cloud_config" {
  template = "${file("${path.module}/cloud-config.yml")}"
}

## Instance
resource "aws_instance" "instance" {
  count = "${var.count}"
  instance_type = "${var.type}"

  ami = "${lookup(var.amis, var.region)}"
  availability_zone = "${var.region}${var.availability_zone}"
  vpc_security_group_ids = ["${var.security_group}"]
  subnet_id = "${var.subnet_id}"
  key_name = "${var.ssh_key}"
  user_data = "${base64encode(template_file.cloud_config.rendered)}"

  tags {
    Name = "${element(template_file.iname.*.rendered, count.index)}"
    Environment = "${var.prefix}"
    Role = "mesos-${role}"
  }

  root_block_device {
    volume_type = "${var.storage_type}"
    volume_size = "${var.storage_size}"
    delete_on_termination = true
  }
}

Debug Output

https://gist.github.com/yanatan16/b9a8517939c92e262ac2b48d03f48a7a

Expected Behavior

Tags should be applied to instances.

Actual Behavior

No tags exist in EC2 console, terraform plan reports changed tags.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply
  2. terraform plan
bug core

All 10 comments

Hi @yanatan16! Thanks for the detailed report here. This is an interesting bug - tags are tested as part of our acceptance testing suites and are widely used. My first guess reading your config is that issue lies with the interpolation - particularly the element. This _should_ work - but as a potential workaround (and probably clearer code) why not perform the string formatting directly in the tag rather than via a template_file resource? I'll investigate why this doesn't work though.

I understand this is probably a configuration error, being unable to set the "Name" tag would be a pretty big regression.

I edited the tags to use regular interpolation with no function calls (no format), but still no luck. Same output and results.

  tags {
    Name = "${var.prefix}${var.role}${count.index}"
    Environment = "${var.prefix}"
    Role = "mesos-${role}"
  }

Alas @jen20, I have found the bug. "${role}" is an invalid string interpolation (it should be "${var.role}"). Apparently this error is being suppressed in the aws_instance resource. Perhaps this is a different issue of error suppression?

Hi @yanatan16 - yes I'd agree this is a bug. I'm going to test this against the dev-0.7 branch, as I'm fairly sure the HIL interpolator should be complaining about that. Perhaps we are failing to surface an error somewhere.

As an update: I have the following simpler repro on this:

variable "role" {
    default = "testrole"
}

resource "aws_vpc" "test" {
    cidr_block = "10.0.0.0/16"

    tags {
        Role = "test-${role}"
    }
}

My current working theory is that the tags field being computed is somehow ignoring interpolation errors.

@jen20 I just ran into this with a build off master.

Saw the same thing, left out var in the variable and got silent failures.

This is fixed with 0.7.2!

EH, correction: I fixed another var prefix issue in 0.7.2. This appears to still be happening. Centralizing work/feedback on #8010.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings