3

I am deploying my code to AWS EC2. The documentation says there's something called "user data" or "user data scripts" that you can enter this info when you're launching an ec2 instance and the script will be executed at instance startup.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts

the following is in my user data script:

#!/bin/bash
echo 1111 >> /home/ubuntu/1111.txt
export MONGODB_HOST=www.mongodb.com
export MONGODB_PORT=12345
export MONGODB_USER=user
export MONGODB_PASS=pass

enter image description here

So when I launch the instance with this user data script I would expect to see the environment variables being set, but it didn't.

So is there something that I did wrong?

7
  • Already answered in this thread stackoverflow.com/questions/9764145/…
    – Tommy
    Commented Oct 27, 2015 at 9:23
  • yes I have read that post but somehow it's not working. I can see the user data inside /var/lib/cloud/instance/user-data.txt also I can get $user_data variable with user_data=curl 169.254.169.254/latest/user-data` but I just cannot get them into environment variables.. (my node.js app reads credentials from env variables..) Commented Oct 27, 2015 at 10:04
  • are you sshing to the machine and starting node by hand? or how are you starting it? Commented Oct 27, 2015 at 11:47
  • AWS codedeploy will tell code-deploy agent to run some scripts (start.sh) when all is done the final script will do node server.js Commented Oct 28, 2015 at 1:25
  • Some help at forums.aws.amazon.com/message.jspa?messageID=139744. Also note that userdata scripts are run as root.
    – jarmod
    Commented Oct 28, 2015 at 1:38

1 Answer 1

6

Your user data script is actually run. Nevertheless, it is run on its own bash process which dies at the end of your script.

Exported variables are preserved only for the lifetime of your script and they are also visible from child processes of your script.

Since new connections to your ec2 instance are not children of the original script that ran the user data, they don't inherit exported variables.

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.