DEV Community

Cover image for πŸ–₯️Deploy Your First EC2 with Terraform (Step-by-Step Guide) β€” Part 3
Ahkar Swe
Ahkar Swe

Posted on

πŸ–₯️Deploy Your First EC2 with Terraform (Step-by-Step Guide) β€” Part 3

In the previous post, you set up Terraform and AWS CLI.

Now it’s time to do what really matters:

πŸ‘‰ Build real infrastructure using code

By the end of this guide, you’ll launch an EC2 instance using Terraform β€” no AWS Console clicks required.


🎯 What You’ll Build

We’ll create:

  • An EC2 instance
  • Using Terraform
  • In just a few lines of code

πŸ“ Step 1 β€” Create Project Structure

Create a new folder:

mkdir terraform-ec2
cd terraform-ec2
Enter fullscreen mode Exit fullscreen mode

Create files:

touch provider.tf main.tf
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 2 β€” Configure AWS Provider

Open provider.tf:

provider "aws" {
  region = "ap-southeast-1"
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 3 β€” Create EC2 Instance

Open main.tf:

resource "aws_instance" "web_server" {
  ami           = "ami-xxxxxxxxxxxx"
  instance_type = "t2.micro"

  tags = {
    Name = "terraform-server"
  }
}
Enter fullscreen mode Exit fullscreen mode

⚠️ Replace the AMI with a valid one from your AWS region.


πŸ”Ή Step 4 β€” Initialize Terraform

terraform init
Enter fullscreen mode Exit fullscreen mode

This downloads the AWS provider and prepares your project.


πŸ”Ή Step 5 β€” Preview Changes

terraform plan
Enter fullscreen mode Exit fullscreen mode

Terraform will show something like:

Plan: 1 to add, 0 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This means Terraform is about to create 1 resource (your EC2).


πŸ”Ή Step 6 β€” Apply (Create Infrastructure)

terraform apply
Enter fullscreen mode Exit fullscreen mode

Type:

yes
Enter fullscreen mode Exit fullscreen mode

πŸ” Verify in AWS Console

Go to:

πŸ‘‰ AWS Console β†’ EC2 β†’ Instances

You should see:

πŸ‘‰ Your instance running πŸŽ‰


🧠 What Just Happened?

Terraform just:

  1. Read your .tf files
  2. Compared desired state vs current state
  3. Called AWS API
  4. Created your EC2 instance

πŸ‘‰ This is Infrastructure as Code in action.


⚠️ Common Issue β€” AMI Not Found

You might see this error:

InvalidAMIID.NotFound
Enter fullscreen mode Exit fullscreen mode

Why?

AMI IDs are region-specific.


βœ… Fix

  • Go to AWS Console β†’ EC2 β†’ AMIs
  • Copy a valid AMI ID for your region
  • Replace it in your code

πŸ’‘ DevOps Insight

Right now, we are using a hardcoded AMI.

This works for learning, but in production:

❌ Not reusable
❌ Not flexible

πŸ‘‰ In the next posts, we’ll fix this using:

  • Variables
  • Data sources (dynamic AMI selection)

🧹 Clean Up (Important)

To delete your infrastructure:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Always clean up to avoid unnecessary AWS charges.


🎯 What You Just Learned

  • How to create an EC2 with Terraform
  • How init, plan, and apply work
  • How Terraform interacts with AWS

πŸ’‘ Final Thought

You just replaced:

clicking in AWS Console

with:

writing infrastructure as code

That’s a major step toward becoming a DevOps engineer πŸš€


πŸ‘¨β€πŸ’» About the Author

Hi, I’m Ahkar β€” sharing DevOps, AWS, and Infrastructure knowledge to help others grow πŸš€

I publish bilingual content (Myanmar πŸ‡²πŸ‡² + English πŸ‡ΊπŸ‡Έ) focused on real-world cloud learning.

🌐 Blog: https://mindgnite.com

If you found this helpful, consider following for more Terraform & DevOps content πŸ”₯


πŸ“š Terraform Learning Series

  • Part 1: Why Terraform
  • Part 2: Setup Guide
  • Part 3: First EC2 Deployment (this post)
  • Part 4: Variables, Outputs & State (coming next)

πŸ‘‰ Follow to continue the journey πŸš€

Top comments (0)