Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map within a map in terraform variables

Does anyone know if it's possible with possibly code snipits representing whether I can create a map variable within a map variable in terraform variables?

variable "var" {
  type = map
  default = {
    firstchoice = {
      firstAChoice ="foo"
      firstBChoice = "bar"
    }
    secondchoice = {
      secondAChoice = "foobar"
      secondBChoice = "barfoo"
    }
  }
}

If anyone has any insight to whether this is possible or any documentation that elaborates that would be great.

like image 400
IsaacD Avatar asked Jun 26 '19 15:06

IsaacD


People also ask

Can Terraform variables reference other variables?

The values can be hard-coded or be a reference to another variable or resource. Local variables are accessible within the module/configuration where they are declared.

What is map variable Terraform?

terraform-map-variable.tfA variable can have a map type assigned explicitly, or it can be implicitly declared as a map by specifying a default value that is a map. The above demonstrates both. Then, replace the aws_instance with the following: resource "aws_instance" "example" { ami = var.

How do you use Tomap in Terraform?

tomap converts its argument to a map value. Explicit type conversions are rarely necessary in Terraform because it will convert types automatically where required. Use the explicit type conversion functions only to normalize types returned in module outputs.


1 Answers

Yes, it's possible to have map variable as value of map variable key. Your variable just needed right indentation. Also I am putting ways to access that variable.

variable "var" {
  default = {
    firstchoice = {
      firstAChoice = "foo"
      firstBChoice = "bar"
    }

    secondchoice = {
      secondAChoice = "foobar"
      secondBChoice = "barfoo"
    }
  }
}

To access entire map value of a map key firstchoice, you can try following

value = "${var.var["firstchoice"]}"

output:
{
  firstAChoice = foo
  firstBChoice = bar
}

To access specific key of that map key (example firstAChoice), you can try

value = "${lookup(var.var["firstchoice"],"firstAChoice")}"

output: foo
like image 60
Avichal Badaya Avatar answered Oct 13 '22 23:10

Avichal Badaya