What is Security Group in EC2?

A security group in EC2 is essentially a virtual firewall that controls inbound and outbound traffic to an EC2 instance. It acts as a set of rules that filter traffic based on protocols, ports, and IP addresses.

Here are some key points about security groups in EC2:

  1. Stateful Filtering: Security groups are stateful, meaning if you allow inbound traffic for a specific port, the corresponding outbound traffic for the response is automatically allowed, regardless of any outbound rules.
  2. Default Security Group: Every EC2 instance is associated with a default security group if you don’t specify one during instance launch. This group allows all outbound traffic by default and blocks all inbound traffic unless rules are added explicitly.
  3. Inbound and Outbound Rules: You can define inbound rules to control the traffic coming into your EC2 instances and outbound rules to control the traffic leaving your instances. These rules can be based on IP addresses, protocols, and ports.
  4. Dynamic Updates: Changes to security group rules take effect immediately. There’s no need to restart instances or services for the changes to apply.
  5. No Deny Rules: Security groups only have “allow” rules. If no rule explicitly allows traffic, it’s implicitly denied. Unlike traditional firewalls, you don’t define explicit “deny” rules.
  6. Stateless Filtering: While security groups are stateful, they don’t track the state of connections like a stateful firewall does. Each request is evaluated independently.
  7. Multiple Security Groups: You can associate multiple security groups with an EC2 instance. The rules from all associated security groups are aggregated to create the effective rules.

Overall, security groups provide a fundamental layer of security for your EC2 instances, allowing you to control access to your resources based on your specific requirements and security policies.