16.03.2017 Views

The Terraform Book

TheTerraformBook_sample

TheTerraformBook_sample

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

user data, you can specify some commands or actions that should be run when<br />

the EC2 instance is launched.<br />

TIP We’ll learn more about provisioning and integration with configuration<br />

management tools in Chapter 4.<br />

To make use of user data in <strong>Terraform</strong> we add the user_data attribute to our<br />

aws_instance.web resources in the web.tf file.<br />

Listing 3.73: Adding user data to our instances<br />

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

. . .<br />

user_data = "${file("files/web_bootstrap.sh")}"<br />

. . .<br />

}<br />

count<br />

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

We can see that the value of our user_data attribute is:<br />

${file("files/web_bootstrap.sh")}<br />

This uses a new function, file, to load the contents of a file as the value of an<br />

attribute. In this case we’re loading a shell script called web_bootstrap.sh from<br />

a directory called files. <strong>The</strong> location of the files directory is relative to the<br />

current directory.<br />

Let’s create that directory and file now.<br />

Version: v1.1.1 (e771e4d) 58

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

Saved successfully!

Ooh no, something went wrong!