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

Terraform: Up & Running, 2nd Edition

Book Description

Terraform has become a key player in the DevOps world for defining, launching, and managing infrastructure as code (IaC) across a variety of cloud and virtualization platforms, including AWS, Google Cloud, Azure, and more. This hands-on second edition, expanded and thoroughly updated for Terraform version 0.12 and beyond, shows you the fastest way to get up and running.

Gruntwork cofounder Yevgeniy (Jim) Brikman walks you through code examples that demonstrate Terraform’s simple, declarative programming language for deploying and managing infrastructure with a few commands. Veteran sysadmins, DevOps engineers, and novice developers will quickly go from Terraform basics to running a full stack that can support a massive amount of traffic and a large team of developers.

  • Explore changes from Terraform 0.9 through 0.12, including backends, workspaces, and first-class expressions
  • Learn how to write production-grade Terraform modules
  • Dive into manual and automated testing for Terraform code
  • Compare Terraform to Chef, Puppet, Ansible, CloudFormation, and Salt Stack
  • Deploy server clusters, load balancers, and databases
  • Use Terraform to manage the state of your infrastructure
  • Create reusable infrastructure with Terraform modules
  • Use advanced Terraform syntax to achieve zero-downtime deployment

Table of Contents

  1. Preface
    1. Who Should Read This Book
    2. Why I Wrote This Book
    3. What You Will Find in This Book
    4. What’s New in the Second Edition
    5. What You Won’t Find in This Book
    6. Open Source Code Examples
    7. Using the Code Examples
    8. Conventions Used in This Book
    9. O’Reilly Online Learning
    10. How to Contact O’Reilly Media
    11. Acknowledgments
  2. 1. Why Terraform
    1. The Rise of DevOps
    2. What Is Infrastructure as Code?
      1. Ad Hoc Scripts
      2. Configuration Management Tools
      3. Server Templating Tools
      4. Orchestration Tools
      5. Provisioning Tools
    3. The Benefits of Infrastructure as Code
    4. How Terraform Works
    5. How Terraform Compares to Other IaC Tools
      1. Configuration Management Versus Provisioning
      2. Mutable Infrastructure Versus Immutable Infrastructure
      3. Procedural Language Versus Declarative Language
      4. Master Versus Masterless
      5. Agent Versus Agentless
      6. Large Community Versus Small Community
      7. Mature Versus Cutting Edge
      8. Using Multiple Tools Together
    6. Conclusion
  3. 2. Getting Started with Terraform
    1. Setting Up Your AWS Account
    2. Install Terraform
    3. Deploy a Single Server
    4. Deploy a Single Web Server
    5. Deploy a Configurable Web Server
    6. Deploying a Cluster of Web Servers
    7. Deploying a Load Balancer
    8. Cleanup
    9. Conclusion
  4. 3. How to Manage Terraform State
    1. What Is Terraform State?
    2. Shared Storage for State Files
    3. Limitations with Terraform’s Backends
    4. Isolating State Files
      1. Isolation via Workspaces
      2. Isolation via File Layout
    5. The terraform_remote_state Data Source
    6. Conclusion
  5. 4. How to Create Reusable Infrastructure with Terraform Modules
    1. Module Basics
    2. Module Inputs
    3. Module Locals
    4. Module Outputs
    5. Module Gotchas
      1. File Paths
      2. Inline Blocks
    6. Module Versioning
    7. Conclusion
  6. 5. Terraform Tips and Tricks: Loops, If-Statements, Deployment, and Gotchas
    1. Loops
      1. Loops with the count Parameter
      2. Loops with for_each Expressions
      3. Loops with for Expressions
      4. Loops with the for String Directive
    2. Conditionals
      1. Conditionals with the count Parameter
      2. Conditionals with for_each and for Expressions
      3. Conditionals with the if String Directive
    3. Zero-Downtime Deployment
    4. Terraform Gotchas
      1. count and for_each Have Limitations
      2. Zero-Downtime Deployment Has Limitations
      3. Valid Plans Can Fail
      4. Refactoring Can Be Tricky
      5. Eventual Consistency Is Consistent…Eventually
    5. Conclusion
  7. 6. Production-Grade Terraform Code
    1. Why It Takes So Long to Build Production-Grade Infrastructure
    2. The Production-Grade Infrastructure Checklist
    3. Production-Grade Infrastructure Modules
      1. Small Modules
      2. Composable Modules
      3. Testable Modules
      4. Releasable modules
      5. Beyond Terraform Modules
    4. Conclusion
  8. 7. How to Test Terraform Code
    1. Manual Tests
      1. Manual Testing Basics
      2. Cleaning Up After Tests
    2. Automated Tests
      1. Unit Tests
      2. Integration Tests
      3. End-to-End Tests
      4. Other Testing Approaches
    3. Conclusion
  9. 8. How to Use Terraform as a Team
    1. Adopting IaC in Your Team
      1. Convince Your Boss
      2. Work Incrementally
      3. Give Your Team the Time to Learn
    2. A Workflow for Deploying Application Code
      1. Use Version Control
      2. Run the Code Locally
      3. Make Code Changes
      4. Submit Changes for Review
      5. Run Automated Tests
      6. Merge and Release
      7. Deploy
    3. A Workflow for Deploying Infrastructure Code
      1. Use Version Control
      2. Run the Code Locally
      3. Make Code Changes
      4. Submit Changes for Review
      5. Run Automated Tests
      6. Merge and Release
      7. Deploy
    4. Putting It All Together
    5. Conclusion
  10. A. Recommended Reading
    1. Books
    2. Blogs
    3. Talks
    4. Newsletters
    5. Online Forums
  11. Index