DEV Community

Srinivasulu Paranduru for AWS Community Builders

Posted on • Updated on

Terraform - Workspaces & Demos

Workspaces in the Terraform CLI refer to separate instances of state data inside the same Terraform working directory.

  • Terraform relies on state to associate resources with real-world objects.
  • When you run the same configuration multiple times with separate state data, Terraform can manage multiple sets of non-overlapping resources.

Under a folder TF_AWS having below mentioned .tf files
TF_AWS (folder)

  • data.tf
  • main.tf
  • provider.tf
  • variable.tf
  • terraform.tfvars

Image description

1.Create a folder TF_AWS
2.Create a new file main.tf under the folder TF_AWS and copy the below mentioned code

resource "aws_instance" "myfirstec2" {
  ami           =  data.aws_ami.app_ami.id 
  instance_type =  "t2.micro"
}

Enter fullscreen mode Exit fullscreen mode

3.Create a new file provider.tf under the folder TF_AWS and copy the below mentioned code

provider "aws" {
  region     = "eu-west-1"
}
Enter fullscreen mode Exit fullscreen mode

4.Create a new file data.tf under the folder TF_AWS and copy the below mentioned code

data "aws_ami" "app_ami" {
   most_recent =true
   owners = ["amazon"]
    filter{
     name = "name"
     values = ["amzn2-ami-hvm*"]
   }
}


Enter fullscreen mode Exit fullscreen mode

5.Create a new file variable.tf under the folder TF_AWS and copy the below mentioned code

variable "instance_type" {
  type = string
  description = "instance type"
}

Enter fullscreen mode Exit fullscreen mode

6.Create a file terraform.tfvars under the folder TF_AWS and copy the below mentioned code

instance_type = "t2.micro"
Enter fullscreen mode Exit fullscreen mode

Run the terraform command terraform workspace then will get the following message

Usage: terraform workspace
new, list, show, select and delete Terraform workspaces.

terraform workspace list
*default

Note : Initially with out creating any workspaces, default is selected and its highlighted with * symbol

  • Creating a new workspace dev by the running the below command

terraform workspace new dev

Image description

  • Newly created workspace will be selected by default

Image description

  • When a workspace is created initially then following folders will be created under the base folder

  • terraform.tfstate.d

    • dev
  • Similarly create new test,prod workspaces

Image description

  • test and prod folders will be created under terraform.tfstate.d folder

  • terraform.tfstate.d

    • dev
    • test
    • prod

Image description

Note :

  • If any workspaces related any environment deleted using the below command, then the following environment folder will be deleted under the folder terraform.tfstate.d
  • Cant delete the workspace selected, if still wanted to deleted the current workspace then switch to other workspace and delete the same
terraform workspace delete prod
Enter fullscreen mode Exit fullscreen mode
  • Select the dev workspace terraform workspace select dev

Then the following the terraform commands

  • terraform init
  • terraform plan
  • terraform approve --auto-approve

Image description

  • Separate terraform.tfstate file will be created under the dev folder terraform.tfstate.d\dev\terraform.tfstate
  • Select the dev workspace terraform workspace select test Then the following the terraform commands
  • terraform init
  • terraform plan
  • terraform approve --auto-approve

`PS C:\Terraform_Training\TF_AWS> terraform workspace select test
Switched to workspace "test".
PS C:\Terraform_Training\TF_AWS> terraform apply --auto-approve

PS C:\Terraform_Training\TF_AWS> terraform apply --auto-approve
aws_instance.myfirstec2: Creating...
aws_instance.myfirstec2: Still creating... [10s elapsed]
aws_instance.myfirstec2: Still creating... [20s elapsed]
aws_instance.myfirstec2: Still creating... [30s elapsed]
aws_instance.myfirstec2: Creation complete after 31s [id=i-0274011109b4a4e04]
`

  • Separate terraform.tfstate file will be created under the test folder `terraform.tfstate.d\test\terraform.tfstate

Image description

  • If you are trying to delete the current selected workspace 'test' ` PS C:\Terraform_Training\TF_AWS> terraform workspace delete test Workspace "test" is your active workspace.

You cannot delete the currently active workspace. Please switch
to another workspace and try again.`

  • Switch to workspace 'dev' terraform workspace select dev
  • Now try to delete 'test' workspace terraform workspace select test

PS C:\Terraform_Training\TF_AWS> terraform workspace delete test
Workspace "test" is not empty.
Deleting "test" can result in dangling resources: resources that
exist but are no longer manageable by Terraform. Please destroy
these resources first. If you want to delete this workspace
anyway and risk dangling resources, use the '-force' flag.

Note :

  1. Cant able to delete a workspace having resources and best way is to destroy the resources for the selected workspace then destroy the workspace
  2. If you try with the below option for the 'test' workspace and for which resources exists

terraform workspace delete -force test

Deleted workspace "test"!
WARNING: "test" was non-empty.
The resources managed by the deleted workspace may still exist,
but are no longer manageable by Terraform since the state has
been deleted.

It has deleted the 'test' workspace but the resources still existing and those resources needs to be deleted manually through aws console

Image description

Note: Destroy the resources under dev and prod if any existing then delete the workspaces
PS C:\Terraform_Training\TF_AWS> terraform workspace list
default

  • dev prod

PS C:\Terraform_Training\TF_AWS> terraform destroy --auto-approve
aws_instance.myfirstec2: Destroying... [id=i-0546cf22debe05002]
aws_instance.myfirstec2: Still destroying... [id=i-0546cf22debe05002, 10s elapsed]
aws_instance.myfirstec2: Still destroying... [id=i-0546cf22debe05002, 20s elapsed]
aws_instance.myfirstec2: Still destroying... [id=i-0546cf22debe05002, 30s elapsed]
aws_instance.myfirstec2: Still destroying... [id=i-0546cf22debe05002, 40s elapsed]
aws_instance.myfirstec2: Destruction complete after 40s

PS C:\Terraform_Training\TF_AWS> terraform workspace select prod
Switched to workspace "prod".
PS C:\Terraform_Training\TF_AWS> terraform workspace delete dev
Deleted workspace "dev"!
PS C:\Terraform_Training\TF_AWS> terraform workspace select default
Switched to workspace "default".
PS C:\Terraform_Training\TF_AWS> terraform workspace delete prod
Deleted workspace "prod"!
PS C:\Terraform_Training\TF_AWS> terraform workspace list

  • default

  • Now all the workspaces created by me are destroyed

  • Recheck the folders inside terraform.tfstate.d are deleted

Image description

Command Description Usuage
workspace list The terraform workspace list command is used to list all existing workspaces. terraform workspace list [DIR]
workspace select The terraform workspace select command is used to choose a different workspace to use for further operations. terraform workspace select NAME [DIR]
workspace new The terraform workspace new command is used to create a new workspace. terraform workspace new [OPTIONS] NAME [DIR]
workspace delete The terraform workspace delete command is used to delete an existing workspace. terraform workspace delete [OPTIONS] NAME [DIR]
workspace show The terraform workspace show command is used to output the current workspace. terraform workspace show

Conclusion : Discussed about terraform workspace and created multiple workspaces and deleted
💬 If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it 😀 and follow me in dev.to , linkedin and buy me a coffee

Top comments (0)