Terraform: Cannot assign to map type variable from CLI

Created on 3 Jun 2015  ·  5Comments  ·  Source: hashicorp/terraform

I'm trying to set a map type variable aws_amis (as in the multi-tier AWS example) from the CLI with the -var flag but I keep getting "aws_amis: cannot assign string value to map type". Is assigning to map variables from the CLI supported?

question

Most helpful comment

Hi @dramdass - you can assign fields in a mapping variable using dot notation. The default is important to establish the variable as a mapping type.

Here's an example:

variable "mapme" {
  default = {
    "hello" = "world"
  }
}

output "out" {
  value = "${lookup(var.mapme, "hello")}"
}
❯ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  out = world

❯ terraform apply -var mapme.hello=planet

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  out = planet

All 5 comments

I don't believe it is. Although you define them using variable, they are most like constants. Look here http://terraform.io/intro/getting-started/variables.html under the Mappings section.

Hi @dramdass - you can assign fields in a mapping variable using dot notation. The default is important to establish the variable as a mapping type.

Here's an example:

variable "mapme" {
  default = {
    "hello" = "world"
  }
}

output "out" {
  value = "${lookup(var.mapme, "hello")}"
}
❯ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  out = world

❯ terraform apply -var mapme.hello=planet

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  out = planet

Ah, that's good to know.

can someone verify that terraform apply -var mapme.hello=planet actually works?
it doesn't work for me"

$ terraform apply -var mapme.hello=planet 
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
out = world

The only thing works for me is that I have to override the entire map, which is not really desirable - it drops all the other values in the map :
```
terraform apply -var mapme='{"hello"="planet"}'
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
out = planet

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