Procedure
These instructions are for Terraform version 0.15.4 or above.
1. Have your Terraform configuration on your local machine
2. Add a remote backend stanza as shown below and configure Terraform CLI credentials for Terraform Cloud:
terraform { backend "remote" { hostname = "app.terraform.io" organization = "your_organization_name_here" workspaces { name = "your_workspace_name_here" } } }
3. Execute terraform init
on your local machine which will initialize Terraform with the remote backend and download a copy of the state file to your local machine.
4. Replace the remote backend configuration with an S3 backend configuration as shown below (make sure to supply your AWS credentials so you can access the S3 bucket):
terraform { backend "s3" { bucket = "mybucket" key = "path/to/my/state" - #name of the S3 object that will store the state file region = "us-east-1" } }
5. Execute terraform init -migrate-state
and Terraform will detect the backend change and it will offer you to migrate the state file (enter yes
when prompted) to the S3 bucket. If the migration is successful it should look like below:
Initializing the backend... Backend configuration changed! Terraform has detected that the configuration specified for the backend has changed. Terraform will now check for existing state in the backends. Terraform detected that the backend type changed from "remote" to "s3". Do you want to copy existing state to the new backend? Pre-existing state was found while migrating the previous "remote" backend to the newly configured "s3" backend. No existing state was found in the newly configured "s3" backend. Do you want to copy this state to the new "s3" backend? Enter "yes" to copy and "no" to start with an empty state. Enter a value: yes Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
Additional Information
-
If you have any questions or you need help please contact support at tf-cloud@hashicorp.support or submit a ticket via our support portal.