Terraform: Layered interpolation

Created on 25 Jan 2016  ·  6Comments  ·  Source: hashicorp/terraform

Terraform flags the ami line in this as a syntax error:

resource "aws_instance" "generic_0" {
    instance_type          = "${var.instance_type}"
    ami                    = "${atlas_artifact.consul_client_ami.metadata_full.region-${var.region}}"
    ...
}

Note the ${ } expression embedded in a larger ${ } expression.

I'm trying to double-interpolate - render the inner variable ${var.region} so the outer variable becomes "${atlas_artifact.consul_client_ami.metadata_full.region-us-west-2}.

Q1. Is nested interpolation of this kind supported?

Q2. If no, how do you avoid having static values for some variables that conceptually are the same as other dynamic variables set at build time?

core question

All 6 comments

Hi @chris-hailstorm - Terraform does not support nested interpolation like this. We generally discourage thinking about resource or variable names as dynamic for this reason.

Generally mapping variables + lookup() is where I'd point for this type of AMI-per-region setup:

variable "region" {
  default = "us-east-1"
}
variable "ami" {
  default = {
    "us-east-1": "ami-abc123",
    "us-west-2": "ami-def567",
  } 
}

output "ami" {
  value = "${lookup(var.ami, var.region)}"
} 

In the case of atlas_artifact, we're limited by the structure of its metadata, which may be something we need to fix. I'll look into the Atlas behavior there and follow up.

Does this address your Q2?

Yes thank you.

On Mon, Jan 25, 2016 at 12:28 PM, Paul Hinze [email protected]
wrote:

Hi @chris-hailstorm https://github.com/chris-hailstorm - Terraform does
not support nested interpolation like this. We generally discourage
thinking about resource or variable names as dynamic for this reason.

Generally mapping variables + lookup() is where I'd point for this type
of AMI-per-region setup:

variable "region" {
default = "us-east-1"
}
variable "ami" {
default = {
"us-east-1": "ami-abc123",
"us-west-2": "ami-def567",
}
}

output "ami" {
value = "${lookup(var.ami, var.region)}"
}

In the case of atlas_artifact, we're limited by the structure of its
metadata, which may be something we need to fix. I'll look into the Atlas
behavior there and follow up.

Does this address your Q2?


Reply to this email directly or view it on GitHub
https://github.com/hashicorp/terraform/issues/4816#issuecomment-174594436
.

I encountered a situation where I'd like some nested interpolation, but I'm inferring that this isn't planned. So, I'd like to pile on another question.

I can do this sort of thing:
vpc_security_group_ids = ["${split(",",var.stage_sg)}"]

Which produces:

~ aws_instance.stage
    vpc_security_group_ids.1137882915: "" => "wxmix_admin_sg"
    vpc_security_group_ids.1691984883: "" => "wxmix_http_sg"
    vpc_security_group_ids.2015269846: "sg-7cc82407" => ""
    vpc_security_group_ids.4094431172: "sg-ff628f84" => ""
    vpc_security_group_ids.584576804:  "" => "wxmix_sg"
    vpc_security_group_ids.746700993:  "sg-d8e1a9a3" => ""

Very close to what I want. What would be nice is if I could avoid hardcoding my SG names inside my instance code. I'd also like it to iterate (I know, right?) so I could MAYBE do:

vpc_security_group_ids = [ "${aws_security_group.${split(",",var.stage_sg), count.index}.id}",
                             "${aws_security_group.${split(",",var.stage_sg), count.index}.id}",
                             "${aws_security_group.${split(",",var.stage_sg), count.index}.id}" ]

I expect that is a big ask in terms of under-the-hood work. I just don't like hardcoded values. Maybe there is another way to do what I want to do?

We're currently not interested in nested interpolations since it makes the requirements too dynamic. Sorry!

I am also in need of this feature.

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