EC2 instance has a public IP address

Review whether an EC2 public IP is needed and restrict Internet exposure with routing and security groups.

Description

Check whether an internal EC2 instance needs a public IPv4 address. An internet gateway route and permissive network rules can make a listening service reachable from the internet.

Potential impact

A publicly reachable service or management port can receive unwanted sign-in attempts or allow unauthorized access.

Remediation

  1. Prepare the paths you need. Use a load balancer for a public service, a VPN or Systems Manager for administration, and a NAT gateway for outbound internet access as appropriate.
  2. For a new internal instance, select a private subnet and set assign_public_ip to false. Limit security groups and network ACLs to required traffic.
  3. Review existing interface addresses, Elastic IPs, and IPv6 routes separately. Test application and management connectivity.

Examples

These tasks use the older network setting. With amazon.aws 8.2.0 or later, a launch that creates a single primary interface can set assign_public_ip on its network_interfaces entry. Supply your image, subnet, and security groups.

Before

yaml
- name: start an instance with a public IP address
  community.aws.ec2_instance:
    name: public-compute-instance
    vpc_subnet_id: subnet-5ca1ab1e
    instance_type: c5.large
    network:
      assign_public_ip: true

After

yaml
- name: start an instance without a public IP address
  community.aws.ec2_instance:
    name: private-compute-instance
    vpc_subnet_id: subnet-5ca1ab1e
    instance_type: c5.large
    network:
      assign_public_ip: false

The tasks launch differently named instances in the same subnet. Review existing instances separately.

References