How to change an Ec2 instance’s IP address Automatically?

Ec2 instance’s IP

Ec2 instance’s IP


Hello folks,

I’m writing this blog to answer a common question: how can we automatically and regularly update the IP address of an Ec2 instance?

First, if you already have numerous Elastic IP addresses in your elastic IP list, that is OK;

but, if they do not exist, you will have to To create a new Elastic IP,

Navigate to the Ec2 instance -> Elastic IPs on the left panel. You can generate up to 5 EIPs.

If you require more, increase the EIP quota limit.

To increase EIPs quota limit you will need to request on below url.

https://ap-south-1.console.aws.amazon.com/servicequotas/home/services/ec2/quotas/L-0263D0A3

Once completed, you will be able to attach the EIP to an Ec2 instance.
Create a script to rotate IP addresses for certain Ec2 instances.

Enter the instance_id and EIP allocation id, then click on EIP to obtain the EIP_allocation_Id.

Once completed, you will be able to attach the EIP to an Ec2 instance.
Create a script to rotate IP addresses for certain Ec2 instances.

Enter the instance_id and EIP allocation id, then click on EIP to obtain the EIP_allocation_Id.

#!/bin/bash

# Replace with your EC2 instance ID
INSTANCE_ID=”i-00da5b3472e58″
#EIP_ALLOC_ID1=”” # replace with your EIP allocation id
EIP_ALLOC_ID1=”eipalloc-0cc19cb3b49″ # replace with your EIP allocation id
EIP_ALLOC_ID2=”eipalloc-0664da1545″ # replace with your EIP allocation id
EIP_ALLOC_ID3=”eipalloc-053226056eb95″ # replace with your EIP allocation id
EIP_ALLOC_ID4=”eipalloc-0aea6a941bf7″ # replace with your EIP allocation id
EIP_ALLOC_ID5=”eipalloc-0224639a4888″ # replace with your EIP allocation id

# Replace with the allocation IDs of the Elastic IPs
ELASTIC_IPS=($EIP_ALLOC_ID1 $EIP_ALLOC_ID2 $EIP_ALLOC_ID3 $EIP_ALLOC_ID4 $EIP_ALLOC_ID5)

while true; do
for ELASTIC_IP in “${ELASTIC_IPS[@]}”; do
# Disassociate the current Elastic IP from the EC2 instance
aws ec2 disassociate-address — association-id $(aws ec2 describe-addresses — allocation-ids $ELASTIC_IP — query ‘Addresses[0].AssociationId’ — output text)

# Associate the new Elastic IP with the EC2 instance
aws ec2 associate-address — instance-id $INSTANCE_ID — allocation-id $ELASTIC_IP

# Optional: Check if the association was successful
ASSOCIATION_STATUS=$(aws ec2 describe-addresses — allocation-ids $ELASTIC_IP — query ‘Addresses[0].AssociationId’ — output text)

if [ -n “$ASSOCIATION_STATUS” ]; then
echo “Elastic IP $ELASTIC_IP successfully associated with instance.”
else
echo “Failed to associate Elastic IP $ELASTIC_IP with instance.”
fi

# Sleep for 5 minutes
sleep 300
done
done

Once completed, you will be able to attach the EIP to an Ec2 instance.
Create a script to rotate IP addresses for certain Ec2 instances.

Enter the instance_id and EIP allocation id, then click on EIP to obtain the EIP_allocation_Id.

This script will change the instance IP every 5 minutes. It will change 5 EIPs per instance, but you may provide a list of allocation ids to change more.
I hope this blog helps you achieve your goals and alter the IP address of an EC2 instance.
To use the Ec2 instance, it must be added to the Public Subnet. Using the Private Subnet will direct traffic to the NAT gateway, exposing only one NAT IP address.
When you use a Public Subnet, no IP is exposed; instead, the IP is constantly changed to the Public IP.

Thank you everybody!!

I believe the information above is valuable to you; if you have any questions, please email or DM me.

Check for more : https://www.aliaceitsolutions.com/devops-specialist/

No comment

Leave a Reply

Your email address will not be published. Required fields are marked *