Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ Terraform Series – Day 6

Deploy EC2 with VPC, Security Group & SSH Access

Updated
β€’3 min read
πŸ“˜  Terraform Series – Day 6
G

Gujjar Apurv is a passionate DevOps Engineer in the making, dedicated to automating infrastructure, streamlining software delivery, and building scalable cloud-native systems. With hands-on experience in tools like AWS, Docker, Kubernetes, Jenkins, Git, and Linux, he thrives at the intersection of development and operations. Driven by curiosity and continuous learning, Apurv shares insights, tutorials, and real-world solutions from his journeyβ€”making complex tech simple and accessible. Whether it's writing YAML, scripting in Python, or deploying on the cloud, he believes in doing it the right way. "Infrastructure is code, but reliability is art."

🎯 Objective

In this hands-on, we will:

  • Generate SSH key

  • Create key pair using Terraform

  • Configure VPC & Security Group

  • Launch EC2 instance

  • Connect via SSH

  • Clean up resources

πŸ‘‰ This is your first real-world Terraform task

🧩 Step 1: Generate SSH Key

ssh-keygen

βœ” This creates:

πŸ‘‰ We will use this to access EC2

πŸ“„ Step 2: Create Terraform File

touch ec2.tf

🧱 Step 3: Add Terraform Code

# Create Key Pair
resource "aws_key_pair" "my_key" {
  key_name   = "terra-key-aws"
  public_key = file("terra-key-aws.pub")
}


# Default VPC
resource "aws_default_vpc" "default" {}

# Security Group
resource "aws_security_group" "my_groups" {
  name        = "my-group"
  description = "Security group for EC2"
  vpc_id      = aws_default_vpc.default.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow SSH"
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow HTTP"
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow all outbound"
  }

  tags = {
    Name = "automate-sg"
  }
}

# EC2 Instance
resource "aws_instance" "my_instance" {
  ami                    = "ami-0cb91c7de36eed2cb"
  instance_type          = "t2.micro"
  key_name               = aws_key_pair.my_key.key_name
  vpc_security_group_ids = [aws_security_group.my_groups.id]

  root_block_device {
    volume_size = 10
    volume_type = "gp3"
  }

  tags = {
    Name = "terra-ec2"
  }
}

βš™οΈ Step 4: Initialize Terraform

terraform init

βœ” Downloads AWS provider

βœ” Prepares working directory

βœ… Step 5: Validate Configuration

terraform validate

βœ” Ensures syntax is correct

πŸ“Š Step 6: Plan Execution

terraform plan

βœ” Shows resources to be created:

  • Key Pair

  • VPC

  • Security Group

  • EC2 Instance

πŸš€ Step 7: Apply (Create Infrastructure)

terraform apply

πŸ‘‰ Type yes to confirm

❌ Common Error: Not Authorized

πŸ‘‰ Reason:

  • IAM user does not have required permissions

βœ” Fix:

Go to AWS IAM β†’ Attach Policy

  • AdministratorAccess (easy way)

    OR

  • EC2FullAccess

  • VPCFullAccess

πŸ–₯ Step 8: Verify in AWS Console

Go to EC2 Dashboard:

βœ” Instance running
βœ” Security group attached
βœ” Key pair created

πŸ” Step 9: Fix Key Permission

chmod 400 terra-key-aws

πŸ‘‰ Required before SSH

πŸ”— Step 10: Connect to EC2

ssh -i terra-key-aws ubuntu@<your-public-ip>

πŸ‘‰ Now your server is live πŸš€

🧹 Step 11: Destroy Resources (IMPORTANT)

terraform destroy

πŸ‘‰ Prevent unnecessary AWS charges πŸ’Έ

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

β€œA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.”

πŸ“¬ Let's Stay Connected

Terraform

Part 6 of 12

πŸš€ Terraform Series – Automate Your Infrastructure Starting a complete **Terraform series** where I’ll cover everything from **basic to advanced level** with real-world practicals. In this series, you will learn: β€’ What is Infrastructure as Code (IaC) & why it matters β€’ Terraform fundamentals (providers, resources, state) β€’ Writing and managing Terraform configurations β€’ Variables, outputs & modules β€’ Remote state & state management β€’ Provisioning infrastructure on AWS β€’ Automation & real-world use cases β€’ Advanced concepts like workspaces, modules, and best practices 🎯 Goal: Help you automate infrastructure and become job-ready in DevOps. Perfect for **beginners, students, and DevOps learners** who want hands-on experience. Stay tuned and let’s build infrastructure the smart way βš‘πŸ’» #Terraform #DevOps #Cloud #AWS #InfrastructureAsCode #Automation

Up next

πŸ“˜ Terraform Series – Day 7

Variables & Outputs (Make Your Code Smart πŸ”₯) In real-world DevOps, writing flexible and reusable code is very important.Today, we will learn how to use Variables and Outputs in Terraform to make our

More from this blog

T

The OpsVerse with Apurv

26 posts

Sharing hands-on DevOps, AWS, and Cloud tutorials with real-world projects, tips, and automation guides for students and professionals.