AWS EC2 UserData 脚本不会在 t3 实例上触发

AWS EC2 UserData Script Doesn't Fire on t3 instances

我有一个漂亮的 simple userdata script,它在 ubuntu 上安装 CodeDeploy 代理,然后向 CloudFormation 发出一个信号,表明该实例是健康的。我的堆栈是使用 CloudFormation 部署的,它设置了 ASG、LaunchTemplate、TargetGroup 等。目标实例类型是 Ubuntu 18.04 (ami-07ebfd5b3428b6f4d AMI) t3.small 个实例。我以前在不同的 AMI 上遇到过同样的问题,但升级到 ami-07ebfd5b3428b6f4d 似乎暂时解决了这个问题……它工作了几个星期。

最近,我的ASG试图更换一些不健康的实例,但新实例没有出现。在进一步调查中,我发现我又遇到了同样的问题——用户数据脚本没有触发。云-init.log 以这一行结束:

2020-03-20 01:23:56,741 - util.py[DEBUG]: Running command ['/var/lib/cloud/instance/scripts/part-001'] with allowed return codes [0] (shell=False, capture=False)

但系统日志中似乎没有 activity 挂在这里。日志中的这个文件正确包含脚本,如果我手动调用它,这个脚本会成功执行(不需要任何交互)。

将我的 CloudFormation 堆栈更改为使用 t2 实例可以解决问题。不过我已经预留了t3容量,所以我需要回到t3。

Thoughts/ideas有人吗?

根据评论更新。这是 cloud-init-output.log 的最后几行:

Setting up apport (2.20.9-0ubuntu7.12) ...
Installing new version of config file /etc/init.d/apport ...
apport-autoreport.service is a disabled or a static unit, not starting it.
Setting up ubuntu-standard (1.417.4) ...
Setting up grub-pc (2.02-2ubuntu8.15) ...
ESC[1;24rESC[4lESC)0ESC[mESC(BESC[1;24rESC[HESC[JESC[1;1HPackage configurationESC[3;2H┌──────────────────────────┤ Configuring grub-pc ├──────────────────────────┐ESC[4;2H│ESC[75C│ESC[5;2H│ The GRUB boot loader was previously installed to a disk that is noESC[8C│ESC[6;2H│ longer present, or whose unique identifier has ch

事实证明,非交互模式要求交互。不知道为什么会这样,或者为什么随后手动调用脚本没有表现出相同的行为。提示是 cloud-init-output.log 末尾的乱码日志消息(感谢@Marcin 对此的推动)。

修复方法是将脚本的顶部修改为如下所示:

export DEBIAN_FRONTEND=noninteractive
apt-get update 
apt-get -y upgrade 
apt-get install awscli python-pip -y
pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
apt-get autoremove 
apt-get autoclean