Terraform: Terraform interpolation throwing error when using terraform.workspace option

Created on 24 Jul 2018  ยท  3Comments  ยท  Source: hashicorp/terraform

Hi there,

so I am trying to do interpolation i-e with public scheme and terraform.workspace so subnets will dynamically get selected. For that tried merging terraform.workspace with elb_subnets but its throwing error that only supported key for terraform.Xinterpolations is workspace

Terraform Version v0.11.7

Code

variable "elb_scheme" {
  default = "public"
}

variable "prod_elb_subnets" {
  type = "map"

  default = {
    public  = "subnet-23ywe324, subnet-234hj34, subnet-cdh7868"
    private = "subnet-hj3h2323, subnet-jihi782, subnet-237dew"
  }
}

variable "qa_elb_subnets" {
  type = "map"

  default = {
    public  = "subnet-234ee234, subnet-da238sdf, subnet-sd2233"
    private = "subnet-09jsdf23, subnet-hi232rf, subnet-89832w32"
  }
}

  setting {
    namespace = "aws:ec2:vpc"
    name      = "ELBSubnets"

    value = "${var.(terraform.workspace_elb_subnets["${var.elb_scheme}"])}"
  }

Crash Output

Error: module.ebs.aws_elastic_beanstalk_environment.beanstalk: 1 error(s) occurred:

* module.ebs.aws_elastic_beanstalk_environment.beanstalk: terraform.workspace_elb_subnets: only supported key for 'terraform.X' interpolations is 'workspace'
question

Most helpful comment

Hi @ahsannaseem,

Terraform is complaining here because you used the expression terraform.workspace_elb_subnets, and workspace_elb_subnets is not a valid attribute of the terraform object.

It looks like you are trying to dynamically generate a variable name, which is not possible in Terraform. Instead, you'll need to create a map value whose keys include the workspace names and access that map. For example:

variable "elb_scheme" {
  default = "public"
}

locals {
  subnets = {
    "prod_public" = "subnet-23ywe324, subnet-234hj34, subnet-cdh7868"
    "prod_private" = "subnet-hj3h2323, subnet-jihi782, subnet-237dew"
    "qa_public" = "subnet-234ee234, subnet-da238sdf, subnet-sd2233"
    "qa_private" = "subnet-09jsdf23, subnet-hi232rf, subnet-89832w32"
  }
}

resource "aws_elastic_beanstalk_environment" "example" {
  # ...

  setting {
    namespace = "aws:ec2:vpc"
    name      = "ELBSubnets"

    value = "${local.subnets["${terraform.workspace}_${var.elb_scheme}"]}"
  }
}

As @mildwonkey said, we can't provide general help here on GitHub since this is focused on tracking bugs and enhancements in Terraform. If you have further questions about the above, please ask in one of the community forums where there are more people able to help with usage questions.

All 3 comments

Hi @ahsannaseem!

Please note that we use GitHub issues for tracking bugs and enhancements rather than for questions. While we may be able to help with certain simple problems here it's generally better to use one of the community forums where there are far more people ready to help, whereas the GitHub issues here are generally monitored only by our few core maintainers.

Having said that, I think this hint might get you pointed in the right direction:

var.{terraform.workspace}_elb_subnets

Hopefully this is enough to get you started. Thanks!

Hi @ahsannaseem,

Terraform is complaining here because you used the expression terraform.workspace_elb_subnets, and workspace_elb_subnets is not a valid attribute of the terraform object.

It looks like you are trying to dynamically generate a variable name, which is not possible in Terraform. Instead, you'll need to create a map value whose keys include the workspace names and access that map. For example:

variable "elb_scheme" {
  default = "public"
}

locals {
  subnets = {
    "prod_public" = "subnet-23ywe324, subnet-234hj34, subnet-cdh7868"
    "prod_private" = "subnet-hj3h2323, subnet-jihi782, subnet-237dew"
    "qa_public" = "subnet-234ee234, subnet-da238sdf, subnet-sd2233"
    "qa_private" = "subnet-09jsdf23, subnet-hi232rf, subnet-89832w32"
  }
}

resource "aws_elastic_beanstalk_environment" "example" {
  # ...

  setting {
    namespace = "aws:ec2:vpc"
    name      = "ELBSubnets"

    value = "${local.subnets["${terraform.workspace}_${var.elb_scheme}"]}"
  }
}

As @mildwonkey said, we can't provide general help here on GitHub since this is focused on tracking bugs and enhancements in Terraform. If you have further questions about the above, please ask in one of the community forums where there are more people able to help with usage questions.

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