Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS EC2 set environment variables

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?

like image 260
Shih-Min Lee Avatar asked Nov 09 '25 00:11

Shih-Min Lee


1 Answers

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.

like image 116
Daniel777 Avatar answered Nov 11 '25 00:11

Daniel777



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!