0 votes
1 view
in AWS by (12.2k points)

I searched a lot of topic about "user-data script is not working" in these few days, but until now, I haven't gotten any idea about my case yet, please help me to figure out what happened, thanks a lot!

According to AWS User-data explanation:

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts.

So I tried to pass my own user-data when instance launch, this is my user-data:

\#!/bin/bash

echo 'test' > /home/ec2-user/user-script-output.txt

But there is no file in this path: /home/ec2-user/user-script-output.txt

I checked /var/lib/cloud/instance/user-data.txt, the file is exist and same as my user-data script.

Also I checked the log in /var/log/cloud-init.log, there is no error message.

But the user-data script is working if I launch an new instance with Amazon linux(2014.09.01), but I'm not sure what difference between my AMI (based on Amazon linux) and Amazon linux.

The only different part I saw is if I run this script:

sudo yum list installed | grep cloud-init

My AMI:

cloud-init.noarch 0.7.2-8.33.amzn1 @amzn-main

Amazon linux:

cloud-init.noarch 0.7.2-8.33.amzn1 installed

I'm not sure this is the reason?

If you need more information, I'm glad to provide, please let me know what happened in my own AMI and how to fix it?

many thanks

1 Answer

0 votes
by (18.6k points)

From my understanding, you are trying to automatically create a file with every restart of your Amazon EC2 instance, launched from your custom AMI.

I had done the same thing once and I didn't remember running into any issues with my user data. Just do be sure, I did it again before answering your question and it still works.

Here's what I did:

1. Launched an Amazon Linux Instance (Accepting all the default wizard suggestions)

2. Created an AMI from this instance

3. Launched an instance using this AMI and gave the following user data in the user data field:

#!/bin/bash

/bin/echo "Hello World" >> /tmp/testfile.txt --//

image

4. Edited the inbound rules in security groups and added a rule for HTTP from anywhere.

5. SSH into this instance using ec2-user username

6. Ran the following commands to verify is my file was created or not:

cd /tmp/

ls

image

You can also check out the following link for a detailed explanation.

https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/ 

Hope this helps in some way.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...