Terraform Cookbook

Book description

Discover how to manage and scale your infrastructure using Infrastructure as Code (IaC) with Terraform

Key Features

  • Get up and running with the latest version of Terraform, v0.13
  • Design and manage infrastructure that can be shared, tested, modified, provisioned, and deployed
  • Work through practical recipes to achieve zero-downtime deployment and scale your infrastructure effectively

Book Description

HashiCorp Configuration Language (HCL) has changed how we define and provision a data center infrastructure with the launch of Terraform—one of the most popular and powerful products for building Infrastructure as Code. This practical guide will show you how to leverage HashiCorp's Terraform tool to manage a complex infrastructure with ease.

Starting with recipes for setting up the environment, this book will gradually guide you in configuring, provisioning, collaborating, and building a multi-environment architecture. Unlike other books, you’ll also be able to explore recipes with real-world examples to provision your Azure infrastructure with Terraform. Once you’ve covered topics such as Azure Template, Azure CLI, Terraform configuration, and Terragrunt, you’ll delve into manual and automated testing with Terraform configurations. The next set of chapters will show you how to manage a balanced and efficient infrastructure and create reusable infrastructure with Terraform modules. Finally, you’ll explore the latest DevOps trends such as continuous integration and continuous delivery (CI/CD) and zero-downtime deployments.

By the end of this book, you’ll have developed the skills you need to get the most value out of Terraform and manage your infrastructure effectively.

What you will learn

  • Understand how to install Terraform for local development
  • Get to grips with writing Terraform configuration for infrastructure provisioning
  • Use Terraform for advanced infrastructure use cases
  • Understand how to write and use Terraform modules
  • Discover how to use Terraform for Azure infrastructure provisioning
  • Become well-versed in testing Terraform configuration
  • Execute Terraform configuration in CI/CD pipelines
  • Explore how to use Terraform Cloud

Who this book is for

This book is for developers, operators, and DevOps engineers looking to improve their workflow and use Infrastructure as Code. Experience with Microsoft Azure, Jenkins, shell scripting, and DevOps practices is required to get the most out of this Terraform book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Terraform Cookbook
  3. Foreword
  4. Dedication
  5. About Packt
    1. Why subscribe?
  6. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Code in Action
      3. Download the color images
      4. Conventions used
    4. Get in touch
      1. Reviews
  8. Setting Up the Terraform Environment
    1. Technical requirements
    2. Downloading and installing Terraform manually
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Installing Terraform using a script on Linux
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Installing Terraform using a script on Windows
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Executing Terraform in a Docker container
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Writing Terraform configuration in VS Code
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Migrating your Terraform configuration to Terraform 0.13
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  9. Writing Terraform Configuration
    1. Technical requirements
    2. Configuring Terraform and the provider version to use
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Manipulating variables
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Using local variables for custom functions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    5. Using outputs to expose Terraform provisioned data
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Provisioning infrastructure in multiple environments
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    7. Obtaining external data with data sources
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    8. Using external resources from other state files
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    9. Querying external data with Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    10. Calling Terraform built-in functions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    11. Writing conditional expressions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    12. Manipulating local files with Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    13. Executing local programs with Terraform 
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    14. Generating passwords with Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  10. Building Dynamic Environments with Terraform
    1. Technical requirements
    2. Provisioning multiple resources with the count property
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Using a table of key-value variables with maps
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Looping over object collections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Generating multiple blocks with dynamic expressions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  11. Using the Terraform CLI
    1. Technical requirements
    2. Keeping your Terraform configuration clean
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Validating the code syntax
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Destroying infrastructure resources
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Using workspaces for managing environments
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Importing existing resources
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Exporting the output in JSON
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    8. Tainting resources
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    9. Generating the graph dependencies
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    10. Debugging the Terraform execution
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  12. Sharing Terraform Configuration with Modules
    1. Technical requirements
    2. Creating a Terraform module and using it locally
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Using modules from the public registry
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Sharing a Terraform module using GitHub
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Using another file inside a custom module
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Using the Terraform module generator
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Generating module documentation
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    8. Using a private Git repository for sharing a Terraform module
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    9. Applying a Terrafile pattern for using modules
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    10. Testing Terraform module code with Terratest
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    11. Building CI/CD for Terraform modules in Azure Pipelines
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    12. Building a workflow for Terraform modules using GitHub Actions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  13. Provisioning Azure Infrastructure with Terraform
    1. Technical requirements
    2. Using Terraform in Azure Cloud Shell
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Protecting the Azure credential provider
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
      5. See also
    4. Protecting the state file in the Azure remote backend
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Executing ARM templates in Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Executing Azure CLI commands in Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Using Azure Key Vault with Terraform to protect secrets
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    8. Getting a list of Azure resources in Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    9. Provisioning and configuring an Azure VM with Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    10. Building Azure serverless infrastructure with Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    11. Generating a Terraform configuration for existing Azure infrastructure
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  14. Deep Diving into Terraform
    1. Technical requirements
    2. Creating an Ansible inventory with Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    3. Testing the Terraform configuration using kitchen-terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    4. Preventing resources from getting destroyed
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    5. Zero-downtime deployment with Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    6. Detecting resources deleted by the plan command
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    7. Managing Terraform configuration dependencies using Terragrunt
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    8. Using Terragrunt as a wrapper for Terraform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    9. Building CI/CD pipelines for Terraform configurations in Azure Pipelines
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    10. Working with workspaces in CI/CD
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  15. Using Terraform Cloud to Improve Collaboration
    1. Technical requirements
    2. Using the remote backend in Terraform Cloud
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Using Terraform Cloud as a private module registry
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Executing Terraform configuration remotely in Terraform Cloud
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Automating Terraform Cloud using APIs
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Testing the compliance of Terraform configurations using Sentinel
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Using cost estimation for cloud cost resources governance
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Terraform Cookbook
  • Author(s): Mikael Krief
  • Release date: October 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781800207554