How to Use Elastic IP in AWS: Simple Guide
To use an
Elastic IP in AWS, first allocate an Elastic IP address in your AWS account, then associate it with an EC2 instance or a network interface. This gives your resource a fixed public IP that stays the same even if you stop or restart the instance.Syntax
Using Elastic IPs involves three main steps:
- Allocate Elastic IP: Reserve a public IP address in your AWS account.
- Associate Elastic IP: Attach the allocated IP to an EC2 instance or network interface.
- Disassociate and Release: Remove the IP from the resource and free it when no longer needed.
These steps can be done via AWS Console, CLI, or SDKs.
bash
aws ec2 allocate-address --domain vpc aws ec2 associate-address --instance-id i-1234567890abcdef0 --allocation-id eipalloc-12345678 aws ec2 disassociate-address --association-id eipassoc-12345678 aws ec2 release-address --allocation-id eipalloc-12345678
Example
This example shows how to allocate an Elastic IP and associate it with an EC2 instance using AWS CLI.
bash
aws ec2 allocate-address --domain vpc # Output includes AllocationId, e.g. eipalloc-12345678 aws ec2 associate-address --instance-id i-0abcdef1234567890 --allocation-id eipalloc-12345678 # Output includes AssociationId, e.g. eipassoc-87654321
Output
{
"PublicIp": "203.0.113.25",
"AllocationId": "eipalloc-12345678",
"Domain": "vpc"
}
{
"AssociationId": "eipassoc-87654321"
}
Common Pitfalls
- Not specifying
--domain vpcwhen allocating Elastic IPs for VPC causes allocation in EC2-Classic, which is deprecated. - Trying to associate an Elastic IP to an instance in a different region than the IP causes errors.
- Forgetting to release Elastic IPs when not in use leads to unnecessary charges.
- Associating Elastic IPs to stopped instances still incurs charges; disassociate if not needed.
bash
aws ec2 allocate-address # Wrong: allocates in EC2-Classic (legacy) aws ec2 allocate-address --domain vpc # Correct: allocates in VPC aws ec2 associate-address --instance-id i-0abcdef1234567890 --allocation-id eipalloc-12345678 # Make sure instance and allocation are in the same region
Quick Reference
| Action | AWS CLI Command | Notes |
|---|---|---|
| Allocate Elastic IP | aws ec2 allocate-address --domain vpc | Reserves a public IP in your VPC |
| Associate Elastic IP | aws ec2 associate-address --instance-id | Attaches IP to EC2 instance |
| Disassociate Elastic IP | aws ec2 disassociate-address --association-id | Removes IP from instance |
| Release Elastic IP | aws ec2 release-address --allocation-id | Frees the IP to avoid charges |
Key Takeaways
Always allocate Elastic IPs with --domain vpc for modern AWS setups.
Associate Elastic IPs only with instances in the same AWS region.
Release Elastic IPs when not in use to avoid extra costs.
Elastic IPs provide a fixed public IP that persists across instance stops and starts.
Use AWS CLI or Console to manage Elastic IPs easily.