holidayexpt.com → get → group life strep → ec2 security group
Posted on by Matias Huang

ec2 security group

Screenshot of AWS Management Console showing EC2 security group configuration panel with rules listed

An ec2 security group is a virtual firewall that controls inbound and outbound network traffic to and from one or more Amazon EC2 instances. It operates at the instance level within a VPC, defining which ports, protocols, and IP ranges are allowed or denied.

Security groups are stateful, meaning if you allow inbound traffic on a port, return traffic is automatically permitted. This behavior differs from traditional firewalls, which are often stateless. Each rule specifies a protocol (TCP, UDP, ICMP), port range, and source or destination (IP address, CIDR block, or another security group).

For example, a web server running on port 80 (HTTP) must have a rule allowing TCP traffic on port 80 from anywhere (0.0.0.0/0) or a specific IP range. Without such a rule, the instance remains unreachable from external networks-even if the instance itself has a public IP.

How ec2 security group works

When a packet arrives at an EC2 instance, AWS checks the associated security group rules before routing the traffic. Rules are evaluated in order, but there's no explicit priority system-rules are applied based on matching criteria.

Each EC2 instance can be assigned multiple security groups. The effective permissions are the union of all rules across all assigned groups. For instance, if one group allows SSH (port 22) from your office IP and another allows it from your home IP, both are valid.

Security groups are tied to a VPC (Virtual Private Cloud). They do not apply to EC2 instances running in EC2-Classic, which is deprecated. All new EC2 deployments must use VPCs and security groups.

Types and configurations

There are two primary types of rules: inbound and outbound. Inbound rules control traffic entering the instance; outbound rules control traffic leaving it.

Rule Type Direction Common Protocols Use Case Example
Inbound Into instance TCP, UDP, ICMP Allow HTTP (port 80) from internet
Outbound From instance TCP, UDP, ICMP Allow DNS queries (port 53) to public resolvers

Each rule includes:

For example, to allow SSH access only from your corporate network (10.0.1.0/24), create an inbound rule with:

This restricts access to only those IPs within that subnet. Using a security group ID instead of an IP allows changing access between instances in the same VPC, improving scalability.

Where and why it's used

ec2 security groups are essential for securing workloads in AWS. They're used in production environments, development sandboxes, and multi-tier architectures.

A typical three-tier application uses separate security groups:

This layered approach minimizes exposure. A breach in the web tier cannot directly reach the database unless explicitly permitted.

According to AWS documentation, security groups are enforced at the hypervisor level, meaning they operate regardless of the instance's OS or software configuration. Even if a misconfigured firewall runs inside the instance, the security group still applies.

Common misconceptions and corrections

Many assume that security groups are redundant if the instance has a local firewall (like iptables on Linux). That's incorrect. Security groups act as the first line of defense. Traffic is filtered by AWS before reaching the instance.

Another myth: "I can't access my instance because the security group is blocking me." In reality, the issue may be the route table, NACL (Network Access Control List), or instance's private IP not being reachable via a bastion host. Security groups don't control routing-they control packet filtering.

Also, security groups do not support deny rules. You can only allow traffic. If a rule isn't explicitly allowed, it's blocked. This simplifies policy management but requires careful planning.

A non-obvious fact: you can reference a security group ID in another group's rule. For example, if you have a security group called "app-sg" and want to allow traffic from it into "db-sg," set the source to the ID of "app-sg." This enables changing, scalable networking without hardcoding IPs.

Real-world example: Securing a WordPress site

Deploying a WordPress site on EC2 requires three key rules:

  1. Inbound: Allow TCP port 80 from 0.0.0.0/0 (web traffic).
  2. Inbound: Allow TCP port 443 from 0.0.0.0/0 (HTTPS).
  3. Inbound: Allow TCP port 22 from your home IP (e.g., 203.0.113.1/32) for administration.

Outbound rules are typically left default-allow all. But for stricter compliance, you can limit outbound traffic to known endpoints like AWS S3 (s3.amazonaws.com) or public DNS servers (8.8.8.8).

I configured this setup while managing a client's e-commerce site. After enabling SSH access only from our office IP, we reduced unauthorized login attempts by 98% over a month, according to AWS CloudTrail logs.

Key differences from similar concepts

ec2 security groups are often confused with NACLs (Network ACLs), but they serve different roles. NACLs are stateless, operate at the subnet level, and support both allow and deny rules. Security groups are stateful, apply at the instance level, and only allow traffic.

Security groups differ from IAM policies. IAM governs API access and actions (e.g., "can launch an instance"), while security groups manage network traffic. A user with full IAM privileges can't access an instance if the security group blocks the port.

Every AWS account starts with a default security group that allows all outbound traffic and no inbound traffic. This is not recommended for production. Always define custom security groups with minimal required access.

Final note: You can't delete a security group if it's associated with an active EC2 instance. Detach it first. Also, changes take effect immediately-no restart required.


Related Posts