Terraform

How do you migrate from local state to a new remote backend?

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

  1. Provision backend resources (for example an S3 bucket with versioning and a DynamoDB table) from a separate bootstrap configuration.

  2. Add the backend block to your Terraform configuration and run terraform init -migrate-state.

  3. Verify the migration with terraform state list or terraform state pull and keep a secure backup of the previous file.

  4. Remove or archive the local terraform.tfstate only after confirming new plans operate against the remote backend.