Amazon web services 启动/重新启动EC2实例时更新IP地址

Amazon web services 启动/重新启动EC2实例时更新IP地址,amazon-web-services,amazon-ec2,amazon-cloudformation,Amazon Web Services,Amazon Ec2,Amazon Cloudformation,我已经从一个现有的EC2实例创建了一个AMI映像,在那里我配置了我的.net应用程序。在applications web.config文件中,我使用了我的私有/公共IP。当我从AMI启动新的ec2实例时,分配了新的私有/公共IP。如何在ec2实例启动或重新启动时更新web.config文件中的新私有/公共IP。您应该创建一个启动脚本,该脚本将更改每个实例/AMI启动时的IP 启动时更改ip。sh #!/bin/bash # Fetch instance IPs from metadata INS

我已经从一个现有的EC2实例创建了一个AMI映像,在那里我配置了我的.net应用程序。在applications web.config文件中,我使用了我的私有/公共IP。当我从AMI启动新的ec2实例时,分配了新的私有/公共IP。如何在ec2实例启动或重新启动时更新web.config文件中的新私有/公共IP。

您应该创建一个启动脚本,该脚本将更改每个实例/AMI启动时的IP

启动时更改ip。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
然后使用以下推理使脚本在每个实例/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