By using AWS re:Post, you agree to the Terms of Use

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 Answers
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 picture
EXPERT
answered 15 days ago
0
Accepted Answer

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
answered 15 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions