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 />

We can then populate this file with variables—here a string, map, and list respectively.<br />

Listing 3.19: Adding variable assignments<br />

access_key = "abc123"<br />

secret_key = "abc123"<br />

ami = {<br />

us-east-1 = "ami-0d729a60"<br />

us-west-1 = "ami-7c4b331c"<br />

}<br />

security_group_ids = [<br />

"sg-4f713c35",<br />

"sg-4f713c35",<br />

"sg-4f713c35"<br />

]<br />

When <strong>Terraform</strong> runs it will automatically load the terraform.tfvars file and<br />

assign any variable values in it. <strong>The</strong> file can contain <strong>Terraform</strong> configuration<br />

syntax or JSON, just like normal <strong>Terraform</strong> configuration files.<br />

Any variable for which you define a value needs to exist. In our case, the variables<br />

access_key, secret_key, and security_group_ids need to be defined with<br />

variable blocks in our variables.tf file. If they do not exist you’ll get an error<br />

like so:<br />

Listing 3.20: Variable doesn’t exist error<br />

module root: 1 error(s) occurred:<br />

* provider config 'aws': unknown variable referenced: '<br />

access_key'. define it with 'variable' blocks<br />

You can also name the terraform.tfvars file something else—for example, we<br />

Version: v1.1.1 (e771e4d) 15

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

Saved successfully!

Ooh no, something went wrong!