Skip to content

Migrate State to Terraform Cloud

Step-01: Introduction

  • We are going to migrate State to Terraform Cloud

Step-02: Review Terraform Manifests

  • Important Note: No default values provided for variables

Step-03: Execute Terraform Commands (First provision using local backend)

  • First provision infra using local backend
  • terraform.tfstate file will be created in local working directory
  • In next steps, migrate it to Terraform Cloud
# Terraform Initialize
terraform init

# Terraform Validate
terraform validate

# Terraform Plan
terraform plan

# Terraform Apply
terraform apply -auto-approve

Step-04: Review your local state file

  • Review your local terraform.tfstate file once

Step-05: Update remote backend in Terraform Block

Step-05-01: Create New Workspace with CLI-Driven workflow

  • Create New workspace with CLI-Driven workflow
  • Login to Terraform Cloud
  • Select Organization -> hcta-demo1
  • Click on New Workspace
  • Choose your workflow: CLI-Driven Workflow
  • Workspace Name: state-migration-demo
  • Click on Create Workspace

Step-05-02: Update remote backend in Terraform Block

# Template
  backend "remote" {
    hostname      = ""
    organization  = "<YOUR-ORG-NAME>"

    workspaces {
      name = "<SOME-NAME>"

# Replace Values
  backend "remote" {
    hostname      = ""
    organization  = "hcta-demo1"  # Organization should already exists in Terraform Cloud

    workspaces {
      name = "state-migration-demo" 
      # Two cases: 
      # Case-1: If workspace already exists, should not have any state files in states tab
      # Case-2: If workspace not exists, during migration it will get created
  • Case-2 above for workspaces is failing with this error
Kalyans-MacBook-Pro:terraform-manifests kdaida$ terraform init
Initializing the backend...
Error: Error looking up workspace
Workspace read failed: resource not found
Kalyans-MacBook-Pro:terraform-manifests kdaida$

Step-06: Migrate State file to Terraform Cloud and Verify

# Terraform Login
terraform login
1) Should see message |Success! Terraform has obtained and saved an API token.|
2) Verify Terraform credentials file
cat /Users/<YOUR_USER>/.terraform.d/credentials.tfrc.json
cat /Users/kdaida/.terraform.d/credentials.tfrc.json
Additional Reference:

# Terraform Initialize
terraform init
1) During reinitialization, Terraform presents a prompt saying that it will copy the state file to the new backend. 
2) Enter yes and Terraform will migrate the state from your local machine to Terraform Cloud.

# Verify in Terraform Cloud
1) New workspace should be created with name "state-migration-demo"
2) Verify "states" tab in workspace, we should find the state file

Step-07: Add Variables & AWS Credentials in Environment Variables

Step-07-01: Add Variables

aws_region = us-east-1
instance_type = t3.micro

Step-07-02: Configre Environment Variables

  • Setup AWS Access Keys for Terraform
  • Go to Organization (hcta-demo1) -> Workspace(state-migration-demo) -> Variables
  • In environment variables, add the below two
  • Configure AWS Access Key ID and Secret Access Key
  • Environment Variable: AWS_ACCESS_KEY_ID
  • Environment Variable: AWS_SECRET_ACCESS_KEY

Step-08: Delete local terraform.tfstate

  • First take backup and put it safe and delete it
# Take backup
cp terraform.tfstate terraform.tfstate_local_before_migrate_to_TF_Cloud

# Delete
rm terraform.tfstate

Step-09: Apply a new run from CLI

  • Make a change and do terraform apply
# Change Instances from 1 to 2 (
count = 2

# Terraform Apply
terraform apply 

# Verify in Terraform Cloud
1) Verify in Runs Tab in TF Cloud
2) Verify States Tab in TF Cloud

Step-10: Destroy & Clean-Up

  • Destroy Resources from cloud this time instead of terraform destroy command
  • Go to Organization (hcta-demo1) -> Workspace(state-migration-demo) -> Settings -> Destruction and Deletion
  • Click on Queue Destroy Plan
# Clean-Up files
rm -rf .terraform*
rm -rf terraform.tfstate*