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
- Establish a management path first. Use NAT for required outbound internet access or a VPC endpoint for a supported service.
- Set
AssociatePublicIpAddress: falsefor new internal instances. Check existing public addresses, Elastic IPs, and IPv6 access separately. - 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