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 Antworten
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
EXPERTE
beantwortet vor 2 Jahren
0
Akzeptierte Antwort

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
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen