Skip to main content

Command Palette

Search for a command to run...

πŸš€ Terraform Series – Day 6

Deploy EC2 with VPC, Security Group & SSH Access (Full Hands-On)

Published
β€’3 min read
πŸš€ Terraform Series – Day 6

🎯 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 3 of 8

πŸš€ 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 5

Terraform Providers, Resource Types & Naming In today’s Terraform journey, I explored one of the most fundamental concepts that every DevOps engineer must understand Providers and Resource Naming Stru