Terraform workspaces – Infrastructure as Code (IaC) with Terraform-2

To access the name of the workspace, Terraform provides the terraform.workspace variable, which we have used to define the resource_group name. So, the template is now ready to take configuration for any environment, and we will have a separate resource group for each environment.

Also, update the backend.tf file with the tfstate container name we created in the last section and initialize the Terraform workspace by using the following command:
$ terraform init

Now, once Terraform has initialized, let’s create a dev workspace by using the following command:
$ terraform workspace new dev
Created and switched to workspace “dev”!

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

So, as we’re in a new, empty workspace called dev, let’s run a plan.
Use the following command to run a plan on the dev environment:
$ terraform plan -out dev.tfplan
Acquiring state lock. This may take a few moments…

Terraform will perform the following actions:
resource “azurerm_network_interface” “main” {

}

  • resource “azurerm_resource_group” “main” {
  • id            = (known after apply)
  • location = “westeurope”
  • name        = “terraform-ws-dev”
    }
  • resource “azurerm_subnet” “internal” {

    }
    resource “azurerm_virtual_machine” “main” {

    }
    resource “azurerm_virtual_network” “main” {

    }

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

Now, let’s go ahead and apply the plan using the following command:
$ terraform apply “dev.tfplan”
Acquiring state lock. This may take a few moments…
azurerm_resource_group.main: Creating…
azurerm_virtual_network.main: Creating…
azurerm_subnet.internal: Creating…
azurerm_network_interface.main: Creating…
azurerm_virtual_machine.main: Creating…

Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
Releasing state lock. This may take a few moments…

As the dev plan has been applied and the resources are created in the dev resource group, let’s create a workspace for testing:
$ terraform workspace new test

As the new workspace is created, let’s run a plan on the test workspace using the following command and save it to the test.tfplan file:
$ terraform plan -out test.tfplan

  • resource “azurerm_resource_group” “main” {
  • id            = (known after apply)
  • location = “westeurope”
  • name        = “terraform-ws-test”
    }

As we can see, the resources will be created in the terraform-ws-test resource group. So, let’s go ahead and apply the plan using the following command:
$ terraform apply test.tfplan

The test plan has been applied as well. Now let’s go ahead and inspect the created resources.

Tip

Terraform workspaces are ideal for maintaining separate infrastructure configurations for different environments, such as development, staging, and production. This helps prevent accidental configuration changes and ensures consistent setups.

Leave a Reply

Your email address will not be published. Required fields are marked *