首页 > 解决方案 > 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):

  1. 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

  2. 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

  3. 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

  4. 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

问题:

  1. 未安装 HTTPD。
  2. index.html 没有在路径中创建。

难道我做错了什么?为什么它不起作用?

还有其他方法可以解决这个问题吗?


关于这个问题的新更新:

我传递给run-instances命令的用户数据正在安装,但它花费了太多时间。

并非一直都在安装。我运行了五次代码,只有 3 个 EC2 安装了用户数据。

来自 AWS 状态网页:我发现us-east-1有很多延迟问题。

标签: amazon-web-servicesamazon-ec2command-line-interfaceaws-cli

解决方案


通过用户数据提供的命令以foot用户身份执行。

因此,请勿sudo在用户数据脚本中使用命令。

在 Amazon Linux 上执行用户数据脚本的任何错误都可以在以下位置找到:

/var/log/cloud-init-output.log

如果您不确定脚本是否正在执行,请在标题行之后添加如下行:

echo Script started > /tmp/script.log

然后,您可以检查 的内容/tmp/script.log以确定脚本是否已执行。


推荐阅读