Book Image

Terraform Cookbook

By : Mikael Krief
Book Image

Terraform Cookbook

By: Mikael Krief

Overview of this book

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.
Table of Contents (10 chapters)

Using workspaces for managing environments

In Terraform, there is the concept of workspaces, which enables the same Terraform configuration to be used in order to build multiple environments.

Each of these configurations will be written to a different Terraform state file and will thus be isolated from the other configurations. Workspaces can be used to create several environments of our infrastructure.

In this recipe, we will study the use of Terraform workspaces in the Terraform configuration, with the execution of Terraform commands.

Getting ready

The purpose of this recipe is for an application to create a Resource Group for each of its environments (dev and prod).

Regarding the Terraform configuration, no prerequisites are necessary. We will see it in the steps of the recipe.

The Terraform configuration for this recipe is available here: https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP04/workspaces

How to do it…

To manage a Terraform workspace, perform...