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
Create files:
touch provider.tf main.tf
πΉ Step 2 β Configure AWS Provider
Open provider.tf:
provider "aws" {
region = "ap-southeast-1"
}
πΉ Step 3 β Create EC2 Instance
Open main.tf:
resource "aws_instance" "web_server" {
ami = "ami-xxxxxxxxxxxx"
instance_type = "t2.micro"
tags = {
Name = "terraform-server"
}
}
β οΈ Replace the AMI with a valid one from your AWS region.
πΉ Step 4 β Initialize Terraform
terraform init
This downloads the AWS provider and prepares your project.
πΉ Step 5 β Preview Changes
terraform plan
Terraform will show something like:
Plan: 1 to add, 0 to change, 0 to destroy.
π This means Terraform is about to create 1 resource (your EC2).
πΉ Step 6 β Apply (Create Infrastructure)
terraform apply
Type:
yes
π Verify in AWS Console
Go to:
π AWS Console β EC2 β Instances
You should see:
π Your instance running π
π§ What Just Happened?
Terraform just:
- Read your
.tffiles - Compared desired state vs current state
- Called AWS API
- Created your EC2 instance
π This is Infrastructure as Code in action.
β οΈ Common Issue β AMI Not Found
You might see this error:
InvalidAMIID.NotFound
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
Always clean up to avoid unnecessary AWS charges.
π― What You Just Learned
- How to create an EC2 with Terraform
- How
init,plan, andapplywork - 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)