How to Create a Perfect AWS Security Group (Production-Ready & Secure)
AWS

How to Create a Perfect AWS Security Group (Production-Ready & Secure)

Design secure, production-ready AWS Security Groups using least-privilege—without downtime or guesswork

TermTrix
TermTrix
Dec 20, 2025
4min read

🔐 How to Create a Perfect AWS Security Group (Without Breaking Your App)

TΞRMTRIX


When people say “my EC2 is secure”, most of the time it isn’t.

Open ports, default security groups, public databases — these are some of the most common (and dangerous) AWS mistakes.

In this article, I’ll walk you through how to design a perfect AWS Security Group (SG) using least-privilege principles, without downtime, and without guesswork.

This approach works for:

  • EC2
  • RDS (PostgreSQL / MySQL)
  • ElastiCache (Redis)
  • Docker-based backends
  • Production & staging environments

🔍 What Is a Security Group (Really)?

An AWS Security Group is a stateful virtual firewall that controls:

  • Inbound traffic — who can connect to your resource
  • Outbound traffic — where your resource can connect

Key rule

If a port is not explicitly allowed, it is blocked.

Security Groups work at the network interface level, not inside your OS.


❌ The Biggest Mistake People Make

Using one security group for everything — usually the default SG — and opening ports like:

0.0.0.0/0 → 22 0.0.0.0/0 → 5432 0.0.0.0/0 → 6379

This makes your infrastructure:

  • Easy to hack
  • Hard to audit
  • Impossible to scale securely

✅ The “Perfect SG” Design (Production-Ready)

Instead of one SG, we use multiple purpose-based security groups.

Target Architecture

Internet ↓ Backend EC2 (backend-sg) ↓ RDS PostgreSQL (rds-sg) ↓ ElastiCache Redis (redis-sg)

Each layer trusts only the layer above it.


🧱 Step 1: Create a Backend Security Group (backend-sg)

Attach this SG to your EC2 backend
(FastAPI, Node.js, Django, etc.)

Inbound Rules

HTTP (80) → 0.0.0.0/0 HTTPS (443) → 0.0.0.0/0 SSH (22) → Your IP only (x.x.x.x/32)

Why this is correct

  • Web traffic is public
  • SSH is restricted
  • No database or cache ports are exposed

❌ Do NOT add

  • PostgreSQL (5432)
  • Redis (6379)
  • Custom internal ports

🗄️ Step 2: Create a Database Security Group (rds-sg)

Attach this SG only to your RDS instance.

Inbound Rules

PostgreSQL (5432) → Source: backend-sg

✔ Yes — the source is another security group, not an IP.

Why this is powerful

  • Only EC2s using backend-sg can connect
  • No public access
  • No IP allowlists to maintain

❌ Never use

0.0.0.0/0


🧠 Step 3: Create a Redis Security Group (redis-sg)

Attach this SG only to ElastiCache Redis.

Inbound Rules

Custom TCP (6379) → Source: backend-sg

Why this matters

  • Redis is one of the most attacked services
  • Public Redis = instant compromise
  • SG-to-SG access is the safest option

🚫 Step 4: Stop Using the Default Security Group

Your default SG should have NO inbound rules.

Best practice

  • Do not attach it to EC2
  • Do not attach it to RDS
  • Do not attach it to Redis

Think of it as a parking lot, not a firewall.


🔄 Step 5: Attach the Right SGs (Order Matters)

Always do this without downtime:

  1. Attach the new SG first
  2. Verify the app works
  3. Then remove the old SG

Example (EC2)

Before

default

After

backend-sg

⚠️ Never delete rules before attaching the correct SG.


🚨 Common Red Flags (Fix Immediately)

  • 5432 → 0.0.0.0/0
  • 6379 → 0.0.0.0/0
  • SSH open to the world
  • Databases using the default SG
  • One SG attached to everything

✅ Final Checklist for a “Perfect” Security Group Setup

  • ✅ Separate SG per service
  • ✅ SG-to-SG rules instead of IPs
  • ✅ No public DB or Redis ports
  • ✅ Default SG unused
  • ✅ SSH restricted or replaced with SSM

🏁 Final Thought

A perfect Security Group is not about opening fewer ports
it’s about opening ports only to the right source.

Once you adopt this pattern, your AWS infrastructure becomes:

  • Safer
  • Easier to reason about
  • Ready for production scale

#AWS#AWS EC2#Cloud Security

Read Next

Building RAG with Elasticsearch as a Vector Store
System Design

Building RAG with Elasticsearch as a Vector Store

Build a production-ready RAG system using Elasticsearch as a unified vector store. Learn how to integrate LangChain and Ollama for efficient document retrieval.

Using an MCP Server with LangGraph: A Practical Guide to MCP Adapters
AI Agent

Using an MCP Server with LangGraph: A Practical Guide to MCP Adapters

Learn how to integrate an MCP server with LangGraph using MCP adapters to build deterministic, schema-validated AI agents. This practical guide explains why prompt-only tool calling fails and how MCP enables reliable, production-grade agent workflows.

🚀 Turbopack in Next.js: Does turbopackFileSystemCacheForDev Make Your App Lightning Fast?
Next js

🚀 Turbopack in Next.js: Does turbopackFileSystemCacheForDev Make Your App Lightning Fast?

How to Create a Perfect AWS Security Group (Production-Ready & Secure)
Cloud Security

How to Create a Perfect AWS Security Group (Production-Ready & Secure)

Learn how to design a production-ready AWS Security Group using least-privilege principles for EC2, RDS, and Redis—without breaking your app. AWS Security Group best practices Secure EC2 Security Group RDS Security Group configuration Redis Security Group AWS AWS least privilege networking Cloud security for backend apps

Load Testing FastAPI: Can Your API Handle 1 Million Requests?
Backend Engineering

Load Testing FastAPI: Can Your API Handle 1 Million Requests?

Learn how to load test a FastAPI application using Apache JMeter to simulate one million requests, analyze throughput and latency, and uncover real production bottlenecks before traffic hits.

How to Use PostgreSQL for LangGraph Memory and Checkpointing with FastAPI
AI Engineering

How to Use PostgreSQL for LangGraph Memory and Checkpointing with FastAPI

A deep dive into real-world issues when integrating LangGraph with FastAPI and Postgres. Learn why async context managers break checkpointing, how to fix _AsyncGeneratorContextManager errors, create missing tables, and deploy LangGraph agents correctly in production.

Building a Simple AI Agent Using FastAPI, LangGraph, and MCP
AI Agents

Building a Simple AI Agent Using FastAPI, LangGraph, and MCP

Build a production-ready AI agent using FastAPI, LangGraph, and MCP. Learn how to design tool-calling agents with memory, Redis persistence, and clean workflow orchestration.

Using Celery With FastAPI: Solving Async Event Loop Errors Cleanly--
Backend Engineering

Using Celery With FastAPI: Solving Async Event Loop Errors Cleanly--

Learn why async/await fails inside Celery tasks when using FastAPI, and discover a clean, production-safe pattern to avoid event loop errors using internal FastAPI endpoints.Python FastAPI Celery AsyncProgramming BackendEngineering DistributedSystems Microservices

Sharding PostgreSQL for Django Applications with Citus
Databases

Sharding PostgreSQL for Django Applications with Citus

Scale your Django application horizontally by sharding PostgreSQL with the Citus extension. Improve performance with distributed storage and parallel queries.