Hugo Future Imperfect Slim

Shashi Shetty

Opinions Are My Own.

Infrastructure as code using Terraform - Part 2

Series of blog posts on Terraform.

Shashi Shetty

3-Minute Read

Terraform Workspace

Terraform Workspace provides you an option to create dedicated runtime stacks for each of your environments. For instance, the requirement is to create a Resource group based on project code for UAT and Prod environments. One of the ways to achieve this is using terraform workspace with a single code.

A Workspace is created using terraform workspace new workspace_name. To list the workspace, you can use the “list” option. * indicates the current workspace. To switch to a different workspace, use the “select” option.

PS C:\content-management\blog> terraform workspace new prod
Created and switched to workspace "prod"!

You're now on a new, empty workspace. Workspaces isolate their state,   
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.

PS C:\content-management\blog> terraform workspace list    
* prod   

In this example, I am creating the resource group based on the workspace, i.e., Prod and UAT. I am using the local variable to achieve this. The result would be two resource groups Prod-XYZ and UAT-XYZ . The sample code is enclosed below.

variable "ProjectCode" {
type = string
default = "XYZ"
variable "location" {
type = string
default = "eastus"
locals {
RG = "${terraform.workspace}-${var.ProjectCode}"

Code for actual resource creation based on Workspace .

resource "azurerm_resource_group" "RGname" {
name = local.RG
location = var.location

Result post running the code in Prod and UAT workspace

PS D:\Terraform\blogLabs\02-workspace> terraform apply .\uat.tfplan
azurerm_resource_group.RGname: Creating...
azurerm_resource_group.RGname: Creation complete after 3s [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path   
below. This state is required to modify and destroy your      
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate
PS D:\Terraform\blogLabs\02-workspace> 

PS D:\Terraform\blogLabs\02-workspace> terraform show
# azurerm_resource_group.RGname:
resource "azurerm_resource_group" "RGname" {
    id       = "/subscriptions/XXXX/resourceGroups/UAT-XYZ"
    location = "eastus"
    name     = "UAT-XYZ"
    tags     = {}
PS D:\Terraform\blogLabs\02-workspace> Get-AzResourceGroup *XYZ | select ResourceGroupName


When you use destroy command within a workspace, all resources created in that stack are deleted. In the below example, I have run the destroy command on the UAT workspace.

PS D:\Terraform\blogLabs\02-workspace> terraform workspace select UAT         
Switched to workspace "UAT".
PS D:\Terraform\blogLabs\02-workspace> terraform destroy             
azurerm_resource_group.RGname: Refreshing state... [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # azurerm_resource_group.RGname will be destroyed
  - resource "azurerm_resource_group" "RGname" {
      - id       = "/subscriptions/XXXX/resourceGroups/UAT-XYZ" -> null
      - location = "eastus" -> null
      - name     = "UAT-XYZ" -> null
      - tags     = {} -> null

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources in workspace "UAT"?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

azurerm_resource_group.RGname: Destroying... [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ]
azurerm_resource_group.RGname: Still destroying... [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ, 10s elapsed]
azurerm_resource_group.RGname: Still destroying... [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ, 20s elapsed]
azurerm_resource_group.RGname: Still destroying... [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ, 30s elapsed]
azurerm_resource_group.RGname: Still destroying... [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ, 40s elapsed]
azurerm_resource_group.RGname: Still destroying... [id=/subscriptions/XXXX/resourceGroups/UAT-XYZ, 50s elapsed]
azurerm_resource_group.RGname: Destruction complete after 51s

Destroy complete! Resources: 1 destroyed.

PS D:\Terraform\blogLabs\02-workspace> Get-AzResourceGroup *XYZ | select ResourceGroupName


Say Something


Nothing yet.

Recent Posts



Dependable and goal-oriented IT infrastructure engineer with 12 years of experience in designing and delivering infrastructure projects.