Stable Addresses with Elastic IP

An Elastic IP is a fixed public IP you can keep even when instances change.

By default an EC2 instance gets a public IP that changes every time it stops and starts. That breaks DNS and anything hard-coded to the old address. An Elastic IP (EIP) is a static public IP you own and can attach to any instance.

When to use one

  • A single server whose address must stay constant.
  • Allow-listing an outbound IP with a third party.

When not to

For anything that scales to more than one instance, front it with a load balancer and point DNS at that instead. The load balancer's name is stable and hides the individual instances.

Example

Example Β· bash
# Allocate an Elastic IP and attach it to an instance
ALLOC_ID=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)

aws ec2 associate-address \
  --instance-id i-0123456789abcdef0 \
  --allocation-id $ALLOC_ID

When to use it

  • A team allocates an Elastic IP and associates it with their EC2 instance so DNS records remain valid when the instance is replaced.
  • A firewall rule at a customer's office is written against a fixed IP, so the team uses an Elastic IP to ensure it never changes.
  • An ops engineer moves an Elastic IP from a failed instance to a replacement instance in under a minute to restore service.

More examples

Allocate and associate Elastic IP

Allocates a static public IP and attaches it to an EC2 instance so the IP survives stop/start cycles.

Example Β· bash
# Allocate a new Elastic IP
ALLOC_ID=$(aws ec2 allocate-address \
  --domain vpc \
  --query 'AllocationId' \
  --output text)

# Associate it with a running instance
aws ec2 associate-address \
  --instance-id i-0abc12345 \
  --allocation-id $ALLOC_ID

Reassign EIP to a new instance

Moves an Elastic IP from one instance to another to redirect traffic without changing DNS records.

Example Β· bash
# Disassociate from the old instance
aws ec2 disassociate-address \
  --association-id eipassoc-0abc1234

# Reassociate with the replacement instance
aws ec2 associate-address \
  --instance-id i-0new56789 \
  --allocation-id eipalloc-0def5678

List allocated Elastic IPs

Lists all Elastic IPs in the account with their allocation IDs and which instance each is attached to.

Example Β· bash
aws ec2 describe-addresses \
  --query 'Addresses[*].[PublicIp,AllocationId,InstanceId,AssociationId]' \
  --output table

Discussion

  • Be the first to comment on this lesson.
Stable Addresses with Elastic IP β€” AWS Deployment | SoundsCode