Terraform: Resource addressing when specifiying [N] does not work

Created on 22 Feb 2017  ยท  3Comments  ยท  Source: hashicorp/terraform

https://www.terraform.io/docs/internals/resource-addressing.html

According to the docs, I should be able to target a resource using [N] to select a specific instance. This does not work.

Terraform Version

Terraform v0.8.7

Affected Resource(s)

  • aws_instance

Terraform Configuration Files

resource "aws_instance" "ci-swarm-manager" {
  count         = "${var.ci_swarm_managers}"
  ami           = "${var.ci_swarm_ami_id}"
  instance_type = "${var.ci_instance_type}"

  tags {
    Name = "ci-swarm-manager"
  }

  vpc_security_group_ids = [
    "${aws_security_group.docker_ci.id}",
  ]

  key_name = "rbci"

  connection {
    user        = "ubuntu"
    private_key = "${file("rbci.pem")}"
  }

  provisioner "remote-exec" {
    inline = [
      "if ${var.swarm_init}; then docker swarm init --advertise-addr ${self.private_ip}; fi",
      "if ! ${var.swarm_init}; then docker swarm join --token ${var.ci_swarm_manager_token} --advertise-addr ${self.private_ip} ${var.ci_swarm_manager_ip}:2377; fi",
      "if ${var.rexray}; then echo \"${data.template_file.rexray.rendered}\" | sudo tee /etc/rexray/config.yml; fi",
      "if ${var.rexray}; then sudo rexray service start >/dev/null 2>/dev/null; fi",
      "if ${var.rexray}; then sudo chown -R ubuntu /var/run/rexray/; fi",
    ]
  }
}

Expected Behavior

Able to target a node using [N] as specified in the documents

Actual Behavior

Error.

Steps to Reproduce

ci master โœ— 4h43m โ–ณ โžœ terraform plan -target aws_instance.ci-swarm-manager      

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.

aws_security_group.docker_ci: Refreshing state... (ID: sg-2535445d)
data.template_file.rexray: Refreshing state...
aws_instance.ci-swarm-manager.0: Refreshing state... (ID: i-0498d51853c6ba1db)
aws_instance.ci-swarm-manager.1: Refreshing state... (ID: i-093440d92ad29a943)

No changes. Infrastructure is up-to-date. This means that Terraform
could not detect any differences between your configuration and
the real physical resources that exist. As a result, Terraform
doesn't need to do anything.

ci master โœ— 4h44m โ–ณ โžœ terraform plan -target aws_instance.ci-swarm-manager[0]
zsh: no matches found: aws_instance.ci-swarm-manager[0]

ci master โœ— 4h44m โ–ณ โ‰ โžœ terraform plan -target aws_instance.ci-swarm-manager.[0]
zsh: no matches found: aws_instance.ci-swarm-manager.[0]

ci master โœ— 4h44m โ–ณ โ‰ โžœ terraform plan -target aws_instance.ci-swarm-manager.0  
Error configuring: Unexpected value for InstanceType field: "0"

ci master โœ— 4h44m โ–ณ โ‰ โžœ terraform plan -target aws_instance.ci-swarm-manager.1
Error configuring: Unexpected value for InstanceType field: "1"

ci master โœ— 4h44m โ–ณ โ‰ โžœ terraform plan -target aws_instance.ci-swarm-manager[1] 
zsh: no matches found: aws_instance.ci-swarm-manager[1]

ci master โœ— 4h44m โ–ณ โ‰ โžœ terraform plan -target aws_instance.ci-swarm-manager1  
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.


No changes. Infrastructure is up-to-date. This means that Terraform
could not detect any differences between your configuration and
the real physical resources that exist. As a result, Terraform
doesn't need to do anything.
bug core

Most helpful comment

Hey there! I took a look and this looks like the error is coming from zsh and not Terraform. Gave it a try and if you wrap the address in single quotes then this works (your first attempt, but in single quotes).

In bash this works without quotes at all. Hard to appease every shell!

All 3 comments

Hey there! I took a look and this looks like the error is coming from zsh and not Terraform. Gave it a try and if you wrap the address in single quotes then this works (your first attempt, but in single quotes).

In bash this works without quotes at all. Hard to appease every shell!

@mitchellh awesome good to know - thanks for looking in to this!

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