Public EC2 instance exposure through its subnet

Review EC2 public addresses, routing, and access rules together to reduce unnecessary internet exposure.

Description

An EC2 instance with a public IPv4 address can accept direct internet connections when an internet gateway route and access rules allow them.

Potential impact

Broad inbound rules can expose internal services. Removing an existing path can also interrupt administration or service dependencies.

Remediation

  1. Establish a management path first. Use NAT for required outbound internet access or a VPC endpoint for a supported service.
  2. Set AssociatePublicIpAddress: false for new internal instances. Check existing public addresses, Elastic IPs, and IPv6 access separately.
  3. Limit public services to required traffic in security groups and network ACLs. Keep application authentication and authorization in place.

Examples

Supply your AMI and subnet references. The excerpts define separate instances and omit routes and security groups.

Before

yaml
Resources:
  PublicInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0ff8a91507f77f867
      NetworkInterfaces:
        - AssociatePublicIpAddress: true
          DeviceIndex: "0"
          SubnetId: !Ref PublicSubnet

After

yaml
Resources:
  PrivateInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0ff8a91507f77f867
      NetworkInterfaces:
        - AssociatePublicIpAddress: false
          DeviceIndex: "0"
          SubnetId: !Ref PrivateSubnet

References