0
0
AWScloud~5 mins

Elastic IP addresses in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your cloud server needs a fixed public IP address that does not change even if you stop or restart it. Elastic IP addresses in AWS solve this by giving you a static IP you can attach to your server anytime.
When you want your server to have a fixed public IP address for users or services to connect to.
When you need to replace a server but keep the same IP address for continuity.
When your server restarts and you want to avoid changing IP addresses.
When you want to quickly remap your public IP to a different server in case of failure.
When you run a web service that requires a consistent IP for firewall rules or DNS.
Commands
This command requests a new Elastic IP address for your VPC. It reserves a static public IP that you can attach to your server.
Terminal
aws ec2 allocate-address --domain vpc
Expected OutputExpected
{ "PublicIp": "54.210.123.45", "AllocationId": "eipalloc-0abcd1234efgh5678" }
--domain - Specifies the scope of the Elastic IP; 'vpc' means it is for use in a VPC.
This command attaches the allocated Elastic IP to your running EC2 instance so it uses the fixed public IP.
Terminal
aws ec2 associate-address --instance-id i-0123456789abcdef0 --allocation-id eipalloc-0abcd1234efgh5678
Expected OutputExpected
{ "AssociationId": "eipassoc-1234abcd5678efgh" }
--instance-id - Specifies which EC2 instance to attach the Elastic IP to.
--allocation-id - Specifies the Elastic IP allocation to associate.
This command checks details about the Elastic IP, including which instance it is attached to.
Terminal
aws ec2 describe-addresses --allocation-ids eipalloc-0abcd1234efgh5678
Expected OutputExpected
{ "Addresses": [ { "InstanceId": "i-0123456789abcdef0", "PublicIp": "54.210.123.45", "AllocationId": "eipalloc-0abcd1234efgh5678", "AssociationId": "eipassoc-1234abcd5678efgh" } ] }
--allocation-ids - Filters the output to show information about a specific Elastic IP.
This command detaches the Elastic IP from the EC2 instance, freeing it for reassignment.
Terminal
aws ec2 disassociate-address --association-id eipassoc-1234abcd5678efgh
Expected OutputExpected
No output (command runs silently)
--association-id - Specifies which Elastic IP association to remove.
This command releases the Elastic IP back to AWS so you no longer pay for it and it can be assigned to others.
Terminal
aws ec2 release-address --allocation-id eipalloc-0abcd1234efgh5678
Expected OutputExpected
No output (command runs silently)
--allocation-id - Specifies which Elastic IP allocation to release.
Key Concept

If you remember nothing else from this pattern, remember: Elastic IPs give your cloud server a fixed public IP that you control and can move between servers.

Common Mistakes
Trying to associate an Elastic IP without allocating it first.
You cannot attach an IP that AWS has not reserved for you, so the command fails.
Always run allocate-address first to get an Elastic IP before associating it.
Not specifying the correct instance ID when associating the Elastic IP.
The Elastic IP will not attach to the intended server, causing connectivity issues.
Double-check the instance ID and use the exact value when associating.
Forgetting to disassociate before releasing the Elastic IP.
AWS will not allow releasing an Elastic IP that is still attached to an instance.
Always disassociate the Elastic IP before releasing it.
Summary
Allocate an Elastic IP to reserve a fixed public IP address.
Associate the Elastic IP with your EC2 instance to give it a static IP.
Use describe-addresses to check the Elastic IP status and attachment.
Disassociate the Elastic IP before releasing it to avoid errors.
Release the Elastic IP when you no longer need it to avoid charges.