title: How to set Environment Variables on EC2 instance via User Data
body: <p>I am trying to set environment variables with EC2s user data, but nothing i do seems to work</p>
  
  <p>here are the User data scripts i tried</p>
  
  <blockquote>
  <pre><code>#!/bin/bash
  echo "export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-23235232.us-east-1.elb.amazonaws.com" &gt;&gt; /env.sh 
  source /env.sh
  </code></pre>
  </blockquote>
  
  <p>And another:</p>
  
  <blockquote>
  <pre><code>#!/bin/bash
  echo "#!/bin/bash" &gt;&gt; /env.sh
  echo "export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-67323523.us-east-1.elb.amazonaws.com" &gt;&gt; /env.sh 
  chmod +x /env.sh
  /env.sh
  </code></pre>
  </blockquote>
  
  <p>They both do absolutly nothing, and if i log in and issue the command <code>source /env.sh</code> or <code>/env.sh</code> it works. so this must be something forbidden that i  am trying to do.</p>
  
  <p>Here is the output from <code>/var/log/cloud-init-output.log</code> using -e -x</p>
  
  <blockquote>
  <pre><code>+ echo 'export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709021.us-east-1.elb.amazonaws.com'
  + source /env.sh
  ++ export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709022.us-east-1.elb.amazonaws.com
  ++ HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709022.us-east-1.elb.amazonaws.com
  </code></pre>
  </blockquote>
  
  <p>Still, <code>echo $HOST_URL</code> is empty</p>
  
  <p>As requested, the full UserData script</p>
  
  <blockquote>
  <pre><code>#!/bin/bash
  set -e -x 
  echo "export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709021.us-east-1.elb.amazonaws.com" &gt;&gt; /env.sh 
  source /env.sh
  /startup.sh staging 2649
  </code></pre>
  </blockquote>