Terraform dynamic variable

by PhilH   Last Updated December 07, 2017 11:00 AM

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?

Tags : terraform

Related Questions

terraform apply keeps changing the same (NAT) route

Updated December 14, 2016 08:00 AM

Can i output a provider from a module in terraform?

Updated April 26, 2018 03:00 AM