在SSH到我的EC2实例时,我可以成功运行sudo yum update
。但是,当我将相同的命令附加到启动配置的userData时,会看到以下错误(在/var/log/cloud-init-output.log
中):
launch script..
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
我的脚本:
#!/bin/bash
echo "launch script.."
sudo yum update -y
sudo yum install java-1.8.0 -y
aws s3 cp s3://bucket/app.jar ./app.jar
java -jar app.jar >> out.log
如何在EC2实例启动时运行yum命令?
答案 0 :(得分:1)
确保您确实有从EC2实例到Internet的路由。通常,这意味着您的VPC中有公共IP或到NAT实例/网关的路由以及Internet网关。
可能是在建立连接之前,userdata脚本开始运行。在脚本运行yum update之前,您可能需要验证实例具有出站网络访问权限,例如:
#!/bin/bash
echo "launch script.."
until ping -c1 www.google.com &>/dev/null; do
echo "Waiting for network ..."
sleep 1
done
yum update -y
# other things here
还要注意,用户数据脚本是作为root用户执行的,因此您不需要sudo。