Learn how to efficiently migrate your Terraform state between workspaces for improved workflow and collaboration.
This guide outlines the steps to move your Terraform state and resources to a new workspace. This process is particularly useful when you want to restructure your project or isolate resources for different environments.
Create a local backup of your Terraform state:
terraform state pull > default.tfstate
This will save the current state of your default workspace to a file named default.tfstate
.
Switch to using a local backend:
backend
block in your Terraform configuration file. This will temporarily disable the remote backend and make Terraform use a local backend.Initialize Terraform with state migration:
terraform init -migrate-state
This command initializes Terraform and migrates the state from the previously configured backend to the local backend.
Verify the local state:
terraform state list
This command should list all the resources managed by Terraform in the local state.
Create a new workspace (if needed):
terraform workspace new <new_workspace_name>
Replace <new_workspace_name>
with the desired name for your new workspace.
Switch to the new workspace:
terraform workspace select <new_workspace_name>
Initialize Terraform in the new workspace:
terraform init
This will initialize Terraform in the new workspace and create an empty state file.
Import the resources from the local state:
terraform import
command:
terraform import <resource_type>.<resource_name> <resource_id>
<resource_type>
with the type of resource (e.g., aws_instance
).<resource_name>
with the name of the resource in your Terraform configuration.<resource_id>
with the actual ID of the resource in your cloud provider.Verify the state in the new workspace:
terraform state list
This should now list all the imported resources in the new workspace.
(Optional) Clean up the default workspace:
terraform destroy
.terraform workspace delete default
.backend
block in your Terraform configuration file.terraform init
to reinitialize Terraform with the remote backend.This code provides a step-by-step guide for migrating Terraform resources from a default workspace with a remote backend to a new workspace with a local backend. It involves backing up the state, commenting out the backend configuration, initializing Terraform with state migration, creating a new workspace, importing resources from the local state, and optionally cleaning up the default workspace and switching back to the remote backend. The code includes commands for each step and emphasizes the importance of backing up state, using correct resource IDs, and consulting provider documentation.
This example demonstrates migrating resources from a "default" workspace with a remote backend to a new workspace named "development" using a local backend temporarily.
Assumptions:
Steps:
Backup your Terraform state:
terraform state pull > default.tfstate
Comment out the backend
block in your Terraform configuration file (e.g., main.tf
).
Initialize Terraform with state migration:
terraform init -migrate-state
Verify the local state:
terraform state list
Create the new workspace:
terraform workspace new development
Switch to the new workspace:
terraform workspace select development
Initialize Terraform in the new workspace:
terraform init
Import resources from the local state:
Important: Replace placeholders with your actual resource types, names, and IDs.
# Example for an AWS instance
terraform import aws_instance.example i-0123456789abcdef0
# Repeat for each resource you want to move
Verify the state in the new workspace:
terraform state list
(Optional) Clean up the default workspace:
terraform workspace select default
terraform destroy -auto-approve
terraform workspace delete default
backend
block in your Terraform configuration file.terraform init
to reinitialize Terraform with the remote backend.Important Notes:
terraform destroy
, especially in the default workspace. Ensure you have a backup of your state and understand the potential impact before deleting any resources.This guide summarizes the steps to move your Terraform resources to a new workspace, with the option to also switch to a new backend.
1. Backup and Localize:
terraform state pull > default.tfstate
backend
block in your configuration.terraform init -migrate-state
terraform state list
2. Create and Populate the New Workspace:
terraform workspace new <new_workspace_name>
terraform workspace select <new_workspace_name>
terraform init
terraform import <resource_type>.<resource_name> <resource_id>
terraform state list
3. Optional Cleanup and Backend Switch:
terraform destroy
terraform workspace delete default
backend
block in your configuration.terraform init
This process allows you to safely move your Terraform resources to a new workspace, providing a clean slate for your infrastructure management.
By following these steps, you can effectively move your Terraform state and resources to a new workspace, enabling better project organization and environment separation. Remember to adapt the commands and procedures to your specific cloud provider and backend configuration, and always prioritize backing up your state before making any significant changes.