Terraform Workspace
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform expressions.
https://cloudaffaire.com/terraform-expressions/
In this blog post, we will discuss terraform workspace.
What is terraform workspace?
Each Terraform configuration has an associated backend that defines how operations are executed and where persistent data such as the Terraform state are stored.
The persistent data stored in the backend belongs to a workspace. Initially, the backend has only one workspace, called “default”, and thus there is only one Terraform state associated with that configuration.
Certain backends support multiple named workspaces, allowing multiple states to be associated with a single configuration. The configuration still has only one backend, but multiple distinct instances of that configuration to be deployed without configuring a new backend or changing authentication credentials. Below is the syntax of terraform workspace
1 |
terraform workspace |
Next, we are going to explain terraform workspace with a demo.
Terraform workspace:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
##-------------------------- ## Terraform: Workspace ## ##-------------------------- ## Create a directory and get inside it mkdir terraform && cd terraform ## List all workspaces terraform workspace list #Terraform starts with a single workspace named "default" which can't be deleted. ## Create a workspace terraform workspace new test ## List files (Terraform stores the workspace states in a directory called terraform.tfstate.d) ls #terraform created a new directory terraform.tfstate.d ls terraform.tfstate.d/ #terraform created a new directory test inside terraform.tfstate.d ## Switch to the test workspace terraform workspace select test #just for demo, already selected when created ## Varify that you are in test workspace terraform workspace show ## Create main.tf vi main.tf --------------- #creates a vpc variable "vpc_tag_name" { #variable type string type = "string" default = "myvpc" } provider "aws" { access_key = " secret_key = " region = "ap-south-1" } resource "aws_vpc" "myvpc" { cidr_block = "10.0.0.0/16" tags { Name = "${var.vpc_tag_name}_${terraform.workspace}" } } ---------------- :wq ## Format code terraform fmt ## Initialize terraform terraform init ## Create the resource terraform apply ## Check for state file ls #no .tfstate file created in root directory ls terraform.tfstate.d/test/ #instead tfstate file created under test directory ## Show state details terraform show #myvpc_test created ## Create a new workspce named dev terraform workspace new dev ## List files ls terraform.tfstate.d/ #terraform created a new directory dev inside terraform.tfstate.d ## Plan changes(workspace test state file is not visible in workspace dev, new vpc will be planned) terrafrom plan ## Create the resource (new vpc created though we did not change anything in main.tf) terraform apply ## Show state details terraform show #myvpc_dev created ## Cleanup terraform destroy #destroy the dev workspace resources terraform workspace select test #switch to test workspace terraform workspace delete dev #delete dev workspace terraform destroy #destroy the test workspace resources terraform workspace select default #switch to default workspace terraform workspace delete test #delete test workspace cd .. && rm -rf terraform #delete terraform directory |
Hope you have enjoyed this article. In the next blog post, we will discuss terraform backends.
To get more details on terraform, please refer below terraform documentation.
https://www.terraform.io/docs/index.html