How to Create Multiple Environments Using Terraform Workspaces - Virtual Network Creation Part 2 [Final]
Welcome back! Let's pick up where we left off. Now that we have all the necessary Terraform files, we can apply the configuration to create and update the infrastructure.
Terraform Init
First, comment out the backend.tf and apply terraform init. We need to do this first before we can actually create the workspace
terraform init

Create Terraform Workspaces
Start by creating a different workspace for each environment. Use the below command to create a workspace for prod. Entering the below command should automatically switch to the workspace.
terraform workspace new prod
Next, uncomment the back-end and then re-initialise the backend using Terraform init. We add a flag -var-file to target the production environment
This allows you to apply the same Terraform configuration to different environments with different variable values without the need to modify configuration files.
terraform init -var-file="prod.tfvars"

Terraform Plan & Apply
Ok great, now that Terraform is initialised you can either run a Terraform Plan or Terraform apply. Since I know my config is going to work, I'll just run a Terraform Apply. Use the below command. Don't forget the -var-file flag
terraform apply -var-file="environments/prod.tfvars"

Switch workspace
Looking good! Now all that's left is to apply the non-production environment. We can do this by using Terraform workspace command.
terraform workspace new nonprod

All that's left is to apply the config. Use terraform apply command previously to do this. Ensure you change the var file from prod.tfvars to nonprod.tfvars
terraform apply -var-file="environments/nonprod.tfvars"

Errors you may encounter
You may get an error like below as I did. This looks like a delay on Azure's side when creating the resource group. I'm sure there is a way around this but for now, simply re-enter the "terraform apply" command again and it should work.
│ Error: creating/updating Network Security Group: (Name "nsg3_ause" / Resource Group "terraform-examples-group2"): network.SecurityGroupsClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ResourceGroupNotFound" Message="Resource group 'terraform-examples-group2' could not be found."
│
│ with azurerm_network_security_group.this["nsg3_ause"],
│ on main.tf line 35, in resource "azurerm_network_security_group" "this":
│ 35: resource "azurerm_network_security_group" "this" {

Verify the resources and backend
Navigate to the portal and check your resources have been created




Now navigate to the storage account where your backend is. You will notice you have 2 state files, one for the prod workspace and the nonprod workspace

Once you have finished with the resources you can destroy the resources using the below
terraform workspace select nonprod
terraform destroy -var-file="environments/nonprod.tfvars"
terraform workspace select prod
terraform destroy -var-file="environments/prod.tfvars"

Congratulations, you have successfully deployed 2 environments using a singular backend.
Found this article useful? Why not buy Phi a coffee to show your appreciation.