Terraform - Concatonate / Join Variables (dynamic interpolation)

Is it possible to include or attach variables in terraform? I am struggling to find a link to the correct syntax.

I would like to do something like this:

variable "env" {
  default = "production"
}

variable "vpc_name" {
  default = "cloudy"
}

resource "aws_subnet" "${var.env}_${var.vpc_name}_pub1" {  
  vpc_id = "${aws_vpc.${var.vpc_name}.id}"  
  cidr_block = "10.0.1.0/24"  
  availability_zone = "us-east-1a"  
}

      

To effectively achieve something like this:

resource "aws_subnet" "production_cloudy_pub1" {  
  vpc_id = "${aws_vpc.cloudy.id}"  
  cidr_block = "10.0.1.0/24"  
  availability_zone = "us-east-1a"  
}

      

Thanks in advance, Alex

+3


source to share


1 answer


As mentioned in the comments, you cannot interpolate variables like in Terraform, or do things like default to a variable for another variable that you could achieve your stated purpose using data sources.

In an example, you can do something like the following:

variable "env" {
  default = "production"
}

variable "vpc_name" {
  default = "cloudy"
}

data "aws_vpc" "selected" {
  tags {
    Name = "${var.vpc_name}"
  }
}

resource "aws_subnet" "pub1" {  
  vpc_id = "${data.aws_vpc.selected.id}"  
  cidr_block = "10.0.1.0/24"  
  availability_zone = "us-east-1a"  
}

      

This will automatically create a subnet in the cloud VPC.

It also allows you to retrieve more information than just the VPC ID so you can do something like this:



variable "env" {
  default = "production"
}

variable "vpc_name" {
  default = "cloudy"
}

data "aws_vpc" "selected" {
  tags {
    Name = "${var.vpc_name}"
  }
}

resource "aws_subnet" "public" {  
  vpc_id = "${data.aws_vpc.selected.id}"  
  cidr_block = "${cidrsubnet(data.aws_vpc.selected.cidr_block, 8, 1)}" 
  availability_zone = "us-east-1a"  
}

      

The function calculates the subnet from the specified CIDR range. In this case, if your VPC was , this will return . cidrsubnet

10.0.0.0/16

10.0.1.0/24

The examples above solve your main problem, but do not allow you to use dynamically named Terraform resources. In your short example, this doesn't seem to be necessary, but if you want something dynamic, then you can also combine that with something like an on-resource count:

variable "env" {
  default = "production"
}

variable "vpc_name" {
  default = "cloudy"
}

data "aws_vpc" "selected" {
  tags {
    Name = "${var.vpc_name}"
  }
}

data "aws_availability_zones" "all" {}

resource "aws_subnet" "public" {
  count = "${length(data.aws_availability_zones.all.names)}"
  vpc_id = "${data.aws_vpc.selected.id}"  
  cidr_block = "${cidrsubnet(data.aws_vpc.selected.cidr_block, 8, count.index)}" 
  availability_zone = "${data.aws_availability_zones.all.names[count.index]}"  
}

      

This now dynamically creates a subnet in every AZ in the region for your VPC. Obviously you can get this pretty far, and I would recommend reading data sources in general plus all AWS specific data .

+2


source







All Articles