🚀 Launch an EC2 Instance & Migrate Terraform State to Terraform Cloud Via Vscode


INTRODUCTION
Terraform is a powerful Infrastructure-as-Code (IaC) tool, but managing its state properly is crucial for team collaboration and scalability. This guide walks you through launching an EC2 instance and migrating the Terraform state to Terraform Cloud—ensuring secure storage and seamless teamwork.
✅ Goals
By the end of this tutorial, you will:
Provision an EC2 instance using Terraform
Use Terraform Cloud as a remote backend
Migrate your local terraform.tfstate to Terraform Cloud, step by step
Before proceeding, ensure you have the following:
Terraform CLI - Installed (
terraform -v
)Vscode -Installed & configured (with terraform extention)
AWS Account -IAM user with EC2 & VPC permissions
Terraform Cloud Account - Organization set up
🔨 Step-by-Step Guide
🏗 Step 1: Create Your Terraform Project Locally
Setting up your project locally ensures you have full control over your infrastructure before migrating state.
Create a folder in vscode named
ec2-tfc-migration
Add the following files and input your desired codes
main.tf
(Defines provider setup & EC2 resource)variables.tf
(Stores reusable parameters)terraform.tfvars
(Optional – overrides defaults)outputs.tf
(Displays key outputs)
🔄 Step 2: Run Terraform Locally First
Running Terraform locally helps validate your configurations before integrating Terraform Cloud.
- terraform init
- terraform plan
- terraform apply
✅ Confirm that the EC2 instance has launched in the AWS Console.
At this point, you'll have a local Terraform state file (terraform.tfstate
) storing the infrastructure state.
🏢 Step 3: Create a Workspace in Terraform Cloud
Terraform Cloud provides secure, remote storage for your infrastructure state.
Log into Terraform Cloud
Select your organization
Click New Workspace
Choose CLI-driven workflow
Name it:
ec2-tfc-migration
Click Create Workspace
🔐 Step 4: Authenticate Terraform CLI to Terraform Cloud
To connect Terraform CLI with Terraform by generating a token as shown in the diagram
Copy the token as shown in the diagram
Run ‘terraform login’ in your terminal and input the copied token
Note that the copied token is pasted in the terminal once you right-click and won’t be visible to you
✅ The terraform logo appears in the terminal once the login is successful
⚙️ Step 5: Configure Terraform Cloud Backend in Your Code
Copy the backend code and paste in the main.tf file which will enable configuration state storage in Terraform Cloud.
🔄 Step 6: Re-Initialize Terraform to Migrate State
Migrating your state ensures Terraform can track infrastructure changes remotely.
terraform init
🚨 Terraform will prompt:
Do you want to migrate your state? → Type yes
✅ Terraform automatically uploads your state file to Terraform Cloud.
🔍 Step 7: Verify Migration Worked
Log into Terraform Cloud
Navigate to the
ec2-tfc-migration
workspaceConfirm the latest state under the States tab
🚀 Step 8: Continue Using Terraform Remotely
Terraform now stores its state in Terraform Cloud, ensuring safe collaboration.
Try running:
terraform plan terraform apply
Each run will be logged in Terraform Cloud for better visibility.
Check your AWS Account and all resources will be visible
🧹 Cleanup (Optional)
If you no longer need the EC2 instance, simply destroy it:
terraform destroy
🎓 Key Takeaways
Concept | What You Learned |
Terraform Cloud | A remote backend for managing Terraform state |
terraform login | Authenticate CLI with Terraform Cloud |
Backend migration | Moves .tfstate from local to remote |
Workspace CLI mode | Allows Terraform execution from local CLI |
Remote visibility | View runs, plans, and state in the Terraform Cloud UI |
Subscribe to my newsletter
Read articles from Joel Thompson directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
