16.03.2017 Views

The Terraform Book

TheTerraformBook_sample

TheTerraformBook_sample

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Building an application stack with <strong>Terraform</strong><br />

Listing 3.65: Interpolated math<br />

tags {<br />

Name = "web-${format("%03d", count.index + 1)}"<br />

}<br />

This would add one to every count.index value producing the tags web-001 and<br />

web-002 respectively. We can do other math: subtract, multiple, divide, etc., on<br />

any integer or float variables.<br />

We can also iterate through list elements with count.index. Let’s create a list<br />

variable with some tags we’d like to add to our instances.<br />

Listing 3.66: AWS owner tags in variables.tf<br />

variable "owner_tag" {<br />

default = ["team1", "team2"]<br />

}<br />

We’d like to distribute our instances between these two tag values in web.tf.<br />

Listing 3.67: Splitting up the count instances<br />

resource "aws_instance" "web" {<br />

. . .<br />

}<br />

tags {<br />

Owner = "${var.owner_tag[count.index]}"<br />

}<br />

count = "${length(var.instance_ips)}"<br />

Version: v1.1.1 (e771e4d) 53

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!