Variable parseing error Error: Invalid attribute name

I am using terraform workspaces, wanted to do something more complex concerning variable parsing.

I have one line

  external_nat_ip_ids = data.terraform_remote_state.eu-eip-nat.outputs."${lookup(var.workspace_to_region, terraform.workspace )}"-aws_eip_id

As you can see from the variables what I want to do is a lookup base don the workspace I am using to use the correct value. I want the value to form part of the location looked up in the output I have.

However I get the error

Error: Invalid character

  on main.tf line 13, in module "vpc":
  13:   external_nat_ip_ids = data.terraform_remote_state.eu-eip-nat.outputs.${lookup(var.workspace_to_region, terraform.workspace )}-aws_eip_id

This character is not used within the language.


Error: Invalid attribute name

  on main.tf line 13, in module "vpc":
  13:   external_nat_ip_ids = data.terraform_remote_state.eu-eip-nat.outputs.${lookup(var.workspace_to_region, terraform.workspace )}-aws_eip_id

An attribute name is required after a dot.

If this is not a valid patten, what would you do?

Hi @t5unamie,

If you want to dynamically construct an attribute name from an expression, you can use the index syntax rather than the normal attribute syntax:

external_nat_ip_ids = data.terraform_remote_state.eu-eip-nat.outputs["${lookup(var.workspace_to_region, terraform.workspace)}-aws_eip_id"]
1 Like