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.tfstateThis 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-stateThis command initializes Terraform and migrates the state from the previously configured backend to the local backend.
Verify the local state:
terraform state listThis 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 initThis 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 listThis 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.tfstateComment out the backend block in your Terraform configuration file (e.g., main.tf).
Initialize Terraform with state migration:
terraform init -migrate-stateVerify the local state:
terraform state listCreate the new workspace:
terraform workspace new developmentSwitch to the new workspace:
terraform workspace select developmentInitialize Terraform in the new workspace:
terraform initImport 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 moveVerify 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 defaultbackend 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.
Moving resources from the default workspace to a new one on ... | If you started to use terraform as you learning it or were managing just one environment and now need to have multiple environments chances are you will need to move your resources from the default workspace to another one with a better name for each environment.
Migrate your Terraform State to Workspaces | by Andris Roling ... | When starting with Terraform most of you will most likely create your own local Terraform state. I will explain how to improve local stateā¦
How To Terraform Migrate State from One Workspace to Another ... | Need a Terraform migrate state from one workplace to another? Follow these easy steps to successfully migrate your state files.
How to change the name of a workspace - Terraform - HashiCorp ... | Hi all I have made a terrible choice naming my workspace, and would like to move all resources in it to a workspace with a better name. Is there a short way of doing this? My backend is S3, there is no state locking. I have considered, but not actually done, the following: backup the state (terraform state pull) ā then what? import using legacy method? import resources the hard way š ā I really donāt want to do that. What are you state ninjas doing when you need to move resources to ...
State: Workspaces | Terraform | HashiCorp Developer | The configuration still has only one backend, but you can deploy multiple distinct instances of that configuration without configuring a new backend or changingĀ ...
How to Migrate Terraform State Between Different Backends | Migrate your Terraform state to a remote backend like Azure Storage Account or Amazon S3 and back to local. Init reconfigure vs migrate-state.