Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_instance tags don't get created or updated #6265

Closed
yanatan16 opened this issue Apr 20, 2016 · 10 comments
Closed

aws_instance tags don't get created or updated #6265

yanatan16 opened this issue Apr 20, 2016 · 10 comments

Comments

@yanatan16
Copy link

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
@jen20
Copy link
Contributor

jen20 commented Apr 20, 2016

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.

@yanatan16
Copy link
Author

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}"
  }

@yanatan16
Copy link
Author

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?

@catsby catsby added core and removed provider/aws labels Apr 20, 2016
@jen20
Copy link
Contributor

jen20 commented Apr 25, 2016

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.

@jen20
Copy link
Contributor

jen20 commented Apr 25, 2016

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.

@ap-dji
Copy link

ap-dji commented Jun 3, 2016

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

@george-angel
Copy link

george-angel commented Jul 21, 2016

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

@mitchellh
Copy link
Contributor

This is fixed with 0.7.2!

@mitchellh
Copy link
Contributor

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

@ghost
Copy link

ghost commented Apr 22, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants