Transitioning Terraform State: Local to Remote Made Easy

PREREQUISITES FOR THIS PROJECT
An active Azure subscription
Visual Studio Code with the following Extensions: Azure CLI, Terraform, GitHub
A GitHub account
A Terraform Cloud account. You can sign up using this link https://app.terraform.io/public/signup/account
STEP 1: Install Terraform on your machine
To do this, go to the official Terraform website, Install | Terraform | HashiCorp Developer and download the package manager for the OS you are using.
After downloading, extract the folder.
After extracting the folder, open the extracted file and select terraform, right-click and select copy as path
On your PC, search for edit the system environment and click open. Next, click Advanced, then Environment variables
Paste the Terraform path file that you copied earlier in the place where the arrow is pointed and click OK.
Confirm that you have successfully installed Terraform on Windows by opening the command prompt and run the command terraform --version
Note that you can get the installation guide for other OS on Terraform official website.
Step 2: Install Terraform Extension on VS Code
Open a window on VS Code, go to Extensions, type Terraform in the search bar and Install Terraform
Step 3: Create a repository on GitHub
Create a repository, give the repository a name, and click Create Repository.
For this project, I created a folder on my PC named Terra_form and I ran the commands below on Git Bash, as seen in the diagram below, to create the files I needed.
Next, open a new window on VS Code - File - Open Folder - Select Folder
Step 4: Select a Provider
For this project, I am using the Azure provider. For this, you go to Browse Providers | Terraform Registry. Select Azure - Use Provider.
After copying the code, open VS Code - main.tf and paste the code you just copied.
Next, create all resources that you need e.g. Resource group, virtual network, subnet etc.
For the purpose of this demonstration, I created a resource group named demo_rg in the UK South region. I also created a virtual network and subnet. You can create as many resources as you want.
To save, press Ctrl + S.
Step 5: Login to Azure
To do this, you can us az login
or az login --tenant <tenant id>
. To get your tenant id, open Azure portal - Microsoft Entra ID - Overview - Tenant ID
Step 6: Initialize, Plan and Apply to Terraform
Run terraform init
to initialize terraform
Next, you plan the terraform configuration by running terraform plan
. From the diagram below, you can see the resources that are created on Azure using Terraform.
I ran terraform plan -out=main.tfplan
to get the result seen in the diagram.
Also, you would notice some changes in VS Code after running the above command as seen below.
Next, apply the configuration by running command terraform apply main.tfplan
You can confirm the creation of your resource on the Azure portal as shown below.
Do not forget to use the command terraform destroy
to delete all the resources created.
NOTE: You do not have to run the terraform destroy
command if you want to migrate from Terraform state to Terraform Cloud
When prompted with Enter a value, type Yes.
Step 7: Migrate from Terraform State to Terraform Cloud
To migrate to the Terraform Cloud, you need to have a Terraform account. You can sign up through https://app.terraform.io/public/signup/account.
After signing in with your GitHub account after authentication has been established, go to Account Settings - Organizations - Create Organization. Give the organization a name and select Create organization.
Next, you create a workspace by selecting the organization you just created. Go to Manage - Workspaces - Create a Workspace - Choose the one most suitable for your project. For my project, I chose CLI-Driven Workflow since I am working with Terraform using CLI.
Give your workspace a unique name, select the project, give a description of the workspace and select Create.
Go to the terminal and run the command terraform login
Terraform opens a web browser for you to generate token
Copy the token as shown below
Paste the token you have copied but note that the token won’t be visible.
Step 8: Set up cloud integration
Next, you add a code block to your Terraform configuration files to set up the cloud integration. To do this, go to Workspaces - Overview - Example code. Copy the code as shown below and paste the code in the main.tf file.
Subscribe to my newsletter
Read articles from Ms. B directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ms. B
Ms. B
Hi, I'm a tech enthusiast who has decided to document her cloud journey as the day goes by. Stay tuned and follow me through this journey which I believe would be a wonderful experience. I'm also a team player who loves collaborating with others to create innovative solutions.