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 1.0 and beyond, shows you the fastest way to get up and running with Terraform.

Gruntwork cofounder Yevgeniy (Jim) Brikman takes 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.

  • Compare Terraform with Chef, Puppet, Ansible, CloudFormation, and Pulumi
  • Deploy servers, load balancers, and databases
  • Create reusable infrastructure with Terraform modules
  • Test your Terraform modules with static analysis, unit tests, and integration tests
  • Configure CI/CD pipelines for both your apps and infrastructure code
  • Use advanced Terraform syntax for loops, conditionals, and zero-downtime deployment
  • Get up to speed on Terraform 0.13 to 1.0 and beyond
  • Work with multiple clouds and providers (including Kubernetes!)

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. What Is 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. What Are the Benefits of Infrastructure as Code?
    4. How Does Terraform Work?
    5. How Does Terraform Compare to Other IaC Tools?
      1. Configuration Management Versus Provisioning
      2. Mutable Infrastructure Versus Immutable Infrastructure
      3. Procedural Language Versus Declarative Language
      4. General-Purpose Language Versus Domain-Specific Language
      5. Master Versus Masterless
      6. Agent Versus Agentless
      7. Paid Versus Free Offering
      8. Large Community Versus Small Community
      9. Mature Versus Cutting Edge
      10. Use of Multiple Tools Together
    6. Conclusion
  3. 2. Getting Started with Terraform
    1. Setting Up Your AWS Account
    2. Installing Terraform
    3. Deploying a Single Server
    4. Deploying a Single Web Server
    5. Deploying 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. State File Isolation
      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. Conclusion
  7. 6. Managing Secrets with Terraform
    1. Secret Management Basics
    2. Secret Management Tools
      1. The Types of Secrets You Store
      2. The Way You Store Secrets
      3. The Interface You Use to Access Secrets
      4. A Comparison of Secret Management Tools
    3. 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. A Crash Course on Docker
      2. A Crash Course on Kubernetes
      3. Deploying Docker Containers in AWS Using Elastic Kubernetes Service
    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. Index
  14. 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