user data on EC2 not working with CDK

0

Hi team,

I created an ec2 instance with CDK and added used data on it :

   const userData = readFileSync("lib/my-script.sh", "utf8");
    instance.addUserData(userData);
    instance.applyRemovalPolicy(RemovalPolicy.DESTROY);

my-script.sh :

#! /bin/bash
sudo su
yum -y update
# adding MYSQL client to connect to RDS(MYSQL) instance.
sudo yum install mysql

but when i connect to my ec2 instance via the session manager tab and do MySQL command I have this error :

sh: mysql: command not found

looks like the user data script was not run.

I'm I missing something? how can I know if the user data script was successfully run on the ec2 instance?

found this on : /var/log/cloud-init-output.log

Sep 15 00:17:50 cloud-init[xxxx]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
Sep 15 00:17:50 cloud-init[xxxx]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Sep 15 00:17:50 cloud-init[xxxx]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
ci-info: no authorized ssh keys fingerprints found for user ec2-user.

thank you!

2개 답변
1

I think you need to do sudo yum install mysql -y so that the yum utility doesn't try and prompt the user.

profile pictureAWS
전문가
답변함 2년 전
0
수락된 답변

removed "sudo" from the script as User Data scripts are run as root user as per this answer:

https://stackoverflow.com/questions/67740072/user-data-not-running-command-in-mysql

https://stackoverflow.com/questions/41603517/installing-mysql-client-on-ec2-via-cloudformation

also noticed that ec2 add #!/bin/bash on top of the script even if it already exists

my final script :

yum -y update
# adding MYSQL client to connect to RDS(MYSQL) instance.
yum -y install mysql
Jess
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠