DEV Community

Ileriayo Adebiyi
Ileriayo Adebiyi

Posted on

Managing Multiple Environments With Terraform

Managing Multiple Environments With Terraform

If you want to manage multiple environments with Terraform e.g., Dev, Stage, and Prod, you can use either:

  • workspaces or
  • git branches (dev, stage, prod)

But there's a third. Just before that:

  1. Workspaces seem good at first:
  2. native to terraform, no new tool to learn; simply create and switch workspaces on the same terraform config codebase: > terraform workspace new > terraform workspace list > terraform workspace select
  • this means code maintenance is a breeze, with no code duplication

However, when it comes to using a remote backend. It might be a mess. With older versions of terraform, you could only use a single backend for all workspaces. Not exactly sure if this is the case for the latest terraform version.

Also, code (terraform module) versioning could pose a challenge. How do you promote module versions from dev to staging to prod workspace without breaking prod (since it's just one codebase right?). Variables you may say, unfortunately, variables cannot be used everywhere. Scary!

  1. Using git branches solves the problem of versioning, since you can have a version in the dev branch, another in the staging branch, and another in the prod (main) branch.

However, as the codebase grows, maintenance becomes a nightmare. Too error-prone and bug-welcoming

The third option is Terragrunt, which captures the best of both worlds and adds a bonus of concurrency: terraform apply on multiple environments at the same time.

This lovely blog post series from Yevgeniy Brikman was a delightful and thought-provoking one for me. https://blog.gruntwork.io/how-to-manage-multiple-environments-with-terraform-32c7bc5d692

Have you worked with Terraform environments before? What are your thoughts?

Read the thoughts of others on the original LinkedIn and Twitter posts:

LinkedIn: https://www.linkedin.com/posts/ileriayoadebiyi_how-to-manage-multiple-environments-with-activity-7019665054975287296-4ORD
Twitter: https://twitter.com/Ileriayooo/status/1613901213744914432

Cheers!

Top comments (0)