hashicorp / terraform-provider-terraform

Terraform terraform provider
https://www.terraform.io/docs/providers/terraform/
Mozilla Public License 2.0
24 stars 22 forks source link

Cannot use 'id' as an output variable when importing it using terraform_remote_state #2

Open hashibot opened 7 years ago

hashibot commented 7 years ago

This issue was originally opened by @jflammia as hashicorp/terraform#15085. It was migrated here as part of the provider split. The original body of the issue is below.


When a module or resource has an output variable of id, it cannot be imported using data.terraform_remote_state. data.terraform_remote_state will pull in the ID of the remote state itself (which happens to be a timestamp) instead of the resource's output variable id.

My work-around was to rename my module's output variable to something other than id. id seems to be a reserved word, so I don't know if this is a simple omission from documentation or this a valid action that has a bug in it.

Terraform Version

Terraform v0.9.6

Affected Resource(s)

data.terraform_remote_state

Expected Behavior

The output variable id's value should be used.

Actual Behavior

The remote state's id value is used.

Steps to Reproduce

  1. Create a resource with an output variable named id
    output "id" {
    value = "${aws_security_group.new_sg.id}"
    }
  2. Import the state using terraform_remote_state

    data "terraform_remote_state" "security_group_new" {
    backend     = "s3"
    environment = "${terraform.env}"
    
    config {
    bucket     = "example-terraform-remote-state"
    key        = "utilities/security_groups/new.tfstate"
    region     = "us-east-1"
    lock_table = "example-terraform-remote-state"
    }
    }
  3. Reference the output variable id in another resource

    module "test_mod" {
    source = "hg::ssh://hg@bitbucket.org/example/example-terraform-modules//aws/ec2/instances/test_mod"
    
    security_group_id = "${data.terraform_remote_state.security_group_new.id}"
    }
  4. During terraform plan, the value of security_group_id is a timestamp (similar to 2017-06-05 15:15:55.60991551 +0000 UTC)
    
    ~ module.test_mod.aws_instance.test_host
    vpc_security_group_ids.#:          "1" => "2"
    vpc_security_group_ids.1485290492: "sg-111111111" => "sg-111111111"
    vpc_security_group_ids.2513576933: "" => "2017-06-05 15:15:55.60991551 +0000 UTC"

Plan: 0 to add, 1 to change, 0 to destroy.



### Important Factoids
If the remote state file is visually inspected, you can see that the timestamp that is being pulled in for the `id` field is the `id` of the remote state, not the output variable `id`.  Renaming the output variable to something other than `id` is the work-around for this issue.
Theragus commented 6 years ago

Thanks for this article. I was pretty confused when "id" always returned a timestamp.

Are there plans to fix this in future realeases? Or at least bring up an warning message if you use id in remote state?