Terraform - Workspaces

In previous post we have discussed about maps and lookups and we used the example of environment variable (type = map) dev or prod where main file getting these variable by lookups and we discussed "tfstate" which has all the information of the current environment and is one the reason we should use workspaces.

So what is workspaces , well workspaces is nothing but the way Terraform differentiate between the efstate files as per the  environment created in the above discussed example.

Let me explain a bit - when we select dev environment Terraform created dev and if we select prod it would overwrite the Dev resources to prod , check below pic when we choose prod after creating dev environment it says "forces new resources"




hence workspaces, which can be created by simple command and keeps the "tfstate" file for both the environmental variables in separate folders. Below are the commands and snippet

To create workspace

terraform workspace new  <name>

To select workspace :

terraform workspace select <name>




You can see the commands run to create the workspace and at the left side you can see Terraform.tfstate folder got created which has 2 separate folders and tfstate file stored for the both the environment variables. This could help you and you need not to move the "tfstate" file and keep monitoring rather create workspace and only need to take care from which workspace you are running commands and select the correct workspace. 


No comments:

Post a Comment

MS Defenders

 Microsoft Defender offers a wide range of security solutions, similar to the ones we've discussed (Defender for Containers, Defender fo...