How to Attach an Elastic IP Address to an AWS EC2 Instance

Elastic IP addresses are static public IPv4 addresses provided by AWS, designed for dynamic cloud computing environments. They enable seamless connectivity to your Amazon EC2 instances, even if the underlying instance’s IP address changes. In this guide, we’ll walk through the steps to attach an Elastic IP address to your EC2 instance.
Why Use Elastic IPs?
When you launch an EC2 instance, it is automatically assigned a public IP address. However, this public IP is dynamic, meaning it changes every time you stop and start the instance. Elastic IP addresses, on the other hand, are persistent across instance stops and starts, making them ideal for applications that require a consistent IP address, such as a web server.
Step 1: Allocate an Elastic IP Address
Before attaching an Elastic IP to your EC2 instance, you need to allocate one.
- Login to the AWS Management Console Start by logging into your AWS account and navigating to the EC2 Dashboard.
- Open the Elastic IPs Section In the left-hand menu under “Network & Security,” click on Elastic IPs. This is where you’ll manage your IP addresses.
- Allocate a New Elastic IP Address Click on the Allocate Elastic IP address button at the top right. AWS will allocate a new Elastic IP address for your account. Once allocated, you’ll see the IP address listed in the Elastic IP dashboard.
Step 2: Attach the Elastic IP Address to an EC2 Instance
Once you’ve allocated your Elastic IP, the next step is to attach it to an instance.
- Select the Elastic IP In the Elastic IPs section, select the IP address you want to attach.
- Associate the Elastic IP Address With the Elastic IP selected, click the Actions dropdown and choose Associate Elastic IP address. A new screen will appear where you can configure the association.
- Select the EC2 Instance In the Associate Elastic IP Address page:
- Under Resource type, choose Instance.
- Select your EC2 instance from the dropdown menu.
- You can also specify the private IP address of your instance if you have multiple private IPs. Typically, you’ll want to leave this as the default.
- Confirm the Association Once you’ve selected your instance, click the Associate button. AWS will now associate the Elastic IP with your EC2 instance.
Step 3: Verify the Elastic IP Attachment
After the Elastic IP has been associated with your instance, it’s important to verify that it’s working correctly.
- Navigate to the EC2 Instances Section Go back to the EC2 Dashboard and select Instances from the left-hand menu.
- Check Public IP Find your instance in the list and check its details. You should see the newly attached Elastic IP listed under the Public IPv4 address field.
- Test Connectivity To ensure everything is working, try accessing your instance using the Elastic IP address. If it’s a web server, open a browser and type in the Elastic IP. If you’re using SSH, connect using the Elastic IP instead of the instance’s public IP.
Detaching and Releasing Elastic IPs
If you ever need to detach an Elastic IP from an instance or release it, simply navigate to the Elastic IPs section, select the IP, and choose the appropriate action from the Actions dropdown. Be mindful that AWS charges for Elastic IPs that are allocated but not in use.
Best Practices for Using Elastic IPs
- Limit Elastic IP Usage: AWS provides a limited number of Elastic IPs per region (typically five). Only use Elastic IPs when necessary, and release them when they are no longer needed.
- Use Route 53: If your application relies heavily on static IP addresses, consider using AWS Route 53 (DNS) in conjunction with Elastic IPs to simplify domain management.
Conclusion
Attaching an Elastic IP address to an EC2 instance is a straightforward process that provides a consistent IP address, even if the instance is stopped or restarted. This can be crucial for hosting public-facing services like websites or APIs. With Elastic IPs, you can ensure seamless connectivity and avoid the complications of dynamic public IPs.
By following this guide, you’ll be able to manage your EC2 instances more effectively, ensuring your services remain accessible regardless of changes in your instance’s state.