Security group rule allows all IPv4 or IPv6 addresses

Review whether all-address security group rules are needed, and restrict inbound and outbound access separately.

Description

0.0.0.0/0 and ::/0 allow every IPv4 and IPv6 address respectively, on the rule’s ports and protocols. Ingress specifies sources. Egress specifies destinations.

Potential impact

Wide ingress lets more clients try to reach a service. Wide egress lets workloads initiate connections to more destinations. The VPC default group initially allows ingress from its own members and egress to every IPv4 address, plus every IPv6 address in IPv6-enabled VPCs.

Remediation

  • Check all attached groups. Assign workload-specific groups and keep all-source access only on ports that need it, such as a public web listener.
  • Omitting rules_egress preserves an existing group’s egress. A new group can retain AWS’s allow-all default. To remove existing egress rules, use rules_egress: [] with purge_rules_egress: true. When purging either direction with purge_rules or purge_rules_egress, list every rule to keep. Replies to allowed connections are statefully permitted.

Examples

Both tasks manage the group named example in vpc_id. Supply the VPC, approved client CIDRs and AWS credentials. They show ingress on TCP 10050.

Before

yaml
---
- name: example ec2 group
  amazon.aws.ec2_security_group:
    name: example
    description: an example EC2 group
    vpc_id: "{{ vpc_id }}"
    region: eu-west-1
    rules:
      - proto: tcp
        from_port: 10050
        to_port: 10050
        cidr_ip:
          - 0.0.0.0/0

The first rule allows every IPv4 source.

After

yaml
- name: example ec2 group
  amazon.aws.ec2_security_group:
    name: example
    description: an example EC2 group
    vpc_id: "{{ vpc_id }}"
    region: eu-west-1
    rules:
      - proto: tcp
        from_port: 10050
        to_port: 10050
        cidr_ip: "{{ allowed_ipv4_cidr }}"
        cidr_ipv6: "{{ allowed_ipv6_cidr }}"

The second specifies separate IPv4 and IPv6 source ranges. Verify that new connections work only from the intended clients.

References