Separating development and production environment in terraform

Today, we are going to work on how we can separate development and production environments.

Terraform recently introduced a feature called Workspaces. This feature allows you to separate your dev environment from other environments like corp and prod.

By default terraform creates a workspace named “default”. In order to create a new workspace, after you run “terraform init” run

terraform workspace list

This command will show you the list of Terraform workspace. To create a new workspace,

terraform workspace new <workspace name>

This will create a local directory “terraform.tfstate.d” directory where the state file for your workspace would be stored.

To select a workspace that you created, run the following command

terraform workspace select <workspace name>

What if there are multiple environments and multiple people working on terraform?

Here terraform backend and remote states comes to the rescue.

You can see how to use remote state from this article

Once you followed this article and created an S3 bucket, create a folder named “env:” and now run terraform init and create workspace using the following command

terraform workspace list

terraform workspace new <workspace name>

terraform workspace select <workspace name>

For this article, I will create 3 environments (dev, corp, prod)

so first I will create a backend with a remote state

<script src=”https://gist.github.com/pgaijin66/dbe5341046f41ac04a864cf22b3428ed.js"></script>

Then create workspaces

terraform init

terraform workspace new dev

terraform workspace new corp

terraform workspace new prod

Now, if you check in the s3 bucket env: folder, you can see the workspaces and state files stored.

--

--

--

DevOps / SRE Engineer. Blog: 99devops.com System admin turned SRE. I love Linux.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using the Kafka REST API with Go

Windows 10 Netstat -tunlp

Backend With Rails

Vectorized: free and source available, cloud native infrastructure for real-time applications

Monitoring Spring boot applications using actuator and prometheus

Build it with Microsoft Flow: Get a notification when a “watch word” is mentioned in Yammer

Access Oracle DB with java and integration with the Test framework

First Steps into Software Engineering — Internship Journey at Jublia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prabesh Thapa

Prabesh Thapa

DevOps / SRE Engineer. Blog: 99devops.com System admin turned SRE. I love Linux.

More from Medium

Workaround to run terraform after macOS Catalina/Monterey update

Using Dynamic Blocks in Terraform

Manage CDN(Akamai) as a Code using Github actions and terraform

Generate SSH key using terraform for multiple environments