Workspaces...¶

Terraform workspaces allow you to manage multiple instances of your infrastructure within a single configuration. Workspaces are particularly useful when you have similar environments (e.g., development, staging, production) that share the same Terraform configuration but need separate state and resources. Each workspace maintains its own state file, allowing you to apply changes independently.
Here's an overview of Terraform workspaces:
Usage¶
-
Create a Workspace:
This command creates a new workspace and switches to it. -
List Workspaces:
Lists all available workspaces. -
Select a Workspace:
Switches to an existing workspace.
Workspaces in Terraform Configuration¶
In your Terraform configuration, you can use variables to customize resources based on the current workspace.
Example in main.tf:
resource "aws_instance" "example" {
ami = var.workspace == "dev" ? "ami-12345678" : "ami-87654321"
instance_type = "t2.micro"
count = var.workspace == "prod" ? 3 : 1
}
In this example, the AMI and instance count are conditionally set based on the current workspace.
Folder Structure with Workspaces¶
Consider structuring your project to accommodate multiple workspaces, such as:
.
├── main.tf
├── variables.tf
├── outputs.tf
├── modules/
│ ├── ...
└── environments/
├── dev/
│ ├── main.tfvars
│ └── variables.tfvars
├── prod/
│ ├── main.tfvars
│ └── variables.tfvars
└── staging/
├── main.tfvars
└── variables.tfvars
Each environment folder contains its own Terraform variables file and can have workspace-specific configurations.
Terraform Commands with Workspaces¶
-
Initialize Workspace:
-
Plan and Apply Changes:
-
Destroy Resources:
Switching Between Workspaces¶
-
Switch to Another Workspace:
-
Create and Switch to a New Workspace:
Summary¶
Terraform workspaces provide a convenient way to manage and isolate different instances of your infrastructure within the same codebase. They help streamline configuration management for various environments and reduce duplication in your Terraform code.