🚀 Creating an EC2 Instance from Scratch with Terraform

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

Today, we’re going to create an EC2 instance from scratch using Terraform. We’ll go step-by-step and learn how to:

✅ Generate an SSH key
 ✅ Create a key pair, VPC, and security group
 ✅ Launch an EC2 instance
 ✅ Use interpolation to extract values
 ✅ Control instance state (stop/start) with Terraform

Let’s get started — in the simplest and most practical way possible.



✅ Prerequisites

Before jumping into code, make sure:

  • You have already created the Terraform provider file
  • Terraform is initialized (terraform init)
  • AWS CLI is configured with an IAM user

All set? Great! Let’s begin.

🔑 Step 1: Generate SSH Key Pair

ssh-keygen
# Name it: terra-key-auto

This will generate two files:

  • terra-key-auto (private key)
  • terra-key-auto.pub (public key)

We’ll use these to access our EC2 machine.

✍️ Step 2: Write ec2.tf File

Let’s write all our resources step by step in one file:

# ec2.tf
# 1. Create SSH Key Pair in AWS
resource "aws_key_pair" "my_ssh_key" {
key_name = "terra-key-auto"
public_key = file("/home/ubuntu/terra-key-auto.pub") # Path to your public key
}
# 2. Use Default VPC
resource "aws_default_vpc" "default" {}
# 3. Create Security Group
resource "aws_security_group" "my_sg" {
name = "TWS-SG"
description = "this is a sooper se ooper upper security group"
vpc_id = aws_default_vpc.default.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "this is for ssh access"
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "this is for http access"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "this is for outside world requests from instance"
}
}
# 4. Launch EC2 Instance
resource "aws_instance" "my_instance" {
ami = "ami-xxxxxxxxxxxxxxxxx" # Replace with valid AMI ID
instance_type = "t2.micro"
key_name = aws_key_pair.my_ssh_key.key_name
security_groups = [aws_security_group.my_sg.name]
tags = {
Name = "My-Auto-server"
}
}
# 5. Control EC2 Instance State (Stop by default)
resource "aws_ec2_instance_state" "my_state" {
instance_id = aws_instance.my_instance.id
state = "stopped"
}

📘 Explanation

  • aws_key_pair uploads your public SSH key to AWS so you can log into EC2 securely.
  • aws_default_vpc lets you use the default networking setup.
  • aws_security_group allows:
  • SSH access on port 22
  • HTTP access on port 80
  • All outbound traffic for internet access
  • aws_instance launches your virtual machine.
  • aws_ec2_instance_state controls the instance’s state (stopped or running).
  • Interpolation is used like this:
  • key_name = aws_key_pair.my_ssh_key.key_name
  • This pulls a value from one resource to use in another — that’s interpolation!

🚀 Deploy Infrastructure

Once your code is ready, run:

terraform plan
terraform apply

And… ✨ Jaddo is in front of you!
 Your EC2 instance is created, and the public IP is ready to be used.

🔁 Change Instance State (From Stopped to Running)

Want to start the instance?

Simply go to this block:

resource "aws_ec2_instance_state" "my_state" {
instance_id = aws_instance.my_instance.id
state = "stopped" # change this to "running"
}

Change stopped to running and run:

terraform apply

💥 Now your instance is running.

🙌 Wrap Up

In this blog, we learned how to:

✅ Generate and use SSH keys
 ✅ Create VPC and security groups
 ✅ Launch and control EC2 instances using Terraform
 ✅ Use interpolation for dynamic value extraction

Everything from scratch, in a way that’s easy to understand.

If this helped you — please share and follow for more DevOps goodness!

By 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