我尝试使用 AWS CLI run-instances 命令在创建时将用户数据传递到我的 EC2 实例。
我将以下命令放入文件中:
#!/bin/bash
sudo su
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<html><h1>Welcome to the server</h1><html>" >> /var/www/html/index.html
我运行的所有 AWS CLI 命令(带 .txt 和不带 .txt):
aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --区域 us-east-1 --user-data file://./text1.txt
aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --区域 us-east-1 --user-data file://./text1
aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --区域 us-east-1 --user-data text1.txt
aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --区域 us-east-1 --user-data text1
然后我尝试将脚本编码为 Base64,但用户数据仍然不被接受。
我创建了一个安全组,端口为 80 和 22,源为 0.0.0.0/0 使用默认 VPC 和子网。
AMI: Amazon Linux 2 - t2.micro
Region: us-east-1
问题:
- 未安装 HTTPD。
- index.html 未在路径中创建。
我做错了什么吗?为什么不起作用?
还有其他方法可以解决这个问题吗?
此问题的新更新:
我传递给 run-instances
命令的用户数据正在安装,但花费了太多时间。
并非总是会安装。我运行了代码五次,只有 3 个 EC2 安装了用户数据。
来自 AWS 状态网页:我发现 us-east-1
存在大量延迟问题。
最佳答案
通过用户数据提供的命令以 foot
用户身份执行。
因此,不要在用户数据脚本中使用 sudo
命令。
在 Amazon Linux 上执行用户数据脚本时出现的任何错误都可以在以下位置找到:
/var/log/cloud-init-output.log
如果您不确定脚本是否正在执行,请在标题行后添加如下行:
echo Script started > /tmp/script.log
然后您可以检查/tmp/script.log
的内容来确定脚本是否被执行。
关于amazon-web-services - AWS CLI 用户数据脚本未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60201060/