4

When I ssh into my EC2 Instance and run the following commands my SpringServer.jar file executes and I can access my Spring application by going to myawsaccount:8080/times. when I specify the following commands in User Data I cant access my application at myawsaccount:8080/times and im not sure why. Any help would be appreciated.

Commands

#!/bin/bash --> only in user script
    sudo su

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.rpm

yum install -y jdk-8u141-linux-x64.rpm

wget https://myawsaccount.s3-eu-west-1.amazonaws.com/SpringServer-1-0.0.1-SNAPSHOT.jar

java -jar SpringServer-1-0.0.1-SNAPSHOT.jar

2 Answers 2

12

To troubleshoot UserData issues, the best thing to do is to login to an instance, and inspect one of UserData log files.

Most impotently /var/log/cloud-init-output.log:

The cloud-init output log file (/var/log/cloud-init-output.log) captures console output so it is easy to debug your scripts following a launch if the instance does not behave the way you intended.

Also your UserData script will be located in /var/lib/cloud/instances/<instance-id>/. Thus, once you are in the instance you can manually try to run it and fix/debug while in the instance.

Sign up to request clarification or add additional context in comments.

11 Comments

why i try to access that file im getting permission denied. Any ideas?
@KahunaDub You must be root. Run sudo su to change to root user.
Im logged in as the root user, im still getting that error
yeah... sudo cat worked while logged as root thanks
@KahunaDub Unfortunately, UserData only execute once - when you launch a new instance. It does not run when you stop/start or repoot an existing instance. For that you would have to look, for example, into putting your script under control of systemd in your instance.
|
2

Setting environment variables using export doesn't work in user data as it only sets them for the current shell session. You can fix this by copying them to your profile configuration:

#!/bin/bash

...

echo 'export JAVA_HOME=/opt/jdk1.8.0_141' >> /etc/profile
echo 'export JRE_HOME=/opt/jdk1.8.0_141/jre' >> /etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin' >> /etc/profile

...

This way, the environment variables will be available in every session.

5 Comments

Whoever downvoted my answer, thanks for letting me know that there's something wrong with it. I probably either didn't understand the question or my answer is wrong. Would you mind letting me know which of the two it is? I'd love to learn and fix whatever issue there is with my answer.
You have my upvote. Most annoying thing is getting down-voted without explanation. And your answer could be the solution, or part of it.
It's always easier to criticize than to create. As long as OP (and whoever else reads this questions in the future) gets their problem solved, I'm fine. I don't really need the points.
Yes, I agree. But downvoting without an explanation is not very useful to anyone. The person who down voted you could misunderstand the question or your answer. Without comment, there is no chance to clarify or correct it.
thanks, this answer helped as well but the accepted answer solved my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.