Is there a way to lookup list variables from a map variable?


Is there anyway to lookup a list variable from a map or achieve the same end in another programmatic way?

I am building AWS ec2 instances with ‘vcp_security_groups_ids’ which will only except lists.

I have two list vars and want to select either of the whole lists based on a parameter. the below gives an idea of what I am trying to achieve but I can’t use lists in a map either as a var or entered as a list of values.

variable “sg1” {
default = [“sg-12345”,“sg-12346”]

variable “sg2” {
default = [“sg-22345”,“sg-22346”]

variable “sg” {
1 = “{$var.sg1}”
2 = “{$var.sg1}”

vcp_security_groups_ids = “${lookup(,var.example)}”


Hi @scousins1!

variable blocks are only for values provided directly by the calling module, but you can use locals blocks for named expressions within your module, as a way to derive one value from another:

variable "sg1" {
  type = set(string)

variable "sg2" {
  type = set(string)

variable "example" {
  type = string

locals {
  security_groups = {
    sg1 = var.sg1
    sg2 = var.sg2

resource "aws_instance" "example" {
  # ...

  vpc_security_group_ids = local.security_groups[var.example]