简体   繁体   English

aws ec2 运行实例:脚本作为纯文本被忽略

[英]aws ec2 run-instances: script as the plain text is ignored

I'm trying to pass the script as the --user-data parameter.我试图将脚本作为 --user-data 参数传递。 If the same is run through --user-data file://some_file.sh all works.如果同样通过--user-data file://some_file.sh运行, --user-data file://some_file.sh一切正常。 Also, it works if launch instance through AWS GUI by adding user-data in the correspondent launch configuration box.此外,如果通过在相应的启动配置框中添加用户数据通过 AWS GUI 启动实例,它也可以工作。

My CLI command is我的 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"

if the same run as a script, it's content formatted as below如果与脚本一样运行,则其内容格式如下

#!/bin/bash
sudo touch /tmp/install.log
sudo chmod 777 /tmp/install.log
echo $(date) >> /tmp/install.log

Also, I'd like to mention that I tried to pass string in different formats like :另外,我想提一下,我尝试以不同的格式传递字符串,例如:

--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-data after launch is seeing but not executed启动后的用户数据可见但未执行

$ curl -L http://169.254.169.254/latest/user-data/

The first line must start with #!第一行必须以#! . .

Then, subsequent lines are executed.然后,执行后续行。 They must be separated by a proper newline.它们必须由适当的换行符分隔。 It looks like \\n is not interpreted correctly.看起来\\n没有正确解释。

From how to pass in the user-data when launching AWS instances using CLI :如何在使用 CLI 启动 AWS 实例时传入用户数据

$ aws ec2 run-instances --image-id ami-16d4986e --user-data '#!/bin/bash
> poweroff'

As an experiment, I put this at the end of the run-instances command:作为一个实验,我把它放在run-instances命令的末尾:

aws ec2 run-instances ... --user-data '#!
echo bar >/tmp/foo
'

When I logged into the instance, I could see the /tmp/foo file.当我登录实例时,我可以看到/tmp/foo文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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