Imagine you living in the same house but in that house, different rooms have different type of settings. Or you remember the concept of multiple windows desktops. On the same OS, you may have different desktop to choose from and each desktop would have different application icons and different wallpapers and themes.
Terraform Workspace concept is exactly like that. You can create different workspaces to manage different environments, however, all the workspaces would be using the same set of code (same configuration files) and we can still customize resources depending on which workspace you are in.
Example: You can create Dev, Stage and Prod workspaces. Then depending on which workspace, you are in at the moment, attribute values like instance_type would change. As an example, if you are in dev workspace and you plan to build an ec2 instance then ‘t2.micro’ instance would be created and if you are in Prod workspace, then ‘t2.large’ instance_type would be created.
Below diagram shows the same thing that using the same set of configuration files i.e. we don’t have to change the code, depending on which workspace you are in like Development, Staging or Production, resources would be created
Note: Each workspace would be maintaining its own .tfstate file. This means separate state file for each environment/workspace.
By default there is a pre-created default workspace, which we can not delete. If you do not create any workspace and you write a code and you run terraform apply command, .tfstate file would be maintained in the default workspace.
Terraform Workspace Commands
- terraform workspace new <workspace name> : This command would create a new terraform workspace
- terraform workspace select <workspace name> : This command would let us switch to the workspace of our choice
- terraform workspace list: This command shows the list of available workspaces. If there is an asterisk (*) sign next to the workspace name then it would mean that we are currently in that workspace.
- terraform workspace show: This command show which workspace we are in currently.
- terraform workspace delete <workspace name>: This command would delete the workspace that we wish to delete. Note: You can not delete the workspace you are already in. It is similar to that you can not demolish a house if you are already sitting in the living room. So to delete a specific workspace, you need to switch to some other workspace and then delete.
Let us do it practically
We will create 2 workspaces i.e. dev and prod. We have to create EC2 instance.
Goal:
- If we are in dev workspace then then the instance_type would be t2.micro.
- If we are in prod workspace then instance_type would be t2.large
We will create 2 workspaces i.e. dev and prod.
Command: Terraform workspace list – Provides list of all the available workspaces. Asterisk (*) denotes which workspace we are currently in.
Command: Terraform workspace show – Shows the name of workspace that we are currently in
We switched to dev workspace, as shown below
Let’s write the code now.
In the below screenshot, we have created a map variable type where in we have assigned values ‘t2.micro’ and ‘t2.large’ to keys ‘dev’ and ‘prod’ respectively.
Now we write code to create EC2 instance and in the instance_type attribute, we have used lookup function
lookup retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.
Lookup(map,key)
Note: terraform.workspace retrieves the name of workspace you are in. We have used it in lookup function to provide that name as key.
Now, because we are in ‘dev’ workspace and if we run terraform plan
So we got the results as per the expectations. Now we will run terraform apply
Now, lets switch the workspace to prod.
We will now run terraform plan and since we are in prod workspace, we should see the instance_type as t2.large
Lets run terraform apply
So it’s a success.
Since we have run terraform apply, we should be able to see state (.tfstate) files of each environment.
Conditional Expression
We can use terraform.workspace (returns which workspace we are in) in conditional expressions and assign the results, as shown below
high_availability = (terraform.workspace == “prod”) ? true : false
count = (terraform.workspace == “dev”) ? 1 : 0
Important links:
https://www.terraform.io/language/functions/lookup
Hope you enjoyed reading today.