hashicorp / terraform-provider-terraform

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

Terraform accepts multiple variables named `environment` #10

Open hashibot opened 6 years ago

hashibot commented 6 years ago

This issue was originally opened by @kitforbes as hashicorp/terraform#16029. It was migrated here as a result of the provider split. The original body of the issue is below.


Terraform Version

Terraform v0.10.3

Terraform Configuration Files

Creating the object in Output of a state:

output "environment" {
  value = "${map(
    "name", "Trevor",
    "code", "42",
  )}"
}

Consuming the object in another state:

data "terraform_remote_state" "network" {
  backend = "s3"

  config {
    region = "${var.remote_state["bucket_region"]}"
    bucket = "${var.remote_state["bucket_name"]}"
    key    = "${var.environment}/${var.region}/network.tfstate"
  }
}

# Spit out the value to check your sanity
output "test" {
  value = "${data.terraform_remote_state.network.environment}"
}

Expected Behavior

The map should be displayed in the console as

test = {
  name = Trevor
  code = 42
}

Actual Behavior

test = default

Steps to Reproduce

  1. Create a Terraform state file with the above output
  2. Reference the terraform_remote_state in another Terraform configuration
  3. Try to access the data.terraform_remote_state.network.environment as a map

Important Factoids

After investigating this, it turns out that Terraform includes an variable called environment. There is no warning that you can not create an additional variable of the same name.

From the remote state file:

"resources": {
    "data.terraform_remote_state.network": {
        "type": "terraform_remote_state",
        "depends_on": [],
        "primary": {
            "id": "2017-09-06 13:24:30.9196337 +0000 UTC",
            "attributes": {
                "environment": "default",
                "environment.%": "2",
                "environment.name": "Trevor",
                "environment.code": "42"

Attempting the same with user defined outputs of the same name results in an error.

output "fruit" {
  value = "Apple"
}

output "fruit" {
  value = "${list("Apple", "Pair")}"
}

Error getting plugins: module root: 1 error(s) occurred:

  • fruit: duplicate output. output names must be unique.