Security group exposes a port range to all addresses

Review whether a port range needs access from all sources, and retain only the access each service requires.

Description

Give public service ports and administrative ports separate rules. Keep consecutive TCP or UDP ports where the service needs them. ICMP uses types and codes, and port values cannot narrow an all-protocol rule.

Potential impact

When routing and listeners permit it, external clients can reach unnecessary ports. Another attached group may keep granting access after you narrow one rule.

Remediation

Check that splitting the rules actually narrows their sources or ports. With the default purge_rules: true, list all existing rules that must remain. Test allowed and blocked connections over the user and management paths, and check service authentication.

Examples

Supply AWS credentials in the execution environment, the target vpc_id and the management network’s admin_cidr. The tasks manage the same group. Attachments and routes are configured separately.

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: 80
        to_port: 82
        cidr_ip: 0.0.0.0/0

The first rule allows TCP 80–82 from all IPv4 sources.

After

yaml
- name: example ec2 group v2
  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: 80
        to_port: 80
        cidr_ip: 0.0.0.0/0
      - proto: tcp
        from_port: 22
        to_port: 22
        cidr_ip: "{{ admin_cidr }}"

The second keeps only TCP 80 public and adds TCP 22 from the management range. Use both permissions only if both services are needed.

References