Take Your Terraform State to the Cloud: A Step-by-Step Migration to Terraform Cloud”

Introduction

Managing infrastructure as code with Terraform is powerful, but storing your state file locally? That’s a ticking time bomb.

Local state files can easily get lost, corrupted, or become a nightmare to manage across teams. That’s where Terraform Cloud steps in — offering remote state management, collaboration, version history, and security out of the box.

In this guide, you’ll learn how to migrate your existing Terraform state from your local machine to Terraform Cloud — step-by-step. Whether you're working solo or building with a team, moving to the cloud is a serious upgrade to your workflow and peace of mind.

Let’s get your Terraform state where it belongs — in the cloud

Step 1.

Create a Terraform account, head to a browser on your local host, then www.terraform.io then click sign up to create an account, after creating an account, verify your email address then login with your credentials. to this website https://app.terraform.io/ , next is to create an Organization and we will create a workspace within the organization , once you click create Organization, give your Organization a name , i will use "Terraform_CloudORG" then click create organization.

Step 2.

After creating our Organization, we will create a new workspace, we will click on "CLI-Driven Workflow to create a CLI-Driven workspace. then give your Workspace a name i will use "Terraform_Project" Every other thing should be left on default, then create. Once created we will have a code block which we will add to our Terraform configuration files on our VSCODE to set up the cloud integration.

Step 3.

We will Navigate back to our VSCODE, then we run our command "az login" to login into our azure then after logging in in we run the next command "terraform init" , we ll get a response with " Terraform has been successfully initialized!" then next we run the next command " terraform plan" then we will run the 3rd command to apply and deploy our configurations " terraform apply"

Step 4.

After we done with step 3 , we head back to the terraform cloud account on our browser , head to the workspace and copy the terraform code provided earlier, so we copy the code from line 2 which has "cloud and the curly braces( { ) to line 9 with the 2nd to the last curly braces . then head to our VSCODE navigate to our code inside the main.tf then navigate to line 8 of our code, move line 8 down then paste the code we copied on line 8, so in a simple explanation the code we copied from the terraform cloud account will stay on line 8 thru line 15. then Save the Code.

Step 4.

After we done with step 3 , we head back to the terraform cloud account on our browser , head to the workspace and copy the terraform code provided earlier, so we copy the code from line 2 which has "cloud and the curly braces( { ) to line 9 with the 2nd to the last curly braces . then head to our VSCODE navigate to our code inside the main.tf then navigate to line 8 of our code, move line 8 down then paste the code we copied on line 8, so in a simple explanation the code we copied from the terraform cloud account will stay on line 8 thru line 15. then Save the Code.

Step 5.

After that we will head back to our terraform cloud environment, then we ll click on our profile icon, then in the drop down we will click on "account settings" after clicking on account settings, we will have to generate a user tokens, then next we click on tokens, then Under creating a user token, give your token a name , mine is "API token, Expiration leave it on 30 days and click Generate Token. copy and save your token once it has been generated then head back to the terminal and run the command " terraform login " you will get a prompt to proceed type yes and press enter to Proceed.. Next is a prompt to enter the token we copied earlier from our terraform cloud account, right click to paste (Please note While pasting your Token it will not be visible for security reasons.) then once we have right clicked, the press enter and we ll gain access to our Terraform cloud account on the terminal successfully. then We will run this command again " terraform init " to initialize all the settings and updates we have made to our codes. we will get a Successful message .

Step 6.

After that, we will then Head back to our Terraform Cloud Account then head to our Workspace , click on our workspace name which is " Terraform_project" then on the left hand side click on States we will discover that our terraform code has been migrated successfully to our terraform cloud environment from our local host, we will see a New State under our work space name.

Please Note: If we want our "terraform.tfstate" to leave our local host since it has been migrated to our Terraform cloud environment we will run this command " rm terraform.tfstate* .

Conclusion

Congrats, we have successfully migrated our Terraform state from local to Terraform Cloud, a crucial step toward more scalable, secure, and team-friendly infrastructure.

Now our workflow is Easier to manage, Safer and more reliable, Ready for collaboration.

This is just the beginning. With Terraform Cloud, we can explore remote operations, workspace automation, and even full-blown CI/CD pipelines

Enjoyed this guide?
Follow me on Hashnode for more DevOps tutorials, and check out my projects on GitHub.
Let’s keep leveling up — one resource at a time.

0
Subscribe to my newsletter

Read articles from Stillfreddie Techman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Stillfreddie Techman
Stillfreddie Techman