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.71: Using ternary with count<br />

variable "environment" {<br />

default = "development"<br />

}<br />

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

ami<br />

= "${lookup(var.ami, var.region)}"<br />

. . .<br />

}<br />

count = "${var.environment == "production" ? 4 : 2}"<br />

Here we’ve set a variable called environment with a default of development. In<br />

our resource we’ve configured our count attribute with a conditional. If the var.<br />

environment variable equals production then launch 4 instances, if it is the default<br />

of development, or any other value, then only launch 2 instances.<br />

<strong>The</strong> condition can be any interpolation: a variable, a function, or even chaining<br />

another conditional. <strong>The</strong> true or false values can also return any interpolation or<br />

valid value. <strong>The</strong> true and false values must return the same type though.<br />

<strong>The</strong> condition supports a bunch of operators. We’ve already seen equality, ==,<br />

and <strong>Terraform</strong> supports the opposite operator != for inequality. It also supports<br />

numeric comparisons like greater or less, > and = and

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

Saved successfully!

Ooh no, something went wrong!