Terraform: Up and Running, 3rd 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 third edition, expanded and thoroughly updated for version 0.12 and beyond, shows you the fastest way to get up and running with Terraform.

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.12 through 1.0, 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

Publisher resources

View/Submit Errata

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. Changes from the Second Edition to the Third Edition
    5. Changes from the First Edition to the Second Edition
    6. What You Won’t Find in This Book
    7. Open Source Code Examples
    8. Using the Code Examples
    9. Conventions Used in This Book
    10. O’Reilly Online Learning
    11. How to Contact O’Reilly Media
    12. 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. Managing Secrets with Terraform
    1. Secret management basics
    2. Secret management tools
      1. Key considerations with secret management tools
      2. A comparison of secret management tools
    3. Using secret management tools with Terraform
      1. Providers
      2. Resources and data sources
      3. State files and plan files
    4. Conclusion
  8. 7. Working with Multiple Providers
    1. Working with one provider
      1. What is a provider?
      2. How do you install providers?
      3. How do you use providers?
    2. Working with multiple copies of the same provider
      1. Working with multiple AWS regions
      2. Working with multiple AWS accounts
      3. Creating modules that can work with multiple providers
    3. Working with multiple different providers
      1. Multi-provider and multi-cloud
      2. Multi-provider example: AWS and Kubernetes
    4. Conclusion
  9. 8. 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. Versioned modules
      5. Beyond Terraform Modules
    4. Conclusion
  10. 9. 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
  11. 10. 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
  12. A. Recommended Reading
    1. Books
    2. Blogs
    3. Talks
    4. Newsletters
    5. Online Forums
  13. About the Author

Product information

  • Title: Terraform: Up and Running, 3rd Edition
  • Author(s): Yevgeniy Brikman
  • Release date: September 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098116743