AWS EC2 用户数据在修改后不起作用

AWS EC2 User Data doesn't work after modifying it

注意:没有关于修改 EC2 实例用户数据的问题。

我的案例:我在 EC2 首次启动时添加了以下用户数据,并且效果很好。

#! /bin/bash
cd ~
echo "Test" > index.html
python -m SimpleHTTPServer 80

启动实例后,为了修改用户数据,我停止了实例,更改了用户数据,然后重新启动了实例。但是这次脚本不起作用。

#! /bin/bash
cd ~
echo "Test2" > index.html
python -m SimpleHTTPServer 80

我不明白为什么修改后的用户数据不起作用。

引用User data and shell scripts:

By default, user data scripts and cloud-init directives run only during the boot cycle when you first launch an instance. You can update your configuration to ensure that your user data scripts and cloud-init directives run every time you restart your instance. For more information, see How can I execute user data with every restart of my EC2 instance? in the AWS Knowledge Center.

默认情况下,用户数据仅在首次启动时 运行(使用实例存储卷的实例除外)

如果您想删除一次,请使用以下信息:

根据以下回答:https://serverfault.com/questions/797482/how-to-make-ec2-user-data-script-run-again-on-startup

rm /var/lib/cloud/instances/*/sem/config_scripts_user

rm /var/lib/cloud/instance/sem/config_scripts_user

对于 Windows 个实例,只需在用户数据中添加 <persist>true</persist>

https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html