O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Terraform Book

Book Description

Updated for Terraform 0.11!

A hands-on, introductory book about managing infrastructure with HashiCorp's Terraform tool. Start small and then build on what you learn to scale up to complex infrastructure. Written for both developers and sysadmins. Focuses on how to build, test and run infrastructure and applications with Terraform and integration with tools like Consul.

The book contains:

Chapter 1: An Introduction to Terraform
Chapter 2: Installing Terraform
Chapter 3: Building our first application
Chapter 4: Provisioning and Terraform
Chapter 5: Collaborating with Terraform
Chapter 6: Building a multi-environment architecture
Chapter 7: Infrastructure testing

Table of Contents

  1. The Terraform Book
    1. 0.1 Who is this book for?
    2. 0.2 Credits and Acknowledgments
    3. 0.3 Technical Reviewers
      1. 0.3.1 Jennifer Davis
      2. 0.3.2 Thom May
      3. 0.3.3 Peter Miron
      4. 0.3.4 James Nugent
      5. 0.3.5 Paul Stack
    4. 0.4 Editor
    5. 0.5 Author
    6. 0.6 Conventions in the book
    7. 0.7 Code and Examples
    8. 0.8 Colophon
    9. 0.9 Errata
    10. 0.10 Disclaimer
    11. 0.11 Copyright
    12. 0.12 Version
  2. 1 Introduction
    1. 1.1 Infrastructure as Code
      1. 1.1.1 Wait … what’s infrastructure? Wait … what’s infrastructure?
    2. 1.2 Introducing Terraform
    3. 1.3 So why not a configuration management tool?
    4. 1.4 So why not CloudFormation et al?
    5. 1.5 So what can you use Terraform for?
      1. 1.5.1 Multi-tier applications
      2. 1.5.2 Self-service infrastructure
      3. 1.5.3 Production, development, and testing environments
      4. 1.5.4 Continuous delivery
      5. 1.5.5 Managing your management tools
    6. 1.6 What’s in the book? What’s in the book?
    7. 1.7 Links and information
  3. 2 Installing and introducing Terraform
    1. 2.1 Installing Terraform
      1. 2.1.1 Installing Terraform on Linux
      2. 2.1.2 Installing Terraform on Microsoft Windows
      3. 2.1.3 Alternative Microsoft Windows installation
      4. 2.1.4 Alternative Mac OS X installation
    2. 2.2 Getting started with Terraform
      1. 2.2.1 Adding some configuration
      2. 2.2.2 Terraform configuration files
      3. 2.2.3 Setting up Amazon Web Services
      4. 2.2.4 Our first configuration file
      5. 2.2.5 Building our first resource
      6. 2.2.6 Terraform state
      7. 2.2.7 Showing our resource
      8. 2.2.8 Adding a second resource
      9. 2.2.9 Outputting plans
      10. 2.2.10 Targeting resources
      11. 2.2.11 Failed plans
      12. 2.2.12 Showing all the resources
      13. 2.2.13 Viewing the Terraform graph
      14. 2.2.14 Destroying infrastructure
    3. 2.3 Summary
  4. 3 Building an application stack with Terraform
    1. 3.1 Our application stack
    2. 3.2 Parameterizing our configuration
      1. 3.2.1 Variables
      2. 3.2.2 Maps
      3. 3.2.3 Lists
      4. 3.2.4 Variable defaults
      5. 3.2.5 Populating variables
    3. 3.3 Starting our stack
      1. 3.3.1 Using AWS shared credentials
    4. 3.4 First resources
    5. 3.5 Modules
      1. 3.5.1 Defining a module
      2. 3.5.2 Module structure
    6. 3.6 Using our module
      1. 3.6.1 Getting our module
      2. 3.6.2 Moving our module to a repository
    7. 3.7 Counts and counting
      1. 3.7.1 Sets of counted resources using splat
      2. 3.7.2 Setting values with count indexes
      3. 3.7.3 Wrapping counts with the element function
      4. 3.7.4 Conditionals
      5. 3.7.5 Locals
    8. 3.8 Provisioning our stack
    9. 3.9 Finishing up our stack
    10. 3.10 Committing our configuration
    11. 3.11 Validating and formatting
    12. 3.12 Initializing Terraform
    13. 3.13 Planning our stack
    14. 3.14 Applying our stack
    15. 3.15 Graphing our stack
    16. 3.16 Seeing the results
    17. 3.17 Destroying the web stack resources
    18. 3.18 Summary
  5. 4 Provisioning with Terraform
    1. 4.1 What does a provisioner do?
    2. 4.2 Provisioning an instance
      1. 4.2.1 Connection block
      2. 4.2.2 The file provisioner
      3. 4.2.3 Remote execution provisioning
      4. 4.2.4 Destroying the old web stack resources
      5. 4.2.5 Provisioning our web stack
      6. 4.2.6 Failed execution of a provisioner
    3. 4.3 Summary
  6. 5 Collaborating with Terraform
    1. 5.1 Terraform state
    2. 5.2 Remote state
      1. 5.2.1 No state protection
      2. 5.2.2 Not all backends have locking
    3. 5.3 Creating an S3 remote state backend
      1. 5.3.1 Creating a module for an S3 remote state
      2. 5.3.2 Planning the remote state module
      3. 5.3.3 Applying our remote state module
      4. 5.3.4 Uploading the remote state module
    4. 5.4 Configuring Terraform to use remote state
      1. 5.4.1 Setting the remote state
      2. 5.4.2 Disabling remote state
    5. 5.5 Using and sharing remote state
      1. 5.5.1 Sharing state between users
      2. 5.5.2 Externally loading remote state backends
      3. 5.5.3 Using remote state data
      4. 5.5.4 Moving our base state remote
      5. 5.5.5 Adding the data source to the base configuration
    6. 5.6 State and service discovery
      1. 5.6.1 Creating a Consul cluster
      2. 5.6.2 Using Consul and remote state
    7. 5.7 Other tools for managing Terraform state
    8. 5.8 Summary
  7. 6 Building a multi-environment architecture
    1. 6.1 Creating a data center
      1. 6.1.1 Creating a directory structure
    2. 6.2 Workflow
      1. 6.2.1 Develop
      2. 6.2.2 Plan
      3. 6.2.3 Apply in development
      4. 6.2.4 Automation and testing
      5. 6.2.5 Deploy to production
    3. 6.3 The development environment
      1. 6.3.1 Getting our VPC module
      2. 6.3.2 Adding some outputs for the development environment
      3. 6.3.3 Planning the development environment
      4. 6.3.4 Applying the development environment
      5. 6.3.5 Configuring remote state
    4. 6.4 Adding the web service
      1. 6.4.1 The web module
      2. 6.4.2 Using a data source in the web module
      3. 6.4.3 Web instances
      4. 6.4.4 A Cloudflare record
      5. 6.4.5 Committing our module
      6. 6.4.6 Getting our web module
      7. 6.4.7 Planning our web service
      8. 6.4.8 Applying the web service
      9. 6.4.9 Testing our web service
      10. 6.4.10 Removing our web service
    5. 6.5 Adding the API service
      1. 6.5.1 The API module
      2. 6.5.2 API instances
      3. 6.5.3 The API service outputs
      4. 6.5.4 Getting our module
      5. 6.5.5 Planning the API service
      6. 6.5.6 Applying the API service
      7. 6.5.7 Testing our API service
    6. 6.6 Adding a production environment
      1. 6.6.1 Adding services to production
    7. 6.7 State environments
    8. 6.8 Other resources for Terraform environments
    9. 6.9 Summary
  8. 7 Infrastructure testing
    1. 7.1 Test Kitchen
      1. 7.1.1 InSpec
      2. 7.1.2 How Test Kitchen works
      3. 7.1.3 Prerequisites
      4. 7.1.4 Creating a test configuration
      5. 7.1.5 Creating our first control
      6. 7.1.6 Decorating controls with metadata
      7. 7.1.7 Adding another test to our control
      8. 7.1.8 Setting up a Test Kitchen instance
      9. 7.1.9 Running the controls
      10. 7.1.10 Adding a new control
      11. 7.1.11 Building custom InSpec resources
    2. 7.2 Alternative infrastructure testing frameworks
    3. 7.3 Summary