{"id":9293,"date":"2024-03-08T13:04:45","date_gmt":"2024-03-08T13:04:45","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/using-a-bash-script-to-associate-an-elastic-ip-address-to-an-ec2-instance\/"},"modified":"2025-01-20T11:47:39","modified_gmt":"2025-01-20T11:47:39","slug":"using-a-bash-script-to-associate-an-elastic-ip-address-to-an-ec2-instance","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/using-a-bash-script-to-associate-an-elastic-ip-address-to-an-ec2-instance\/","title":{"rendered":"Using a Bash Script to Associate an Elastic IP Address to an EC2 instance"},"content":{"rendered":"<div>In previous articles, I explained how to <a href=\"https:\/\/4sysops.com\/archives\/create-an-amazon-machine-image-ami-of-an-ec2-instance-with-a-bash-and-powershell-script\/\" target=\"_blank\" rel=\"nofollow noopener\">create an AMI from an EC2 instance<\/a> and then <a href=\"https:\/\/4sysops.com\/archives\/launch-ec2-instance-from-an-ami-with-a-bash-and-powershell-script\/\" target=\"_blank\" rel=\"nofollow noopener\">launch the instance<\/a> with scripts. Next, you probably want to assign an Elastic IP (IPv4 address) to the instance. In this post, you will learn how to allocate, release, associate, and disassociate an Elastic IP with the AWS CLI and bash scripts.<\/div>\n<p>An Elastic IP address in EC2 is a static IPv4 address that can be reassigned to different AWS resources for flexible IP management. Before you can assign an Elastic IP address, you have to allocate it in the AWS Console or with the AWS CLI. In the AWS Console, you can find the Elastic IP addresses on the EC2 tab under Networking and Security.<\/p>\n<p>Please note that since February 2024 <a href=\"https:\/\/aws.amazon.com\/blogs\/aws\/new-aws-public-ipv4-address-charge-public-ip-insights\/\" target=\"_blank\" rel=\"nofollow noopener\">Amazon charges $0.005<\/a> per hour for in-use public IPv4 address usage. Public IPv4 addresses that are not in use have always incurred a cost, and <a href=\"https:\/\/aws.amazon.com\/vpc\/pricing\/\" target=\"_blank\" rel=\"nofollow noopener\">the rate is $0.005 as well<\/a>.<\/p>\n<p>In this guide, I assume that have you <a href=\"https:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/getting-started-install.html\" target=\"_blank\" rel=\"nofollow noopener\">installed the AWS CLI<\/a>.<\/p>\n<h2>Allocate an Elastic IP address<\/h2>\n<p>The command below allocates a public Elastic IP address and displays it (&#8211;output text).<\/p>\n<pre>aws ec2 allocate-address --query 'PublicIp' --output text<\/pre>\n<p>And this command will list all your Elastic IPs:<\/p>\n<pre>aws ec2 describe-addresses --query 'Addresses[*].[AllocationId,PublicIp]' --output text<\/pre>\n<h2>Release an Elastic IP address<\/h2>\n<p>Releasing an Elastic IP is a bit more complicated because we need an allocation ID, a unique identifier to an Elastic IP (EIP) address when allocated, used to manage and reference the EIP within your AWS environment.<\/p>\n<p>The following script lets you pass the IP you wish to release.<\/p>\n<pre><\/pre>\n<p>#!\/bin\/bash<\/p>\n<p># Check if an IP address is passed as an argument<\/p>\n<p>if [ $# -eq 0 ]; then<\/p>\n<p>echo &#8220;Usage: $0 YOUR_PUBLIC_IP&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p># Assign the first argument to YOUR_PUBLIC_IP<\/p>\n<p>YOUR_PUBLIC_IP=$1<\/p>\n<p># Get the Allocation ID for the given public IP address<\/p>\n<p>allocation_id=$(aws ec2 describe-addresses &#8211;query &#8220;Addresses[?PublicIp==&#8217;$YOUR_PUBLIC_IP&#8217;].AllocationId&#8221; &#8211;output text)<\/p>\n<p># Check if the allocation ID was found<\/p>\n<p>if [ -n &#8220;$allocation_id&#8221; ]; then<\/p>\n<p># Release the EIP using the allocation ID<\/p>\n<p>aws ec2 release-address &#8211;allocation-id $allocation_id<\/p>\n<p>echo &#8220;Released EIP: $YOUR_PUBLIC_IP&#8221;<\/p>\n<p>else<\/p>\n<p>echo &#8220;Allocation ID not found for IP: $YOUR_PUBLIC_IP&#8221;<\/p>\n<p>fi<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/03\/Associate-and-disassociate-an-Elastic-IP-address.png\" target=\"_blank\" rel=\"nofollow noopener\">Associate and disassociate an Elastic IP address<\/a><\/p>\n<h2>Associate an Elastic IP address<\/h2>\n<p>Once you have allocated an Elastic IP, you can use the bash script below to assign the IP (in AWS lingo, associate the IP) to an EC2 instance.<\/p>\n<pre><\/pre>\n<p>#!\/bin\/bash<\/p>\n<p># Check for the correct number of arguments<\/p>\n<p>if [ $# -ne 2 ]; then<\/p>\n<p>echo &#8220;Usage: $0 INSTANCE_NAME EIP_ADDRESS&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p># Assign the arguments to variables<\/p>\n<p>INSTANCE_NAME=$1<\/p>\n<p>EIP_ADDRESS=$2<\/p>\n<p># Retrieve the instance ID based on the name<\/p>\n<p>INSTANCE_ID=$(aws ec2 describe-instances<\/p>\n<p>&#8211;query &#8220;Reservations[*].Instances[*].{ID:InstanceId}&#8221;<\/p>\n<p>&#8211;filters &#8220;Name=tag:Name,Values=$INSTANCE_NAME&#8221;<\/p>\n<p>&#8211;output text)<\/p>\n<p># Check if the instance ID was found<\/p>\n<p>if [ -z &#8220;$INSTANCE_ID&#8221; ]; then<\/p>\n<p>echo &#8220;No instance found with the name: $INSTANCE_NAME&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p>echo &#8220;Instance ID: $INSTANCE_ID&#8221;<\/p>\n<p># Retrieve the allocation ID of the Elastic IP<\/p>\n<p>EIP_ALLOC_ID=$(aws ec2 describe-addresses<\/p>\n<p>&#8211;query &#8220;Addresses[?PublicIp==&#8217;$EIP_ADDRESS&#8217;].AllocationId&#8221;<\/p>\n<p>&#8211;output text)<\/p>\n<p># Check if the allocation ID was found<\/p>\n<p>if [ -z &#8220;$EIP_ALLOC_ID&#8221; ]; then<\/p>\n<p>echo &#8220;No Elastic IP found with the address: $EIP_ADDRESS&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p>echo &#8220;Elastic IP Allocation ID: $EIP_ALLOC_ID&#8221;<\/p>\n<p># Associate the Elastic IP with the instance<\/p>\n<p>ASSOCIATION_ID=$(aws ec2 associate-address<\/p>\n<p>&#8211;instance-id $INSTANCE_ID<\/p>\n<p>&#8211;allocation-id $EIP_ALLOC_ID<\/p>\n<p>&#8211;query &#8216;AssociationId&#8217;<\/p>\n<p>&#8211;output text)<\/p>\n<p># Check if the EIP was successfully associated<\/p>\n<p>if [ -z &#8220;$ASSOCIATION_ID&#8221; ]; then<\/p>\n<p>echo &#8220;Failed to associate Elastic IP with the instance.&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p>echo &#8220;Elastic IP associated with the instance successfully. Association ID: $ASSOCIATION_ID&#8221;<\/p>\n<h2>Disassociate an Elastic IP address<\/h2>\n<p>The following script removes the IP address from the EC2 instance. Since an Elastic IP can only be assigned to one instance, there is no need to pass the instance name.<\/p>\n<pre><\/pre>\n<p>#!\/bin\/bash<\/p>\n<p><!-- Check if an IP address is passed as an argument --><\/p>\n<p>if [ $# -eq 0 ]; then<\/p>\n<p>echo &#8220;Usage: $0 EIP_ADDRESS&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p><!-- The first argument is the EIP address --><\/p>\n<p>EIP_ADDRESS=$1<\/p>\n<p><!-- Retrieve the association ID of the Elastic IP --><\/p>\n<p>ASSOCIATION_ID=$(aws ec2 describe-addresses<\/p>\n<p>&#8211;query &#8220;Addresses[?PublicIp==&#8217;$EIP_ADDRESS&#8217;].AssociationId&#8221;<\/p>\n<p>&#8211;output text)<\/p>\n<p><!-- Check if the association ID was found --><\/p>\n<p>if [ -z &#8220;$ASSOCIATION_ID&#8221; ]; then<\/p>\n<p>echo &#8220;No association found for IP: $EIP_ADDRESS&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p><!-- Disassociate the Elastic IP --><\/p>\n<p>aws ec2 disassociate-address &#8211;association-id $ASSOCIATION_ID<\/p>\n<p>if [ $? -eq 0 ]; then<\/p>\n<p>echo &#8220;EIP disassociated successfully.&#8221;<\/p>\n<p>else<\/p>\n<p>echo &#8220;Failed to disassociate EIP.&#8221;<\/p>\n<p>fi<\/p>\n<p>Please note that the IP remains allocated even after disassociating it from the instance. This means you will continue to incur charges for the idle IP until it is released. You can use the provided script above to release the IP address if you no longer need it.<\/p>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/03\/Associate-and-disassociate-an-Elastic-IP-address.png\" target=\"_blank\" rel=\"nofollow noopener\">Associate and disassociate an Elastic IP address<\/a><\/div>\n<h2>Subscribe to 4sysops newsletter!<\/h2>\n<p>If you are frustrated due to the need of paying for actively used public IPv4 addresses, you might find interest in one of my subsequent articles wherein I elucidate how you can allocate an IPv6 address to an EC2 instance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous articles, I explained how to create an AMI from an EC2 instance and then launch the instance with scripts. Next, you probably want to assign an Elastic IP (IPv4 address) to the instance. In this post, you will learn how to allocate, release, associate, and disassociate an Elastic IP with the AWS CLI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9294,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,150,126,133],"tags":[],"class_list":["post-9293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-aws","category-cloud-computing","category-networking"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=9293"}],"version-history":[{"count":1,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9293\/revisions"}],"predecessor-version":[{"id":10483,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9293\/revisions\/10483"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/9294"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=9293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=9293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=9293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}