I am trying to set environment variables with EC2s user data, but nothing i do seems to work
here are the User data scripts i tried
#!/b
You can add another shell script in /etc/profile.d/yourscript.sh
which will contain the set of environment variables you want to add.
This script will run at every bootup and your variable will be available to all users.
#!/bin/sh
echo 'export AWS_DEFAULT_REGION=ap-southeast-2' > ~/myconfiguration.sh
chmod +x ~/myconfiguration.sh
sudo cp ~/myconfiguration.sh /etc/profile.d/myconfiguration.sh
The above code creates a shell script to set environment variable for aws default region and copies it to profile.d
.
You can use this script:
#!/bin/bash
echo HOST_URL=\"checkEmai-LoadBala-ICHJ82KG5C7P-23235232.us-east-1.elb.amazonaws.com\" >> /etc/environment
I created an EC2 instance with Amazon Linux AMI 2018.03.0 and added this user data to it and it works fine.
Refer to this answer for more details.
The user data script on EC2 executes at after boot in its own process. The environment variables get set in that process and disappear when the process exits. You will not see the environment variables in other processes, i.e., login shell or other programs for that matter.
You will have to devise a way to get these environment variables into whatever program needs to see them.
Where do you need these variables to be available? In /startup.sh staging 2649
?
EDIT
Try this:
#!/bin/bash
set -e -x
export HOST_URL="checkEmai-LoadBala-ICHJ82KG5C7P-2141709021.us-east-1.elb.amazonaws.com"
/startup.sh staging 2649
Then edit /startup.sh
, and put the following line on the top:
echo $HOST_URL > /tmp/var
Boot the instance, and then paste /tmp/var
here.