Understanding Public and Private IP Behavior in AWS EC2

When working with AWS EC2 instances, it's important to understand how public and private IP addresses behave, especially when stopping, starting, and connecting to instances. In this article, we’ll explore these concepts and how to use Elastic IPs to maintain a persistent public IP.


1. Public vs. Private IPv4 in AWS

Each EC2 instance is assigned both a public IPv4 and a private IPv4:

Example Scenario: Connecting via SSH

  1. An instance is running with a public IPv4 (e.g., 54.123.45.67).

  2. Using this IP, we can connect via:

    ssh -i my-key.pem [email protected]
    
  3. Once connected, we can see the private IP assigned to the instance (e.g., 10.0.0.5).

  4. If we try to SSH using the private IP from an external network, it won't work since private IPs are not accessible over the internet.


2. What Happens When an Instance is Stopped and Started?

Example Test:

  1. Stop the EC2 instance.
  2. Note down the public IPv4 before stopping.