Problem
When using a version control system (VCS) or API-driven workflow in Terraform Enterprise, you cannot run interactive CLI commands like terraform import or terraform state rm directly against the workspace's state. These workflows are designed for non-interactive runs, which prevents direct state manipulation from a local machine.
This guide provides two methods to manually modify the state for a VCS or API-driven workspace.
Prerequisites
- Terraform CLI installed locally.
- Permissions to download and upload state files for the target Terraform Enterprise workspace.
- A Terraform API token with appropriate permissions if you intend to use the API method.
Solutions
There are two primary methods to accomplish this task. The first involves manually downloading and uploading the state file through the UI or CLI, while the second uses the API for the upload.
Solution 1: Manually Download and Upload the State File
This method involves temporarily converting your configuration to use a local backend to perform the state operations.
- Download the State File: Navigate to your workspace in the Terraform Enterprise UI, go to the States tab, and download the latest state file.
-
Prepare Local Configuration: Place the downloaded state file in your local Terraform configuration directory. Modify your configuration to remove the
cloudorremotebackend block so that Terraform will use the local state file by default. -
Initialize Terraform Locally: Run
terraform initin the directory containing your configuration and the downloaded state file. Terraform will initialize using the local state.$ terraform init
-
Modify the State: Perform the necessary state operations.
- To import a resource, use the
terraform importcommand. - To remove a resource, use the
terraform state rmcommand.
- To import a resource, use the
- Upload the Modified State: In the Terraform Enterprise UI, navigate back to the States tab of your workspace and lock it. Then, upload your modified local state file to create a new state version.
-
Restore Remote Backend: Re-add the
cloudorremotebackend block to your Terraform configuration. -
Re-initialize Terraform: Run
terraform initagain. Terraform will detect the remote backend configuration and connect to your Terraform Enterprise workspace. Your local changes are now reflected in the remote state.$ terraform init
Solution 2: Create a New State Version Using the API
This method is similar to the first but uses the Terraform Enterprise API to upload the modified state file, which can be useful for automation.
- Perform Local State Modifications: Follow steps 1 through 4 from Solution 1 to download the state file and modify it locally.
- Upload State via API: Instead of using the UI, use the Terraform Enterprise API to programmatically create a new state version from your modified local state file. For detailed instructions on this process, refer to the guide on creating a new state version using the API.
-
Restore and Re-initialize: Once the new state version is uploaded, restore your remote backend configuration and run
terraform initas described in steps 6 and 7 of Solution 1.