Variable keys on terrafort cards

In Terraform, I am trying to create a module that includes a map with variable keys. I'm not sure if this is possible, but I tried the following unsuccessfully.

resource "aws_instance" "web" {
    ami = "${var.base_ami}"
    availability_zone = "${var.region_a}"
    instance_type = "${var.ec2_instance_size}"
    security_groups = ["sec1"]
    count = "${var.ec2_instance_count}"
    tags {
        Name = "${var.role} ${var_env}"
        role = "${var.app_role}"
        ${var.app_role} = "${var_env}"
    }
}

and this:

tags {
   Name = "${var.role} ${var_env}"
}
tags."${var.role}" = "${var.env}"

Any ideas? Is it not possible with Terraform now?

+11
source share
6 answers

I know that a lot of time has passed since you sent the message, but recently I needed something similar, so I am posting the solution here, in case anyone else stumbles upon it.

Terraform interpolation syntax now supports a function lookupthat allows you to search for dynamic keys on a map.

Using this, I can now do something like:

output "image_bucket_name" {
  value = "${lookup(var.image_bucket_names, var.environment, "No way this should happen")}"
}

Where:

variable "image_bucket_names" {
  type = "map"

  default = {
    development = "bucket-dev"
    staging = "bucket-for-staging"
    preprod = "bucket-name-for-preprod"
    production = "bucket-for-production"
  }

}

environment - .

+22

terraform 0.11.7. .

resource "aws_instance" "web" {
  ...
  tags = "${map(
    "Name", "${var.role} ${var_env}",
    "role", "${var.app_role}",
    "${var.app_role}", "${var_env}"
  )}"
}
+6

, zipmap:

locals {
  ec2_tag_keys = ["some/prefix/${var.some_var}", "another_tag"]
  ec2_tag_vals = ["some value", "another value"]
}

resource "aws_instance", "foo" {
  ...
  tags = "${zipmap(local.ec2_tag_keys, local.ec2_tag_vals)}"
}

, .

+3

.

+1

, , , , 0.11.7, Terraform . , AWS:

.tf:

variable "environment" {}

...

variable "instance_types_webserver" {
  type = "map"

  default = {
    testing    = "t2.small"
    qa         = "t2.medium"
    staging    = "t2.xlarge"
    production = "t2.xlarge"
  }
}

...

resource "aws_opsworks_instance" "verification" {
  stack_id      = "${aws_opsworks_stack.verification.id}"
  layer_ids     = ["${aws_opsworks_custom_layer.verification.id}"]
  instance_type = "${var.instance_types_webserver["${var.environment}"]}"
  state         = "running"
  count         = 1
}

.tfvars:

...
environment = "testing"
...
+1

, . HCL2 (0.12) :

resource "aws_instance" "web" {
  count = "${var.ec2_instance_count}"

  ami = "${var.base_ami}"
  availability_zone = "${var.region_a}"
  instance_type = "${var.ec2_instance_size}"
  security_groups = ["sec1"]

  tags = {
    Name              = "${var.role} ${var.env}"
    role              = "${var.app_role}"
    "${var.app_role}" = "${var.env}"               # <------ like this
  }
}

# 21566 , "${var.app_role}" (var.app_role), , .

( , , : var.app_role , .)

, . HCL2 (0.12) :

+ merge

, merge :

variable "app_role" {
  type = string
}

locals {
  tags = merge(
    {
      Name = "${var.role} ${var.env}"
      role = "${var.app_role}"
    },
    {
      for k in [var.app_role]: k => "${var.env}"
    }
  )
}

zipmap

Alternatively, you can use zipmapto create it in one go:

locals {
  tags = zipmap(
    [
       "Name",
       "role",
       var.app_role
    ],
    [
       "${var.role} ${var.env}",
       var.app_role,
       var.env
    ]
  )
}

Then you can use this map in the resource:

resource "aws_instance" "web" {
  count = "${var.ec2_instance_count}"

  ami = "${var.base_ami}"
  availability_zone = "${var.region_a}"
  instance_type = "${var.ec2_instance_size}"
  security_groups = ["sec1"]

  tags = local.tags // or inline the above here
}

One caveat: if it var.app_roleis equal to either "Name", or "role", it will overwrite your static value. You can avoid this by swapping the arguments in mergeor reordering the lists in zipmap, although such a collision is likely to be a configuration error that must be detected & corrected before application.

+1
source

Source: https://habr.com/ru/post/1629451/


All Articles