Book Image

Getting Started with Terraform - Second Edition

By : Kirill Shirinkin
1 (1)
Book Image

Getting Started with Terraform - Second Edition

1 (1)
By: Kirill Shirinkin

Overview of this book

Terraform is a tool used to efficiently build, configure, and improve the production infrastructure. It can manage the existing infrastructure as well as create custom in-house solutions. This book shows you when and how to implement infrastructure as a code practices with Terraform. It covers everything necessary to set up the complete management of infrastructure with Terraform, starting with the basics of using providers and resources. It is a comprehensive guide that begins with very small infrastructure templates and takes you all the way to managing complex systems, all using concrete examples that evolve over the course of the book. The book ends with the complete workflow of managing a production infrastructure as code—this is achieved with the help of version control and continuous integration. The readers will also learn how to combine multiple providers in a single template and manage different code bases with many complex modules. It focuses on how to set up continuous integration for the infrastructure code. The readers will be able to use Terraform to build, change, and combine infrastructure safely and efficiently.
Table of Contents (15 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Exploring Terraform configuration resources


Quite frequently, you will require some random data to be generated. This could be default password for a database or a random hostname for your servers. Terraform has random provider that solves this problem.

Of course, completely random values are harmful for Terraform. That's why, the random_id resource generates random string only on creation and then value is kept during updates (unless you change the configuration of this resource). Imagine that we want to pass random hostname to the previously configured template_file user data. We could do it as follows:

resource "random_id" "hostname" { 
  byte_length = 4 
} 
data "template_file" "user_data" { 
  template = "${file("${path.module}/user_data.sh.tpl")}" 
  vars { 
    packages = "${var.extra_packages}" 
    nameserver = "${var.external_nameserver}" 
    hostname = "${random_id.hostname.b64}" 
  } 
} 

Then, the actual script can use the hostname variable to set the hostname of the machine. If...