简体   繁体   中英

Update IP address on start/ reboot of the EC2 instance

I have created an AMI image from an existing EC2 instance, where I have configured my .net application. in the applications web.config file where I have used my private/public IP. When I launch new ec2 instance from AMI, new private/public IP is assigned. how can I update the new private/public IP in my web.config files at the start or reboot of my ec2 instance.

You should create a startup script which will change the IP on each instance/AMI boot.

change-ip-on-startup.sh

#!/bin/bash
# Fetch instance IPs from metadata
INSTANCE_PUBLIC_IP=`curl http://169.254.169.254/latest/meta-data/public-ipv4`
INSTANCE_PRIVATE_IP=`curl http://169.254.169.254/latest/meta-data/local-ipv4`

# Use the variables to replace the IP(s)
# sed "s/.../${INSTANCE_PUBLIC_IP}/g" /path/to/web.config

Then use the following reasoning to make the script runned on each instance/AMI :

# Copy the script in the init.d directory and make it executable
cp /home/ec2-user/change-ip-on-startup.sh /etc/init.d/change-ip-on-startup
chmod +x /etc/init.d/change-ip-on-startup

# Load the script on start
ln -s /etc/init.d/change-ip-on-startup /etc/rc3.d/S99change-ip-on-startup

# Emulate a service behaviour
touch /var/lock/subsys/change-ip-on-startup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM