我尝试将脚本作为--user-data参数传递。如果通过--user-data file://some_file.sh运行相同的命令,则一切正常。此外,如果通过AWS GUI在对应的启动配置框中添加用户数据来启动实例,则此方法也有效。
我的CLI命令是
aws ec2 run-instances --image-id ami-0cc0a36f626a4fdf5 --count 1 --instance-type t2.micro --key-name key_name --security-group-ids sg-00000000 --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=some_name}]" --output table --user-data "sudo touch /tmp/install.log && sudo chmod 777 /tmp/install.log && echo $(date) >> /tmp/install.log"
如果与脚本运行相同,则其内容格式如下
#!/bin/bash
sudo touch /tmp/install.log
sudo chmod 777 /tmp/install.log
echo $(date) >> /tmp/install.log另外,我还想提一下,我尝试过以不同的格式传递字符串,比如:
--user-data echo "some text"
--user-data "command_1\n command_2\n"
--user-data "command_1 && command_2"
--user-data "command_1; command_2;"
--user-data "#!/bin/bash; command_1; command_2;"
User-启动后的数据正在查看但未执行
$ curl -L http://169.254.169.254/latest/user-data/
发布于 2019-12-31 20:38:42
第一行必须以#!开头。
然后,执行后续行。它们必须用适当的换行符分隔。看起来\n没有被正确解释。
来自how to pass in the user-data when launching AWS instances using CLI
$ aws ec2 run-instances --image-id ami-16d4986e --user-data '#!/bin/bash
> poweroff'作为实验,我将此代码放在run-instances命令的末尾:
aws ec2 run-instances ... --user-data '#!
echo bar >/tmp/foo
'当我登录到该实例时,我可以看到/tmp/foo文件。
https://stackoverflow.com/questions/59540773
复制相似问题