← Back to Questions
AWS

Explain about EC2 Security Groups?

Learn Explain about EC2 Security Groups? with simple explanations, real-time examples, interview tips and practical use cases.

EC2 Security Groups are virtual firewalls in AWS that control inbound and outbound traffic for EC2 instances.

They help secure cloud infrastructure by allowing only authorized network traffic.

Simple Definition: A Security Group is a stateful virtual firewall attached to AWS resources that controls which traffic is allowed to enter or leave an EC2 instance.

Why Security Groups are Important

When servers are exposed to the internet, they become targets for:

  • Hackers
  • Unauthorized access
  • Port scanning attacks
  • Malware
  • DDoS attempts

Security Groups protect EC2 instances by filtering traffic at the instance level.

High-Level Security Group Architecture

Internet Traffic
       |
Security Group Rules
       |
EC2 Instance
    

How Security Groups Work

Security Groups evaluate traffic based on defined rules.

Traffic is:

  • Allowed if a rule matches
  • Denied if no rule matches

Traffic Flow Example

Request to Port 80
       |
Security Group Checks Rules
       |
Allowed or Blocked
    

Main Characteristics of Security Groups

Feature Description
Stateful Return traffic automatically allowed
Instance Level Attached directly to EC2 instances
Allow Rules Only Cannot create explicit deny rules
Multiple Groups Supported One instance can use multiple groups

What Does Stateful Mean?

Security Groups are stateful firewalls.

If inbound traffic is allowed, the response traffic is automatically allowed.

Example

User Accesses Website on Port 80
        |
Inbound HTTP Allowed
        |
Response Automatically Allowed
    

Inbound Rules

Inbound rules control incoming traffic to EC2 instances.

Common Inbound Rules

Port Protocol Purpose
22 SSH Linux remote login
3389 RDP Windows remote login
80 HTTP Web traffic
443 HTTPS Secure web traffic
3306 MySQL Database access

Inbound Traffic Flow

Internet User
      |
Port 443 Request
      |
Security Group Rule
      |
EC2 Web Server
    

Outbound Rules

Outbound rules control traffic leaving EC2 instances.

Example

EC2 Server
      |
Outgoing API Call
      |
Outbound Rule Validation
      |
External Service
    

Default Security Group Behavior

Traffic Type Default Action
Inbound Traffic Blocked
Outbound Traffic Allowed

Security Group Components

Component Purpose
Protocol TCP, UDP, ICMP
Port Range Application ports
Source/Destination Allowed IP addresses

Example Security Group Rule

Protocol: TCP
Port: 443
Source: 0.0.0.0/0
Description: Allow HTTPS Traffic
    

Understanding CIDR Blocks

CIDR blocks define allowed IP ranges.

CIDR Meaning
0.0.0.0/0 Allow all internet traffic
192.168.1.0/24 Private subnet range
10.0.0.0/16 VPC internal network

Security Group Example for Web Application

Web Server Security Group
--------------------------------
Allow HTTP  (80)  from Internet
Allow HTTPS (443) from Internet
Allow SSH   (22)  from Admin IP
--------------------------------
    

Database Security Group Example

Database Security Group
--------------------------------
Allow MySQL (3306)
Only from Web Server Security Group
--------------------------------
    

Security Group Referencing

Security Groups can reference other Security Groups instead of using IP addresses.

Architecture Example

Web Server SG
       |
Allowed to Access
       |
Database SG
    

This improves security and simplifies management.

Multi-Tier Architecture Example

Internet
    |
Load Balancer SG
    |
Web Server SG
    |
Application SG
    |
Database SG
    

Security Groups vs Network ACLs

Feature Security Group Network ACL
Level Instance level Subnet level
State Stateful Stateless
Deny Rules Not supported Supported

Real-World Production Architecture

Users
   |
Application Load Balancer
   |
-----------------------------------
| Web Server Security Group |
-----------------------------------
   |
-----------------------------------
| Application Security Group |
-----------------------------------
   |
-----------------------------------
| Database Security Group |
-----------------------------------
    

Common Security Group Mistakes

  • Opening SSH to the entire internet
  • Allowing unrestricted database access
  • Using broad CIDR ranges unnecessarily
  • Ignoring outbound security rules

Example of Dangerous Rule

Port 22
Source: 0.0.0.0/0
    

This allows anyone on the internet to attempt SSH access.

Best Practices for Security Groups

  • Use least privilege access
  • Restrict SSH access to specific IPs
  • Separate security groups by application tier
  • Use security group references
  • Regularly audit rules
  • Enable logging and monitoring

Security Group Monitoring

AWS services used for monitoring:

  • CloudWatch
  • VPC Flow Logs
  • CloudTrail
  • GuardDuty

Example Attack Prevention

Hacker Attempts Database Access
        |
Security Group Blocks Port 3306
        |
Database Protected
    

Security Groups in DevOps

DevOps teams often manage Security Groups using Infrastructure as Code (IaC).

Popular Tools

  • Terraform
  • AWS CloudFormation
  • Ansible

Example Terraform Logic

Create Security Group
       |
Allow HTTP and HTTPS
       |
Attach to EC2 Instances
    

Production Security Strategy

  • Public access only to Load Balancers
  • Private EC2 instances in private subnets
  • Databases isolated from internet access
  • Use bastion hosts for administration

Security Group Lifecycle

Create Security Group
        |
Add Rules
        |
Attach to EC2
        |
Monitor and Update
    

Interview Answer

EC2 Security Groups are stateful virtual firewalls in AWS that control inbound and outbound traffic for EC2 instances.

They work at the instance level and allow only authorized traffic based on defined rules such as:

  • Protocol
  • Port number
  • IP address or CIDR range

Security Groups are commonly used to secure web servers, application servers, databases, and cloud-native applications.

Quick Summary Table

Feature Security Group Behavior
Firewall Type Virtual firewall
Scope Instance level
State Stateful
Deny Rules Not supported
Inbound Default Deny all
Outbound Default Allow all

Useful Internal Links

Final Conclusion

EC2 Security Groups are one of the most critical security components in AWS cloud infrastructure.

They provide instance-level traffic filtering and help organizations secure applications, databases, APIs, and enterprise workloads.

Understanding Security Groups is essential for cloud engineers, DevOps professionals, architects, and cybersecurity teams working with AWS.

Why this AWS question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.