I'm creating subnets as part of a seperate terraform template and exporting the IDs as follows.

output "subnet-aza-dev" {
  value = "${aws_subnet.subnet-aza-dev.id}"
output "subnet-azb-dev" {
  value = "${aws_subnet.subnet-azb-dev.id}"
output "subnet-aza-test" {
  value = "${aws_subnet.subnet-aza-test.id}"
output "subnet-azb-test" {
  value = "${aws_subnet.subnet-azb-test.id}"

I'm then intending to lookup these IDs in another template which is reused to provision multiple environments. Example below shows my second template is calling a module to provision an EC2 instance and is passing through the subnet_id.

variable "environment" {
    description = "Environment name"
    default     = "dev"

module "sql-1-ec2" {
  source     = "../modules/ec2winserver_sql"
  subnet_id  = "${data.terraform_remote_state.env-shared.subnet-aza-dev}" 

What I'd like to do is pass the environment variable as part of the lookup for the subnet_id e.g.

subnet_id  = "${data.terraform_remote_state.env-shared.subnet-aza-${var.environment}"

However I'm aware that variable interpolation isn't supported. I've tried using a map inside of the first terraform template to export them all to a 'subnet' which I could then use to lookup from the second template. This didn't work as I was unable to output variables inside of the map.

This sort of design pattern is something I've used previously with CloudFormation, however I'm much newer to terraform. Am I missing something obvious here?

1 Answer 1


I think an output map is the right approach. I'm not sure what went wrong when you tried it, but try something like:

output "subnets_map" {
  dev = "${aws_subnet.subnet_aza_dev.id}"
  test = "${aws_subnet.subnet_aza_test.id}"


subnet_id = "${data.terraform_remote_state.env_shared.subnets_map[var.environment]}"

BTW I think it's better to use underscores rather than hyphens in HCL

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .