Justin
Justin

Reputation: 45320

Merging maps with variables

Running Terraform v0.11.3 and I am trying to merge two maps into a single map using the merge() function. However I can't get the syntax right. Does merge() support using dynamic variables?

  tags = "${merge({
    Name         = "${var.name}"
    Env          = "${var.environment}"
    AutoSnapshot = "${var.auto_snapshot}"
  }, "${var.tags}")}"

Upvotes: 14

Views: 29177

Answers (2)

Costas
Costas

Reputation: 535

In Terraform > 0.12 this can be done as:

tags = merge(tomap({
    Name = var.name,
    Env  = var.environment,
    AutoSnapshot = var.auto_snapshot }),
    var.tags,
)

Upvotes: 23

Marcin
Marcin

Reputation: 238071

The syntax for merge in TF 0.11 is shown here:

${merge(map("a", "b"), map("c", "d"))} 

So in your case, you should have something as follows:

tags = "${merge(map("Name", var.name,
                    "Env", var.environment,
                    "AutoSnapshot", var.auto_snapshot
               ), var.tags)}"

Upvotes: 9

Related Questions