AWS Builder Center

AWS Security Groups

How AWS manage the in-flux and out-flux of traffic.

AWS Security Groups: The Firewall Protecting Your EC2 Instance

When deploying an application on AWS EC2, getting the application running is only half the job.
The next question is:
Who should be allowed to connect to my server?
This is where AWS Security Groups become extremely important.
A Security Group acts as a virtual firewall that controls network traffic going to and from AWS resources such as EC2 instances.



What Is a Security Group?

Think of an EC2 instance as a house.
Your application might have several doors:
Port 22 → SSH
Port 80 → HTTP
Port 443 → HTTPS
Port 8080 → Spring Boot

A Security Group determines which of these doors are open and who is allowed to use them.
For example:
Internet
|
+---- Port 80 → Web traffic
|
+---- Port 443 → HTTPS traffic
|
+---- Port 22 → SSH
|
X---- Port 8080 → Blocked from public access

This simple concept becomes extremely important when deploying real applications.



Inbound Rules

Inbound rules determine what traffic is allowed to enter your EC2 instance.
For example:
Type
Port
Source
SSH
22
My IP
HTTP
80
Anywhere
HTTPS
443
Anywhere
This means:
  • SSH access is restricted to my IP.
  • HTTP traffic can come from the internet.
  • HTTPS traffic can come from the internet.
Any traffic that doesn't match an allowed inbound rule is denied.



Outbound Rules

Security Groups also control outbound traffic.
For example, an EC2 application might need to communicate with:
EC2
|
+----> RDS
|
+----> S3
|
+----> External APIs

These outbound connections need to be permitted by the Security Group configuration.



Why Shouldn't We Open Port 22 to Everyone?

One of the most common beginner configurations is:
SSH
Port: 22
Source: 0.0.0.0/0

This means:
Anyone on the internet can attempt to connect to the SSH service.
Even if authentication is still required, unnecessarily exposing SSH increases the attack surface.
For development environments, restricting SSH to your own IP is usually much better:
SSH
Port: 22
Source: My IP

The general principle is:
Don't expose a service to the internet if it doesn't need to be publicly accessible.



Security Groups and Spring Boot

Suppose we have a Spring Boot application running on:
EC2
|
└── Spring Boot :8080

A beginner might create:
Inbound:
8080 → 0.0.0.0/0

Now anyone who knows the public IP can potentially reach the application directly.
A better architecture is often:
Internet
|
v
Nginx :80/:443
|
v
Spring Boot :8080

In this setup, users don't need direct access to port  8080 .
Instead, Nginx can communicate with the Spring Boot application locally.
This gives us a cleaner separation between the public-facing web server and the application.



Security Groups Between EC2 and RDS

Security Groups become even more interesting when we introduce a database.
Imagine:
Spring Boot
|
v
EC2
|
v
RDS PostgreSQL

We don't want PostgreSQL to be publicly accessible.
Instead, we can configure the RDS Security Group to allow PostgreSQL traffic only from the EC2 application's Security Group.
Conceptually:
EC2 Security Group
|
| PostgreSQL :5432
v
RDS Security Group

Notice the difference.
We aren't saying:
5432 → Anyone

We're saying:
5432 → Application servers

This is a much stronger security model.



Security Groups Are Stateful

One useful characteristic of Security Groups is that they are stateful.
If an inbound connection is allowed and the application sends a response, the response traffic is automatically allowed.
For example:
Client
|
| Request
v
EC2
|
| Response
v
Client

We don't need to create a separate inbound rule just for the response traffic.



Security Group vs Network ACL

While learning AWS networking, I initially found Security Groups and Network ACLs confusing.
A simple way to remember the difference is:

Security Group

Works at the resource level.
EC2
|
+-- Security Group

It controls traffic associated with the resource.

Network ACL

Works at the subnet level.
VPC
|
+-- Subnet
|
+-- Network ACL

For many beginner deployments, Security Groups are the first networking security mechanism to understand deeply.



A Practical Example

Suppose I'm deploying a Spring Boot application with:
Frontend
|
v
Nginx
|
v
Spring Boot
|
v
PostgreSQL

A possible security configuration could look like:
Internet
|
| 80 / 443
v
EC2
|
| 5432
v
RDS

Security rules:
EC2 Security Group
------------------
80 → Internet
443 → Internet
22 → My IP

RDS Security Group
------------------
5432 → EC2 Security Group

This creates a simple but meaningful security boundary.



The Principle of Least Privilege

Security Groups also teach a broader security principle:
Give access only where it is required.
If an application doesn't need public access to port  8080 , don't expose it.
If PostgreSQL doesn't need to accept connections from the internet, don't expose port  5432 .
If SSH is only required by administrators, restrict it accordingly.
Every unnecessary open port increases the potential attack surface.



What I Learned

Working with AWS Security Groups helped me understand that cloud security isn't something we add after deploying an application.
It needs to be part of the architecture from the beginning.
Instead of asking:
"Which ports should I open?"
A better question is:
"Which connections does my application actually need?"
Once we think about the architecture in terms of required connections, Security Group rules become much easier to design.



Final Takeaway

An EC2 instance is powerful, but putting a server on the internet without understanding network access can create unnecessary security risks.
Security Groups provide a simple way to control that access.
The key concepts to remember are:
  • 🔐 Inbound rules control incoming traffic.
  • 🚀 Outbound rules control outgoing traffic.
  • 🎯 Restrict administrative access whenever possible.
  • 🗄️ Keep databases private when they don't need public access.
  • 🔗 Security Groups can reference other Security Groups.
  • 🛡️ Follow the principle of least privilege.
AWS becomes much easier to understand when we stop looking at services individually and start thinking about how they communicate with each other.
That's the mindset I'm building while learning cloud architecture. 🚀
#AWS #AWSCloud #AmazonEC2 #SecurityGroups #CloudSecurity #DevOps #SpringBoot #Java #CloudComputing #AWSArchitecture
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article