🛠️ How to Migrate Terraform State to Terraform Cloud (with AWS VPC Example)

🧠 Real-world scenario:
You’ve built out a VPC and subnet using Terraform on your local machine. But now, your team needs to collaborate — and using local state isn’t safe.
That’s where Terraform Cloud comes in. It helps you manage infrastructure collaboratively, safely, and at scale.

In this guide, you’ll:

✅ Create AWS infrastructure (VPC + Subnet)
✅ Migrate your Terraform state to Terraform Cloud
✅ Confirm everything in the AWS Console and Terraform Cloud Dashboard


✅ Prerequisites

Before you begin, ensure you have:

  • ✅ An AWS account with admin privileges

  • ✅ AWS CLI installed and configured (aws configure)

  • ✅ A Terraform Cloud account (Sign up here)

  • ✅ Visual Studio Code with:

  • ✅ (Optional) A GitHub account for version control


🔧 Step 1: Install Terraform on Your Machine

  1. Go to the official website: Install | Terraform | HashiCorp Developer

  2. Download the version for your operating system (e.g., Windows 64-bit)

  3. After downloading, extract the ZIP file to a folder, e.g., C:\terraform

Add Terraform to Your System PATH

  1. Open the folder you just extracted

  2. Right-click on the terraform.exe file

  3. Click “Copy as path”

Next:

  1. Press Windows + S, search for:

     Edit the system environment variables
    

    Open it.

  2. In the System Properties window, click “Environment Variables…”

  3. Under System Variables, select Path and click Edit…

  4. Click New and paste the full path you copied earlier (it should end in terraform.exe)

    • 🟡 Tip: You can remove the terraform.exe part, so it ends at the folder (e.g., C:\terraform)
  5. Click OK on all windows to save


✅ Step 2: Verify the Installation

Open Command Prompt and run:

terraform --version

If Terraform is properly installed, it will return the version number.

ℹ️ Want to install Terraform on macOS or Linux? Visit the official installation guide.


❗ Trouble Running terraform?

If typing terraform gives you an error like:

'terraform' is not recognized as an internal or external command...

Your system may not be detecting the path. Follow this alternative method:

✅ Alternative Fix: Run Terraform Directly

  1. Open PowerShell

  2. Run this command (adjust the path to where your Terraform file is located):

& "C:\terraform\terraform.exe" -version

✅ If this works, it confirms Terraform is installed correctly but your system PATH isn’t configured properly.

🛠️ Check What’s in Your PATH

In PowerShell, run:

$env:Path -split ";"

Check if the folder containing terraform.exe (e.g., C:\terraform) is listed.

If it’s not listed, repeat the steps to add it to your Environment Variables properly.


📁 Step 3: Set Up Your Terraform Project

mkdir terraform-aws
cd terraform-aws
git init
touch main.tf README.md

Open the folder in VS Code:

 → Open Folder → Select "terraform-aws"

🌐 Step 4: Write Terraform Configuration (main.tf)

Paste the following into main.tf:

In Visual Studio Code, download the Terraform extension

hclCopyEditterraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.0.0"
    }
  }

  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "demo_vpc"
  }
}

resource "aws_subnet" "main" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.2.0/24"
  availability_zone = "us-east-1a"

  tags = {
    Name = "demo_subnet"
  }
}


🔐 Step 5: Configure AWS CLI

If not done yet:

aws configure

Enter:

  • Access key

  • Secret key

  • Region: us-east-1

  • Output format: json


⚙️ Step 6: Initialize and Deploy Infrastructure

Run:

terraform init
terraform plan -out=main.tfplan
terraform apply main.tfplan

Type yes to confirm when prompted. This provisions your VPC and Subnet.


✅ Step 7: Confirm in AWS Console

Visit the AWS VPC Dashboard:

  • Click Your VPCs → you should see one tagged demo_vpc

  • Click Subnets → confirm the demo_subnet exists in us-east-1a

✅ This confirms your infrastructure was deployed successfully.


☁️ Step 8: Set Up Terraform Cloud

  1. Go to Terraform Cloud

  2. Log in or sign up

  3. Navigate to:

    • Organizations → Create Organization → e.g., my-dev-org

  4. Create a CLI-driven workspace:

    • Workspaces → Create Workspace

    • Name it aws-vpc-terraform

    • Choose CLI-driven workflow


🔑 Step 9: Authenticate Terraform CLI

In your terminal, run:

terraform login
  • Terraform opens a browser

  • Approve the access and copy the token

  • Paste it into the terminal (it will be hidden)


🔁 Step 10: Configure Remote Backend

Update the top of your main.tf:

hclCopyEditterraform {
  backend "remote" {
    organization = "my-dev-org"  # Replace with your actual org name

    workspaces {
      name = "aws-vpc-terraform"
    }
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.0.0"
    }
  }

  required_version = ">= 1.0"
}

🔄 Step 11: Migrate Terraform State

Re-run:

terraform init

Terraform will detect the remote backend and ask:

“Do you want to migrate your existing state to the new backend?”

Type:

yes

✅ Your state is now safely managed by Terraform Cloud!


🧪 Step 12: Confirm in Terraform Cloud

  • Go to Terraform Cloud

  • Navigate to your workspace

  • Under States, you'll see your deployed infrastructure

  • Under Runs, you'll find a full history of actions


🧹 Optional: Clean Up

To destroy all AWS resources:

terraform destroy

✅ Summary

You’ve successfully:

✅ Installed Terraform (and handled any path issues)
✅ Created AWS infrastructure locally
✅ Migrated your Terraform state to Terraform Cloud
✅ Verified deployment in both AWS and Terraform Cloud

Your infrastructure is now scalable, trackable, and team-ready — all powered by Infrastructure as Code.

0
Subscribe to my newsletter

Read articles from Di Nrei Alan Lodam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Di Nrei Alan Lodam
Di Nrei Alan Lodam