I just had the same need to revert from Cloud to local. Besides removing the cloud block from main.tf. I also delete both environment and terraform.tfstate from the .terraform directory within your project.
Because of this limitation in the cloud backend, I prefer to continue using the old remote backend with TFC, which does not suffer from this limitation.
I’m sorry I didn’t see this when it was originally posted.
The Cloud integration doesn’t know how to automatically migrate to a plain backend, but if your intention is to migrate to local state then that is possible with one additional manual step before you remove the cloud block:
terraform state pull >terraform.tfstate
Assuming you run this before you remove the cloud block, this will retrieve the latest state snapshot for your currently-selected workspace and write it into the terraform.tfstate file, which happens to be the default local filename for the local backend.
The terraform state pull command when using the Terraform Cloud integration is effectively using the API in just the same way as mentioned in the error message, but without the need for you to write any custom integration code.
There are some caveats here:
This retrieves a state snapshot for only the currently-selected Terraform Cloud workspace. In the example in the original question there is only one workspace called provisioners and so that’s presumably fine, but if you have multiple workspaces you’ll need to do something more involved because multiple workspaces in Terraform Cloud don’t map well onto multiple workspaces for other backends. (This is one reason why the Terraform Cloud integration cannot migrate this automatically.)
This will leave the same state snapshot still active in Terraform Cloud, so you’ll need to make sure nobody else on your team uses any version of the configuration that still has the cloud block present once you’ve switched to local, or you’ll end up with a “forked state” situation where both Terraform Cloud and the local file have different snapshots that both think they are the latest snapshot.
If you referring to the terraform.workspace symbol anywhere in your configuration, after migration that will return default because terraform.tfstate is the state snapshot for the implicit default workspace. In Terraform cloud that would’ve returned provisioners instead, because that was the name of this workspace in Terraform Cloud.
If you intend to migrate to a backend other than the local backend you can also use the above technique and then perform a few additional steps:
Make sure you ran terraform init after you removed the cloud block so that Terraform CLI knows it’s now using the local backend for state storage.
Add a new backend block to your configuration to select a different backend type to use.
Run terraform init again, at which point Terraform CLI should notice that the configured backend differs from the working directory’s currently-selected backend and thus activate the state migration process to automatically copy your local state file into the default workspace of the newly-selected backend.
Hi @apparentlymart
I have been facing the same problem… but I am following your documentations:
I executed the command
git remote set-url origin YOUR_REMOTE
where YOUR_REMOTE is the url of my git project.
Then, I removed the block Cloud from the terraform file.
I pushed new file on git and finally I created a pull request to merge the main branch to the one linked to Terraform cloud.
That is the moment I get the error.
If I try to launch terraform init I got this error:
I am very new to Terraform.
I just deleted environment and terraform.tfstate files from .terraform folder. And Removed the cloud block from main.tf file. And this resolved the issue.