Public IP vs Private IP
In AWS EC2, Private IPs and Public IPs are used to define how an instance communicates within the AWS network and with the outside world.
Β
π Private IP
- Assigned to EC2 instance within a VPC
- Used for internal communication (within same VPC or peered VPCs)
- Non-routable on the internet
- Remains attached to the instance until it's stopped or terminated
- Example:
172.31.16.45
πΈ Use Case: App server talking to database server in private subnet.
π Public IP
- Mapped to the instanceβs private IP via NAT (Network Address Translation)
- Used for communication over the internet
- Assigned automatically (for default subnets) or manually (via Elastic IP)
- Changes if the instance is stopped and restarted (unless Elastic IP is used)
- Example:
3.109.221.105
πΈ Use Case: Web server accessed from the internet.
π Summary Comparison
Feature | Private IP | Public IP |
---|---|---|
Visibility | Internal (VPC only) | Global (Internet) |
Internet Access | β Directly No | β Yes |
Assignment | By VPC DHCP (or manual) | Auto-assigned or Elastic IP |
Persistence | Until stop/terminate | May change unless Elastic IP used |
Usage | Backend/internal traffic | Web apps, SSH, API calls from outside |
π Note:
- Elastic IP = Static public IP assigned to your account; can be reattached.
- Instances in private subnets usually donβt have public IPs and use NAT gateways for internet access.