Interpolation in resource names: possible?

4,107 views
Skip to first unread message

Robert Kulagowski

unread,
Mar 18, 2016, 3:02:37 PM3/18/16
to Terraform
terraform 0.6.12

I'm trying to create a set of generic scripts, where a variable in variables.tf will create the proper SG, etc.

This is the relevant part; "application" is "foobar". If I can get this to work then I might use "deploymentType" as "dev", "prod", "uat", etc. First things first.

resource "aws_security_group" "SG_USE1_${upper(${var.application})}DEV_PRIVATE_ELB_V01" {
# The next line causes a syntax error
#    name = "${upper(SG_USE1_${var.application}${var.deploymentType}_PRIVATE_ELB_V${var.version})}"
# So we hard code it, but we don't want that.
    name = "SG_USE1_FOOBARDEV_PRIVATE_ELB_V01"
    description = "SG to allow access from the ELB"
    vpc_id = "${var.aws_vpc_id}"


and

resource "aws_launch_configuration" "lan-lc" {
    name_prefix = "lan-${var.deploymentType}-"
    image_id = "${lookup(var.awsAMIs, var.awsRegion)}"
    instance_type = "${lookup(var.instanceType, var.deploymentType)}"
    iam_instance_profile = "S3-SSI-LAN-RW"

    lifecycle {
        create_before_destroy = true
    }


Error validating: 1 error(s) occurred:

* resource 'aws_launch_configuration.lan-lc' config: unknown resource 'aws_security_group.SG_USE1_FOOBARDEV_PRIVATE_ELB_V01' referenced in variable aws_security_group.SG_USE1_FOOBARDEV_PRIVATE_ELB_V01.id

It also fails in the launch config if I replace the second security group with the same resource name structure that I use in the security group:

 security_groups = ["${aws_security_group.SG_USE1_LANDEV_REM_ACC_V01.id}", "${aws_security_group.SG_USE1_${upper(${var.application})}DEV_PRIVATE_ELB_V01.id}"]

"parse error: syntax error"

Paul Hinze

unread,
Mar 23, 2016, 6:28:00 PM3/23/16
to terrafo...@googlegroups.com
Hi Robert,

Thanks for the question! Terraform does not currently allow for interpolation in resource names. Dynamic resource names would make it difficult for us to maintain the predictability guarantees baked in to the `terraform plan` / `terraform apply` contract. If both names of and references to resources supported interpolation, the shape of the dependency graph could change drastically depending on the result of an interpolation. It might be a feature we explore down the line, but it's not currently on the roadmap.

In the meantime, the best way to accomplish the sort of reuse you're looking for is to use Modules, for example:

module "foo-prod" {
  source           = "./application"
  application      = "foo"
  deployment_type  = "prod"
  ami              = "${lookup(var.awsAMIs, var.awsRegion)}"
}

module "foo-dev" {
  source           = "./application"
  application      = "foo"
  deployment_type  = "dev"
  ami              = "${lookup(var.awsAMIs, var.awsRegion)}"
}

# ./application/main.tf
variable "application" { }
variable "deployment_type" { }

resource "aws_security_group" "private-elb" {
  # I'd avoid name and description, since these cannot be changed without
  # replacing the SG, so I'm putting these in tags here, but it's up to you! :)
  tags {
    Name = "sg-use1-${var.application}-${var.deployment_type}-private-elb"
  }
}

resource "aws_launch_configuration" "lan-lc" {
  name_prefix = "lan-${var.deploymentType}-"
  image_id    = "${var.ami}"
  # ... etc ...
}

I hope this helps! Let me know if you have any further questions,

Paul

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/aa1f6fca-5939-43df-875c-141715a7a7c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages