Terraform: Migrating Terraform State to Terraform Cloud

A beginner’s hands-on guide to managing state the right way…

There’s something satisfying about watching your infrastructure come to life with just a terraform apply. But things can get messy when your state files live only on your local machine. One accidental deletion and you’ve got a headache waiting to happen.

That’s why in this project, I decided to move my Terraform state from local storage to Terraform Cloud. In this article, I’ll walk you through the full process, from installing Terraform on your local machine, writing the terraform configuration in VS Code and deploying infrastructure on Azure, to migrating the backend to Terraform Cloud for centralized state management.

This short tutorial will help you scale your setup the smart way!

Step 1: Install Terraform and Add To Environment Variables On Your Local Machine.

The first step is to install the latest version of Terraform on your local machine. If you have Chocolatey installed, open PowerShell as Administrator and run the following command: choco install terraform*.* This command will automatically install Terraform and add it to your system’s environment variables, this makes it accessible from any terminal window.

Alternatively, you can download Terraform manually. Go to developer.hashicorp.com/terraform, download the windows version to your local host. After downloading, extract the zip file, install and move the file to a preferred location on your machine. You also have to add the path to your environmental variables, under the path section.

Step 2: Configure Terraform Script in VS code, Initialize, Plan and Apply.

Open your VS code and install the extensions, Hashicorp Terraform and Terraform.

For the purpose of this article, i’m going to clone a new repository from GitHub so I can push the codes back to it once i’m done. To clone the GitHub repository, Click on the Source Control icon on the left sidebar (just above the Extensions icon). Select “Clone Repository“ and then add the URL link of your GitHub repo. You will be prompted to open a new destination folder on your machine where the local copy will be saved. Refer to these images below for guidance:

As you can see, our folder has been successfully created, click the file icon just beneath the explorer to create a main.tf file. The first code block in your main.tf file should specify the cloud provider you’ll be using. To get the code block, go to the registry section in developer.hashicorp.com/terraform. Select the cloud provider you’ll be working with (Azure) and copy and paste the code block into your main.tf file as the first code block. Refer to the next few images below for visual guidance.

After copying and pasting the code block block in your main.tf file, you need to specify your subscription id. Replace the comment line “# Configuration” options with:
features {}
subscription_id =”paste your subscription_id here

Always save your work. Refer to the images below for visual guidance:

After specifying the cloud provider you’re working with and defining your configuration, the next step is to open your terminal and run terraform init. This initializes your working directory and prepares it for terraform. You can then go ahead with your terraform script. In this project, we’re creating an Azure Resource Group. With Terraform, we can explicitly define how we want it created, from the name, to location/region and other settings. The next set of images shows the configuration set up for creating a Resource Group on Azure using Terraform. Always remember to save your work as you go.

Yes! We’ve successfully configured our Resource Group, specifying how we want Azure to create it. Before applying configuration, it is always best to run a plan, it gives you a preview of what Terraform will do. To plan, run the command: terraform plan -out main.tfplan

This command generates a binary plan file that acts as a blueprint of your changes. As you can see in the image below, in the left sidebar, a new file main.tfplan has been created for us.

After planning, you can now login into your Azure account by running: az login . Once it’s authenticated, you can the apply your configuration, run the command: terraform apply main.tfplan

This command tells Terraform to create all the resource you’ve initially specified in your script, exactly as specified, in your Azure account.

*You may wonder, “Why use Terraform, when i can actually create my resources directly on Azure? You see, when you create resources manually through the Azure Portal, it's a one-time action, hard to reproduce consistently. But with Terraform, your configuration files are reusable. You can recreate the same setup in another environment (development, testing and production environment) just by running a command!

Another reason is Terraform works not only with Azure but also AWS, GCP, and many other providers. So if you're working across clouds, it's a unified solution. So while the Azure Portal is great for quick testing or learning, Terraform is essential for serious, scalable, and professional infrastructure management!

You can also automate the creation, updating, or destruction of infrastructure without clicking through the Azure UI every time!*

As you can see in the Azure account below, the Terraform script has been applied and all the resources specified built!

Step 3: Migrate Terraform State From Local Storage to Terraform Cloud.

Create your Terraform cloud account. Go to developer.hashicorp.com/terraform and create an account. After it’s been successfully create, create an organization and a workspace in it (CLI-Driven workflow). Now you can go back to your terminal and log into your terraform cloud account by running the command: terraform login

*Recall that we are migrating the terraform state from the local storage to terraform cloud. This is usually done for a lot of reasons. For security purposes; you may lose the file or accidentally delete some configurations. It may also contain sensitive data you want to keep securely.

No need to carry your state file around or rely on a single machine. With Terraform Cloud, your state lives in the cloud, accessible from anywhere with secure authentication.

Terraform Cloud locks the state while someone is applying changes to prevent others from modifying at the same time, avoiding collisions and corruption. It keeps versioned backups of your state file, you can roll back to previous versions, audit changes and track what happened, when, and by whom.*

In your terminal, log into your Terraform cloud account by running: terraform login. It will automatically open up your Terraform cloud account and prompt you to generate a user token. Generate the token, copy and paste it in your terminal when it prompt you to.

Once you’re authenticated and fully in your Terraform cloud account from the terminal, navigate to the cloud account from your browser, there, copy your CLI-Driven configuration code block to set up your cloud integration. Paste this code block after the first code block in your main.tf file and save.

Please refer to the images below for visual guidance.

After successfully pasting the CLi-driven code block in your terraform configuration file to set up the cloud integration, save it up and then run terraform init in your terminal. It will clearly ask if you want to Migrate your Existing file state to the cloud. Enter the value: yes.

You will see from the second image below that the terraform.tfstate was successfully migrated to our Terraform cloud account.

You can now remove the state on your local storage by running: rm terraform.tfstate*

Congratulations on Migrating your Terraform state from local storage to Terraform cloud!!! In this guide, we started from the very beginning, installed Terraform, configured a working main.tf script, and set up Azure as our provider. We went through the core Terraform workflow of terraform init, plan, and apply, and we also verified the successful deployment of resources directly on the Azure Portal. To wrap up, we took things a step further by migrating our Terraform state from local storage to Terraform Cloud, setting the stage for better collaboration, version control, and scalability. With this foundation in place, you’re now equipped to build more advanced infrastructure with confidence, knowing your configurations are automated, consistent, and securely managed in the cloud.

Did you enjoy this tutorial? Don’t forget to like, comment and share! Follow me for more hands-on cloud tutorials and tips, your journey to becoming cloud-proficient starts here!

5
Subscribe to my newsletter

Read articles from Damilola Linda Olowookere directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Damilola Linda Olowookere
Damilola Linda Olowookere