terraform.tfstate file.
Example resources
Here is a minimal Terraform configuration that creates a VPC and a subnet. This is the kind of configuration whose state you might be managing locally:Configure the S3 backend
To switch to an S3 remote backend, add aterraform block to your configuration (for example in backend.tf or main.tf) that references the S3 backend and the target bucket/key:
bucket— the S3 bucket to store the state filekey— path/object name inside the bucket (e.g.prd/terraform.tfstate)region— AWS region containing the bucketdynamodb_table— optional DynamoDB table name to enable state locking
To enable safe concurrent operations, create a DynamoDB table and reference it via
dynamodb_table. This ensures Terraform can lock state while applying changes to prevent concurrent modifications.Migrate local state to S3
- Save the backend configuration in your repo (for example
backend.tf). - Initialize the working directory and migrate the state with the
-migrate-stateflag:
Type
yes to copy your current (local) state to the S3 backend so Terraform continues managing the same resources remotely. Type no to start with an empty state in the remote backend.yes, Terraform will configure the backend and upload the state file to the specified S3 bucket and key. You can verify the state object appears in the S3 console at bucket → key.
For example, after a successful migration you will see the prd/terraform.tfstate object in the S3 bucket:

Move state back to local
If you later decide to revert to a local backend:- Remove or comment out the S3 backend block from your Terraform configuration. For example:
- Re-run initialization with migration enabled:
- When prompted, answer
yesto copy the state from S3 back to your localterraform.tfstate. Terraform will migrate the remote state back into the local backend and populate the local state file.
Example of a migrated state file (top)
A minimal example of the top of a migrated state file looks like this:Quick reference
| Action | Command / file | Notes |
|---|---|---|
| Add S3 backend | Add terraform { backend "s3" { ... } } to backend.tf | Configure bucket, key, region, and optionally dynamodb_table |
| Initialize & migrate to S3 | terraform init -migrate-state | Answer yes to copy local state to S3 |
| Remove S3 backend | Comment/remove terraform { backend "s3" { ... } } | Prepare to migrate state back to local |
| Migrate back to local | terraform init -migrate-state | Answer yes to copy S3 state to local terraform.tfstate |
Links and references
That’s it — migrating between local and remote backends is straightforward: add or remove the backend configuration and useterraform init -migrate-state to copy the state to the target backend.