IPv4 and IPv6 addressing protocols are supported by Amazon EC2 and Amazon VPC. Amazon VPC uses the IPv4 addressing protocol by default; you cannot change this behaviour. You must specify an IPv4 CIDR block when creating a VPC (a range of private IPv4 addresses). You can optionally assign an IPv6 CIDR block to your VPC and assign IPv6 addresses to instances in your subnets from that block. When we talk about IP Address, it comes in two types that is, Private and Public IP Address. An IP address that cannot be accessed over the Internet is known as a private IPv4 address. Instances within the same VPC can communicate using private IPv4 addresses. DHCP is used to distribute private IPv4 addresses to instances. On the other hand, a public IP address can be accessed via the Internet. Public addresses can be used to communicate between your instances and the Internet.
To make it publicly accessible from the internet, EC2 instances are usually assigned at least one public IP address when they are created, but in a situation where you encounter an error such as the one below when trying to connect to the instance, you need to manually allocate an IPv4 address to it.
The instance does not have a public IPv4 address. To connect using the EC2 Instance Connect browser-based client, the instance must have a public IPv4 address.
So, in this post, I will show you how to allocate, associate, disassociate and release a public IP address from an EC2 instance. First of all, let’s take a look at why this is necessary.
Refer to the following posts to learn more: How to add an EBS volume to AWS EC2 via the AWS Console and CLI, How to Add a New Virtual Disk to an EC2 Instance and Format the Drive, How to sync S3 Bucket with an EC2 instance. You can also see Quick Steps in Setting Up AWS VPC
Why do we need to Assign an Elastic IP Address to an EC2 Instance?
Reason 1 – You could do this if you receive an error similar to the one in the below screenshot when trying to connect to your EC2 instance.
Reason 2 – You may also need to do this if you want to assign a static IPv4 address to your EC2 instance, as the public IP address that’s auto-assigned to your EC2 instance changes every time you stop and start the instance. Assigning a static IP address to your Windows or Linux EC2 instance allows the instance to preserve its IP address after a stop-and-start process.
Keep the following points in mind when working with static IP addresses: - When you associate an Elastic IP address with an instance, its current public IPV4 address is released back into Amazon's pool of public IPv4 addresses - You can't reuse a public IPv4 address - You can't convert a public IPv4 address to an Elastic IP address The resolution for this is to use an Elastic IP address, first allocate the address to your account, and then associate the address with your instance or a network interface.
To follow along in the demonstration, the following things are required:
- An AWS Account with an active subscription. If you don’t have an account yet, you can sign up here for the free tier account.
- An IAM User account with permission to manage EC2 instances and VPC resources. Caution, don’t use the root account on a regular basis because it is the account with full access to all your AWS Resources.
If you have the above requirements, proceed with me to the demonstration tasks below.
Allocating a Elastic IP Address
Step 1 – To allocate a Elastic IP address to an existing EC2 instance, log in to the AWS Management Console
Step 2 – Navigate to the EC2 Console, in the navigation pane, choose “Network and Security,” and then choose Elastic IPs. Select “allocate an elastic IP address” in the top-right corner of the screen.
Step 3 – Select Amazon’s pool of IPv4 addresses. Choose this option if want an IPv4 address that’s allocated from Amazon’s pool of IPv4 addresses. Choose other options if you have a custom IP address you want to bring to your AWS Account.
Step 4 – Click on Allocate
After that, you will get a successful message prompt that the IP address has been successfully allocated.
Associating Elastic IP Address to your EC2 Instance
Next is to associate the IPv4 address we have just allocated from Amazon’s pool of IPv4 addresses to our EC2 instance.
To associate, you can do this directly from the immediate screen displaying a successful prompt of IP address allocation or you go into the EC2 instance console and locate the “Action” tab and implement it.
Let’s take the following steps to do it:
Step 1– From the EC2 Console, locate Network and Security and then click on Elastic IPs.
Step 2 – Click on the Action tab, and select Associate Elastic IP address
Step 3 – In the Associate Elastic IP address console, make sure the Instance under the Resouce type is checked and the EC2 instance you want to associate the IP address to and its Id is selected.
Next, proceed to click on Associate.
Now the prompt that you have successfully associated the IPv4 address to the EC2 instance displays.
If you navigate to the connect page for the EC2 instance, you will no longer receive the error that the instance doesn’t have a public IPv4 address anymore. Go ahead and connect to your EC2 instance.
Disassociating Elastic IP Address from an EC2 Instance
If your EC2 Instance keeps running or stops you will still incur charges, as far as Elastic IP addresses or EBS volumes are attached to the instance. One of the ways you are able to release the Elastic IP Addresses back to Amazon to save cost is to disassociate it from an instance.
To disassociate the Elastic IP address, follow the below steps:
Step 1 – Navigate to the Running Instance Console, click on the Action tab, then Network and click on Disassociate Elastic IP Address.
Step 32 – Click on Disassociate Elastic IP Address
Step 3 – Type “proceed” when prompted to confirm the disassociation.
Elastic IP Address disassociation successful.
Releasing Elastic IP Address
AWS recommend that you release an Elastic IP address if you no longer require it. You are charged for any Elastic IP address that is assigned to a VPC but is not associated with an instance. So the best way to save cost is to release the Elastic IPv4 address back to Amazon’s pool of IPv4 addresses.
Take the following steps to release the IPv4 address:
Step 1 – Navigate to the Elastic IPv4 Address console, select the IP address and click on the Action tab, and then
Step 2 – Click on Release to release the IP address
Finally, the successful screen shows up: