Migrating Terraform State to Terraform Cloud: A Complete Guide
When working with Terraform, managing your state file is critical. Moving it to Terraform Cloud allows you to enjoy better collaboration, remote state storage, and state locking, among other features. In this post, I'll walk you through the process of migrating a local Terraform state file to Terraform Cloud, providing you with more control and scalability for your infrastructure.
Prerequisites
Before we begin, make sure you have the following:
A Terraform Cloud account (free or paid)
A Terraform project with local state (If you're just starting, follow my previous post on setting up an Azure infrastructure using Terraform)
Setting Up Terraform Cloud
The first step was creating an account on Terraform Cloud. I created an organization called "rabitech", which serves as the space where my team can manage multiple projects. Then, I set up a workspace called "new-remote-state" for storing the Terraform state of this project.
Once the organization is set up, I created a new workspace to store the state file. I named this workspace "new-remote-state". This workspace is where your Terraform state file will live.
Configuring Terraform for Cloud Storage
Next, you’ll need to update your Terraform configuration to point to Terraform Cloud. Add the following code to your main.tf
file:
This code tells Terraform to store the state file in Terraform Cloud.
Authenticate and Initialize Terraform
After updating your main.tf
file to point to Terraform Cloud, you need to initialize Terraform and authenticate with your Terraform Cloud account. Start by running:
If you're not logged in, you might encounter an error asking for a token to authenticate with Terraform Cloud:
Error: Required token could not be found
To resolve this, run the following command:
This will open a browser window where you can complete the authentication process. Once approved, Terraform will generate and retrieve the token required for further operations.
Here's what you should see in the terminal after a successful login:
After logging in, run terraform init
again to initialize Terraform, and you'll be ready to migrate your state to Terraform Cloud.
Verifying the Migration
Once Terraform finishes the migration, you can verify that your state has successfully moved to Terraform Cloud by navigating to the States tab in your workspace. Here, you’ll be able to see the new state file for your infrastructure.
The state file contains detailed information about the resources managed by Terraform, including the version, the resources, and their configurations.
This means your state is no longer stored locally but securely in Terraform Cloud, ensuring better collaboration and remote state management.
Moving your Terraform state to Terraform Cloud brings added security, collaboration, and scalability. I hope this guide helped simplify the process. Stay tuned for more on working with Terraform modules in the next project!
Subscribe to my newsletter
Read articles from Rabiatu Mohammed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rabiatu Mohammed
Rabiatu Mohammed
CLOUD ENGINEER | DEVOPS | SECURITY