0

We store our states with a gitlab backend. Someone deleted the state. We recovered the state to a json file via shapshot of our gluster backend. How do I restore the state to gitlab?

I've tried following this link with no success.

https://docs.gitlab.com/17.6/ee/user/infrastructure/iac/terraform_state.html

I've also seen a post about using the 'Copy terraform init command' from the project state page on gitlab and running

terraform state push /path/to/terraform.tfstate -force 

and this didnt work.

Any guidance would be greatly appreciated.

3
  • From terraform docs: "Usage: terraform state push [options] PATH" Your order looks reversed terraform state push PATH [options] Commented Jan 19 at 16:40
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jan 19 at 21:37
  • the resolution to this was to make a branch of the project and add the restored terraform state. Then run the init cmd which will see the state file know ask if you want to push the state file back to gitlab. (it thinks its a local state and the code is still backend http) Commented Jan 25 at 13:01

1 Answer 1

0

create a new branch. Copy the json file to name it, terraform.tfstate to the same directory as the main.tf file.

Use the code below to initialize the project.

PROJECT_ID="*<gitlab-project-id>*"
TF_USERNAME="*<gitlab-username>*"
TF_PASSWORD="*<gitlab-personal-access-token>*"
TF_ADDRESS="https://gitlab.domain/api/v4/projects/${PROJECT_ID}/terraform/state/**old-state-name**"

terraform init \
  -backend-config=address=${TF_ADDRESS} \
  -backend-config=lock_address=${TF_ADDRESS}/lock \
  -backend-config=unlock_address=${TF_ADDRESS}/lock \
  -backend-config=username=${TF_USERNAME} \
  -backend-config=password=${TF_PASSWORD} \
  -backend-config=lock_method=POST \
  -backend-config=unlock_method=DELETE \
  -backend-config=retry_wait_min=5

At the end, it'll ask you if you want to copy the state to the new back end, type 'yes'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.