amazon-web-services - AWS CLI 用户数据脚本未运行
问题描述
我正在尝试使用 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):
aws ec2 run-instances --image-id ami-062f7200baf2fa504 --count 1 --instance-type t2.micro --key-name lab115key --security-groups my-sg --region 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 --region 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 --region 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 --region us-east-1 --user-data text1
然后我尝试将脚本编码为 Base64 仍然不接受用户数据。
我使用默认 VPC 和子网创建了一个安全组,其端口为 80 和 22,源为 0.0.0.0/0。
AMI:亚马逊 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
以确定脚本是否已执行。
推荐阅读
- javascript - 异步函数的 JavaScript 问题
- php - 在 Yii2 Html::a 标签中使用 PHP 三元
- php - 如何在 Laravel 6 的 Cookie 中存储具有多个值的嵌套关联数组而不将其转换为字符串数据
- reactjs - 如何将自定义文本添加到 react-datepicker?
- sql-server - SQL 服务器 - Row_Number 为 1 行返回不正确的值
- python - tkinter 中是否有循环浏览窗口的功能
- html - 如何应用不同的样式
- 在同一
- 父元素?
- 在同一
- reactjs - React Recoil useRecoilValue 正在返回一个空原子
- excel - 枢轴返回源数据中显示的实际评级值
- sql - 从命令行使用 sqlplus 更新行挂起(linux bash)