Terraform Cookbook

Book description

Cloud services and SaaS software permeate every company's IT landscape, requiring a shift from manually provisioned services to a more structured approach, with codification at its core. Terraform provides tools to manage the lifecycle of your IT landscape across thousands of different cloud providers and SaaS platforms.

By defining your infrastructure as code you can safely and predictably make changes, modularize crucial building blocks, and create reusable service components. Each recipe in this cookbook addresses a specific problem and prefaces the solution with detailed insights into the "how" and "why".

If you're just starting with Terraform and codified infrastructure, this book will help you create a solid foundation, on which you can build for years to come. If you're an advanced user, this guide will help you reaffirm your knowledge and take it to the next level, as you challenge yourself with more complex infrastructure, spread across multiple providers.

Recipes include:

  • Strategies on how to use Terraform with Version Control Systems
  • Validation and testing patterns for Terraform-managed infrastructure
  • Methods for importing pre-existing resources
  • Transforming infrastructure services into reusable components
  • Integrating Terraform with other HashiCorp tools
  • Deploying Containerized Workloads

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who Should Read This Book
    2. Why We Wrote This Book
    3. What You Will Find in This Book
    4. What You Won’t Find in This Book
      1. Conventions Used in This Book
      2. Using Code Examples
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
  2. 1. Getting Started with Terraform
    1. 1.1. When to Use Terraform
    2. 1.2. When Not to Use Terraform
    3. 1.3. Installing and Configuring Terraform
    4. 1.4. Understanding Terraform Providers
    5. 1.5. Understanding Terraform Modules
    6. 1.6. Modifying Terraform State with terraform console
    7. 1.7. Using HashiCorp Cloud Platform Terraform
    8. 1.8. Using Terraform with Visual Studio Code
    9. 1.9. Managing Terraform Provider Version Constraints
    10. 1.10. Strategies for Deploying Version-Controlled Terraform Code Repositories
    11. 1.11. Deploying a Docker Container with Terraform
    12. 1.12. Upgrading Terraform: From 0.x to 1.x and Beyond
    13. 1.13. Using Terraform Variables
    14. 1.14. Consistent Code for a Consistent Experience
  3. 2. Terraform Basics
    1. 2.1. Formatting and Validating Terraform Code
    2. 2.2. Rapid Experimentation with terraform console
    3. 2.3. Improving Code Quality with TFLint
    4. 2.4. Improving Code Quality with TFSec
    5. 2.5. Validating Code with Preconditions and Postconditions
    6. 2.6. Validating Code with Open Policy Agent
    7. 2.7. Documenting Code with terraform-docs
    8. 2.8. Automating Code Validation with GitHub Actions
    9. 2.9. Using Dependabot for Provider Version Updates
    10. 2.10. Using GitHub Codespaces and DevContainers
    11. 2.11. Limiting Blast Radius with Terraform
  4. 3. Terraform Syntax Patterns
    1. 3.1. Cleaning User Inputs with trimspace
    2. 3.2. Removing Prefixes and Suffixes
    3. 3.3. Working with Regular Expressions
    4. 3.4. Advanced String Manipulation
    5. 3.5. Working with Case-Sensitive Strings Using title, upper, and lower
    6. 3.6. Alphabetically Sorting Lists
    7. 3.7. Creating Subnets from a CIDR Block
    8. 3.8. Interacting with the Local Filesystem
    9. 3.9. Rendering Templates
    10. 3.10. Validating Input Strings
    11. 3.11. Optimistically Retrieving Data Using can and try
    12. 3.12. Sequentially Processing Input Data
    13. 3.13. Good Error Messages for Bad Input
    14. 3.14. Consuming Data Across Terraform States
  5. 4. Terraform Modules and Providers
    1. 4.1. Using Public Modules to Create an EKS Cluster
    2. 4.2. Linting Terraform with GitHub Actions
    3. 4.3. Authentication for Terraform Providers
    4. 4.4. Authentication for Private Modules
    5. 4.5. Creating a Terraform Module
    6. 4.6. Managing GitHub Secrets with Terraform
    7. 4.7. Managing GitHub Repositories with Terraform
    8. 4.8. Dynamic Configuration with Consul KV
    9. 4.9. Service-Health-Aware Provider Configuration
    10. 4.10. Consuming Terraform State with Providers
    11. 4.11. Using Multiple, Identical Providers
  6. 5. Container Management with Terraform
    1. 5.1. Using Local Versus Remote Docker Images
    2. 5.2. Distinguishing Between Cluster Deployment and Cluster Configuration
    3. 5.3. Authorizing Terraform for Cluster Operations
    4. 5.4. Scheduling Containers on Kubernetes with YAML
    5. 5.5. Scheduling Containers on Kubernetes with HCL
    6. 5.6. Transmuting Kubernetes YAML into HCL using k2tf
    7. 5.7. Adjusting Annotations for Kubernetes Deployments
    8. 5.8. Adjusting Configuration for Kubernetes Deployments
    9. 5.9. Applying Kubernetes NetworkPolicies with Terraform
    10. 5.10. Deploying Containers with Helm
    11. 5.11. Enabling Monitoring for Kubernetes Deployments Using Helm
    12. 5.12. Scheduling Containers on HashiCorp Nomad
  7. 6. HCP Terraform and Terraform Enterprise
    1. 6.1. Setting Up HCP Terraform
    2. 6.2. Integrating HCP Terraform with VCS
    3. 6.3. Consuming Terraform State with HCP Terraform
    4. 6.4. Using HCP Terraform with GitHub Actions
    5. 6.5. Collaborative Workflows with HCP Terraform
    6. 6.6. Troubleshooting HCP Terraform and Terraform Enterprise
    7. 6.7. Enforcing Policy as Code
    8. 6.8. Managing Costs with HCP Terraform
    9. 6.9. Utilizing Remote Operations for Large-Scale Deployments
    10. 6.10. Advanced State Management and Recovery
  8. 7. Consuming and Managing Secrets with Terraform
    1. 7.1. Dealing with Sensitive Data in Terraform
    2. 7.2. Retrieving Key-Values from HashiCorp Vault
    3. 7.3. Managing Kubernetes Secrets with Kubernetes-Native Functions
    4. 7.4. Managing Kubernetes Secrets with Vault and Terraform
    5. 7.5. Storing Secrets as Environment Variables
    6. 7.6. Auditing and Rotating Secrets
    7. 7.7. Managing Secrets with Terraform and Password Managers
    8. 7.8. Compliance and Governance with Terraform Secrets
    9. 7.9. Dynamic Secrets with HashiCorp Vault
    10. 7.10. Securing Secret Injection in CI/CD Pipelines
  9. 8. Configuration Management with Terraform
    1. 8.1. Writing Shell Scripts with Terraform
    2. 8.2. Writing Ansible Configuration with Terraform
    3. 8.3. Dynamic Configuration with Consul Key-Values
    4. 8.4. Consuming Data from HTTP Interfaces
    5. 8.5. Applying Conditional Logic in Terraform
    6. 8.6. Importing Existing Infrastructure into Terraform
    7. 8.7. Leveraging Terraform Workspaces
    8. 8.8. Utilizing Terraform Templates
    9. 8.9. Managing Dependencies Between Terraform Resources
    10. 8.10. Using Terraform for Blue-Green Deployments
  10. 9. Advanced Terraform Techniques
    1. 9.1. Writing Configuration Files with Terraform
    2. 9.2. Writing Cloud-Init Configuration
    3. 9.3. Implementing Terraform Modules in CI/CD Pipelines
    4. 9.4. Advanced State Management in Terraform
    5. 9.5. Terraform and Multicloud Strategies
    6. 9.6. Terraform for Scalable Architectures
    7. 9.7. Terraform Custom Provider Development
    8. 9.8. Integrating Terraform with Monitoring Tools
    9. 9.9. Managing Security and Compliance with Terraform
    10. 9.10. Advanced Debugging Techniques in Terraform
  11. 10. Real-World Use Cases
    1. 10.1. Managing Multiple Environments with Terraform Workspaces
    2. 10.2. Deploying a Highly Available Web Application Across Regions
    3. 10.3. Provisioning a Scalable Kubernetes Cluster on AWS EKS
    4. 10.4. Implementing Blue-Green Deployments with Terraform
    5. 10.5. Automating Database Migrations with Terraform and AWS RDS
    6. 10.6. Deploying a Serverless Application on AWS Lambda and API Gateway
    7. 10.7. Implementing Infrastructure as Code for GitOps Workflows
    8. 10.8. Optimizing Costs with Terraform and Spot Instances
    9. 10.9. Deploying a Multicloud Monitoring Solution with Terraform
    10. 10.10. Automating Disaster Recovery with Terraform and AWS
  12. Index
  13. About the Authors

Product information

  • Title: Terraform Cookbook
  • Author(s): Kerim Satirli, Taylor Dolezal
  • Release date: October 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098108465