DevOps Interview: Terraform Workspace & commands

Terraform Workspace:

Workspace is a feature provided by Terraform to help you isolate your Terraform configurations and state files. It allows you to manage multiple sets of infrastructure resources independently, each with its own distinct configuration and state. This is useful when working with different environments such as development, staging, and production environments or projects. It helps you to isolate changes and prevent conflicts.
Each workspace will have a separate state file. If you don’t specify the workspace, Terraform automatically creates a default workspace named “default”.

so, let’s see the benefits of using Workspace.

1. Isolation — Workspaces enable you to manage separate environments, such as development, staging, and production without affecting one another environments.This way you can have different configuration files and state files.

2. Collaboration — Workspace gives team members a chance to work simultaneously on different environments without interfering with each other’s configurations.
3. Testing — Workspace helps the team to test configuration in dev or staging before applying in the prod environment. This reduces the risk of introducing errors
into pro.
4. Version control — It means if you want to keep different versions of configuration then we can use Workspace to keep track of versions.

Let’s see the commands to manage Workspace-

1. terraform workspace new <name>: It will create new workspace.
e.g.- terraform workspace new prod

2. terraform workspace select <name>: if you want to jump to an existing workspace.
e.g. — terraform workspace select prod

Note: we also have “ terraform workspace switch <name> “ command for changing workspace. But there is a bit of a difference between “select” and “switch”.
terraform workspace select is used to set the currently active workspace without changing the state file, but terraform workspace switch is
set to the currently active workspace and changes the state file that is being used.
It means in the case of “terraform workspace select” , any changes you make to your configuration will still be applied to the state file for the
previously selected workspace. For “terraform workspace switch”, any changes you make to your configuration will be applied to the state file for newly selected workspace.

3. terraform workspace list: It will list all available workspaces.
e.g. — terraform workspace lists
output: dev
prod
staging

4. terraform workspace delete <name>: It will delete particular Workspace.
e.g. — terraform workspace deletes dev

5. terraform workspace show: This command shows the name of the currently selected workspace. It will show the workspace in which currently you are.
e.g. — terraform workspace show

6. terraform workspace current: This command displays the current workspace’s name and path. It provides more detailed information about the currently
selected workspace.
e.g. — terraform workspace current

Let’s see some high-level commands…

7. terraform workspace lock: it locks the state for the currently selected workspace. This prevents other users from making changes to the state.

8. terraform workspace unlock: it unlocks the state for the currently selected workspace.

9. terraform workspace import <workspace name> <source>: it imports a workspace from a source, such as a remote Terraform Cloud workspace.

10. terraform workspace export <workspace name> <destination>: it exports a workspace to a destination, such as a local directory.

That’s all. Pat yourself on the back. :)

Hope you enjoyed it. Don’t forget to like it.

0
Subscribe to my newsletter

Read articles from Aishwary Prakash directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aishwary Prakash
Aishwary Prakash

Hi Aspirants, my name is Aishwary Prakash and I'm a DevOps/ Site Reliability Engineer (SRE) with 4 years of experience. I'm passionate about using technology to solve real-world problems. In my current role, I'm responsible for the reliability, scalability, and performance of a large-scale cloud platform. I'm also interested in DevOps, monitoring, automation, fitness, cloud, and CI/CD. Some of the topics I write about include: SRE: I write about Site Reliability Engineering (SRE) best practices, tools, and technologies. DevOps: I write about DevOps practices, tools, and technologies that can help teams to deliver software more quickly and reliably. Monitoring: I write about monitoring tools and techniques that can be used to identify and resolve problems with software systems before they impact users. Automation: I write about automation tools and techniques that can be used to automate repetitive tasks, improve efficiency, and reduce errors. Fitness: I write about fitness tips, workouts, and nutrition advice that can help people to improve their overall health and well-being. Cloud: I write about cloud computing platforms, tools, and technologies that can be used to build and deploy scalable software systems. DevOps/SRE Interview: I write about DevOps/SRE related interview based on my experience and other professionals. I hope you enjoy my blog! Connect with me at aishwaryprakash98@gmail.com