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.