In this lesson, we will explore how to use Terraform workspaces effectively to manage multiple environments and streamline your infrastructure as code (IaC) projects. By leveraging workspaces, you can eliminate repetitive tasks and maintain a single configuration directory while handling different projects or environments. This approach not only improves efficiency but also supports best practices for managing Terraform state files. Terraform state is a critical component during IaC operations. Whether stored locally or on remote backends like Amazon Simple Storage Service (Amazon S3), state files map your real-world infrastructure and help Terraform determine the necessary changes based on your configurations. In our examples, we’ve kept a one-to-one relationship between configuration directories and state files—one state file per configuration directory. Consider a scenario where you need to create an EC2 instance in the AWS CA Central 1 region for an initial project, Project A. You can start with a simple configuration file located in a directory named Project A inside your Terraform Projects folder. For example:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
terraform apply in the Project A directory creates the EC2 instance and generates its corresponding state file.
Now, suppose you want to replicate this setup for a new project (Project B) with a different AMI ID and server name. The straightforward approach might involve creating a new directory for Project B, copying Project A’s configuration, and updating the AMI ID and tags. An example configuration combining both projects might look like this:
Terraform workspaces allow you to use one configuration directory to manage multiple projects (or environments) without duplicating files. This improves maintainability and prevents configuration drift.
Using Workspaces to Manage Environments
Terraform workspaces isolate state within the same configuration directory. To create a workspace for Project A, run:Parameterizing Your Configuration for Workspaces
To maintain a single configuration that dynamically adjusts for different projects in theca-central-1 region, create a variables.tf file. This file will define the region, instance type, and a map variable for AMI IDs—differentiating between Project A and Project B.
main.tf) to dynamically select values based on the current workspace. Here, the AMI is chosen using Terraform’s lookup function combined with the terraform.workspace variable, and the instance tag is automatically set to the workspace name:
Switching Workspaces and Applying Configuration Changes
After setting up your configuration files, runterraform plan to preview changes for the current workspace (for example, ProjectA):
terraform plan in the ProjectB workspace, Terraform will use the AMI and tag assigned to Project B:
terraform workspace select command. For example, to switch back to ProjectA:
terraform apply in each workspace, Terraform stores each workspace’s state in a separate subdirectory within the terraform.tfstate.d directory. For example:
Using workspaces ensures that changes in one environment (e.g., ProjectA) do not impact another (e.g., ProjectB), thanks to the isolated state files.