🚀 Creating an EC2 Instance with Terraform — Made Simple

 By Raees Qazi | DevOps Engineer | Learner | Mentor | Creator


🚀 Creating an EC2 Instance with Terraform — Made Simple

By Raees Qazi | DevOps Engineer | Learner | Mentor | Creator

Introduction

In today’s blog, we’ll learn how to create an EC2 instance using Terraform. I will explain the syntax in a simple and easy way so even beginners can follow along.

Let’s get started!


🛠️ Prerequisites

Before writing the Terraform code, I’m assuming:

✅ You’ve already created the provider file (e.g., provider.tf)
 ✅ You’ve already run terraform init to initialize Terraform
 ✅ Your AWS CLI is configured properly using an IAM user

If all the above steps are done, let’s jump into creating our EC2 instance.

📄 Step 1: Create the Terraform Configuration File

Open your terminal and type:

vim ec2.tf

Now paste the following code inside:

resource "aws_instance" "my_instance" {
count = 2 # Number of instances to create
ami = "ami-1234567890abcdef0" # Replace this with a valid AMI ID from your region
instance_type = "t2.micro" # Instance type (free-tier eligible)
key_name = "private-key" # Name of your existing AWS key pair
tags = {
Name = "My-Auto-Server" # Tag to name your instance
}
}

📝 Explanation:

  • resource "aws_instance" "my_instance": This defines the resource type (aws_instance) and gives it a name (my_instance).
  • count = 2: We want to create 2 EC2 instances.
  • ami: The Amazon Machine Image ID — use the correct AMI ID based on your AWS region.
  • instance_type: Type of machine. "t2.micro" is free-tier eligible and good for testing.
  • key_name: The name of the SSH key pair you'll use to connect.
  • tags: AWS tags help organize your resources. We're naming the instance My-Auto-Server.

▶️ Step 2: Run Terraform Commands

Once your code is ready, run the following commands:

terraform plan

This command will show you the execution plan — what Terraform is going to create.

Then, to apply and create the resources:

terraform apply

Type yes when prompted.

🎉 That’s It!

Boom! 💥 Just like magic — or as we say in Urdu, “Jaadoo aap ke samne hai” — your EC2 instances are now live in AWS!

💡 Final Thoughts

Working with Terraform is not just powerful, it’s also simple once you understand the syntax. As a DevOps engineer, automating infrastructure like EC2 instances is a key part of our day-to-day tasks.

If you found this helpful:

👉 Share this post
 👉 Follow for more practical DevOps tips

Until next time, keep learning, keep automating!

— Raees Qazi
 DevOps Engineer | Learner | Mentor | Creator

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator