How to set Environment Variables on EC2 instance via User Data

前端 未结 9 723
北荒
北荒 2020-12-14 07:34

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         


        
相关标签:
9条回答
  • 2020-12-14 08:32

    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 .

     

    0 讨论(0)
  • 2020-12-14 08:32

    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.

    0 讨论(0)
  • 2020-12-14 08:35

    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.

    0 讨论(0)
提交回复
热议问题