Solved: Terraform - Error: Unsupported state file format

Dan | Dec 5, 2023 min read

I have been managing a software project for a client for a couple years. Oftentimes, multiple months go by without needing to do any maintenance work; however, when maintenance comes up, dependency issues can arise because the dependencies have not been updated in a couple of months.

Today, I was releasing some new code using GitHub Actions to deploy Terraform. terraform apply produced th following error during the CI/CD process when I cut a release:

Error: 
│ Error reading local state: 2 problems:
│ 
│ - Unsupported state file format: The state file could not be parsed as JSON: syntax error at byte offset 1.
│ - Unsupported state file format: The state file does not have a "version" attribute, which is required to identify the format version.
│ 
│ Terraform is trying to read your local state to determine if there is
│ state to migrate to your newly configured backend. Terraform can't continue
│ without this check because that would risk losing state. Please resolve the
│ error above and try again.

The error continued when I tried to run the command locally. Stack Overflow cautiously recommended deleting the .terraform directory that contains state metdata in my local repository, but warned that it could be dangerous. I tried it and it did not solve the problem; running any terraform command continued to raise the error.

Solution: I had to delete a local .tfstate file to fully clear the local terraform state, and then run terraform init -reconfigure -upgrade to reconfigure the connection between my local state and the remote backend. Including the -upgrade command updated provider packages to ensure I was running the latest dependencies. This fixed the issue locally, but neither the .terraform directory of the .tfstate file exist in the git repository. I believe adding the -upgrade command fixes the deployment issues.