DEV Community

se-piyush
se-piyush

Posted on

Managing Multiple Environments with Terraform Workspaces

Today, I’ll be discussing Terraform workspaces, a powerful feature that helps manage multiple environments using Terraform. Imagine you’ve deployed resources in your cloud using Terraform, and now there’s a requirement to have two environments: a production environment that you just deployed and a development environment. Terraform workspaces can help you manage these environments efficiently.

The Challenge

When you deploy resources using Terraform, a state file (terraform.tfstate) is generated. This state file maintains the configuration of the deployed resources. To manage multiple environments, you need to maintain more than one state file. Terraform workspaces provide a way to manage multiple state files within a single Terraform configuration.

Introducing Terraform Workspaces

Terraform workspaces allow you to manage different sets of infrastructure within the same configuration by maintaining separate state files for each workspace. Here’s how you can use Terraform workspaces to manage your production and development environments.

Steps to Use Terraform Workspaces

  1. Create a Production Workspace: First, create a new workspace for your production environment using the following command:
   terraform workspace new prod
Enter fullscreen mode Exit fullscreen mode

After executing this command, a folder named terraform.tfstate.d is created, containing a subfolder named prod. This folder will hold the state file for the production environment.

  1. Migrate Existing State File to Production Workspace: Move your existing state file to the newly created prod folder:
   mv terraform.tfstate terraform.tfstate.d/prod/terraform.tfstate
Enter fullscreen mode Exit fullscreen mode

This command migrates your current state file to the production workspace.

  1. Create a Development Workspace: Next, create a new workspace for your development environment:
   terraform workspace new dev
Enter fullscreen mode Exit fullscreen mode

This command creates a dev folder inside the terraform.tfstate.d directory. The development environment’s state file will be maintained here.

  1. Select the Development Workspace: To start working in the development environment, select the dev workspace:
   terraform workspace select dev
Enter fullscreen mode Exit fullscreen mode
  1. Deploy Resources in the Development Workspace: Now, you can deploy resources in the development environment. When you run the terraform apply command, Terraform will use the state file in the dev folder to manage the development resources:
   terraform apply
Enter fullscreen mode Exit fullscreen mode

Switching Between Workspaces

To switch between different environments, you simply need to select the appropriate workspace. For example, to switch back to the production environment:

terraform workspace select prod
Enter fullscreen mode Exit fullscreen mode

And to switch to the development environment:

terraform workspace select dev
Enter fullscreen mode Exit fullscreen mode

Benefits of Using Terraform Workspaces

  • Isolation: Each workspace maintains its own state file, ensuring that the environments are isolated from each other.
  • Convenience: Workspaces make it easy to switch between different environments without needing separate configuration files or directories.
  • Scalability: Workspaces allow you to manage multiple environments (e.g., staging, testing) in addition to production and development.

Conclusion

Terraform workspaces provide an elegant solution for managing multiple environments with separate state files. By following the steps outlined above, you can efficiently manage your production and development environments using Terraform workspaces. This approach not only keeps your environments isolated but also makes it easier to switch between them and manage infrastructure as code.

Top comments (0)