How to set Environment Variables on EC2 instance via User Data

前端 未结 9 722
北荒
北荒 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:16

    I find this to be a pretty easy way to set environment variables for all users using User Data. It allows me to configure applications so the same AMI can work with multiple scenarios:

    #!/bin/bash
    echo export DB_CONNECTION="some DB connection" >> /etc/profile
    echo export DB_USERNAME="my user" >> /etc/profile
    echo export DB_PASSWORD="my password" >> /etc/profile
    

    Now, all users will have DB_CONNECTION, DB_USERNAME and DB_PASSWORD set as environment variables.

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

    Adding this to the init script of the node will add environment variables on launch. They won't show up in the node configuration page but they will be able to use in any job.

    #!/bin/bash
    echo 'JAVA_HOME="/usr/lib/jvm/java-8-oracle/"' | sudo tee -a /etc/profile#
    

    This answer is similar to what hamx0r proposed however, jenkins doesn't have permission to echo to /etc/profiles with or without sudo.

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

    After doing the stuffs in the user data script, the process exits. So, whatever environment variable you export will not be there in the next process. One way is to to put exports in the .bashrc file so that it gets available in the next session also.

        echo "export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-23235232.us-east-1.elb.amazonaws.com" >> ~/.bashrc
    
    0 讨论(0)
  • 2020-12-14 08:24

    From this Medium.com article, you can put a script in UserData that writes to a file in /etc/profile.d that will get run automatically when a new shell is run.

    Here is an example cloudformation.yaml

    Parameters:
      SomeOtherResourceData:
        default: Fn::ImportValue: !Sub "SomeExportName"
    Resources:
      WebApi:
        Type: AWS::EC2::Instance
        Properties:
          # ...
          UserData:
            Fn::Base64: !Sub
              - |
                #!/bin/bash
    
                cat > /etc/profile.d/load_env.sh << 'EOF'
    
                export ACCOUNT_ID=${AWS::AccountId}
                export REGION=${AWS::Region}
                export SOME_OTHER_RESOURCE_DATA=${SomeOtherResourceData}
    
                EOF
                chmod a+x /etc/profile.d/load_env.sh
    

    And a YAML that exports something

    # ...
    Outputs:
      SomeExportName:
        Value: !Sub "${WebDb.Endpoint.Address}"
        Export:
          Name: SomeExportName
    
    0 讨论(0)
  • 2020-12-14 08:26

    One of the more configurable approach to define environment variables for EC2 instances, is to use Systems Manager Parameter Store. This approach will make it easier to manage different parameters for large number of EC2 instances, both encrypted using AWS KMS as well as in plain text. It will also allows to change the parameter values with minimal changes in EC2 instance level. The steps are as follows.

    • Define string parameters (Encrypted with KMS or Unencrypted) in EC2 Systems Manager Parameter Store.
    • In the IAM role EC2 assumes, give required permission to access the parameter store.
    • Using the AWS CLI commands for EC2 System Manager, read the parameters and export to environment variables in User Data section using Get-Parameter or Get-Parameters AWS CLI commands and controlling command output as required.

    e.g Using Get-Parameter command to retrieve db_connection_string parameter(Unencrypted).

    export DB_CONNECTION=$(aws --region=us-east-2 ssm get-parameter --name 'db_connection' --query 'Value')
    

    Note: For more details in setting up AWS KMS Keys, defining encrypted strings, managing IAM policies & etc., refer the following articles.

    • Securing Application Secrets with EC2 Parameter Store
    • Simple Secrets Management via AWS’ EC2 Parameter Store
    0 讨论(0)
  • 2020-12-14 08:30

    This maynot be the exact answer to the OP's question but similar. I've thought of sharing this as I've wasted enough time searching for the answer and finally figured it out.

    Example assuming - AWS EC2 running ubuntu.

    If there is a scenario where you need to define the environment variables as well use it in the same bash session (same user-data process), then either you can add the variables to /etc/profile, /etc/environment or /home/ubuntu/.zshrc file. I have not tried /home/ubuntu/.profile file BTW.

    Assuming adding to .zshrc file,

    sudo su ubuntu -c "$(cat << EOF 
        echo 'export PATH="/tmp:\$PATH"' >> /home/ubuntu/.zshrc
        echo 'export ABC="XYZ"' >> /home/ubuntu/.zshrc
        echo 'export PY_VERSION=3.8.1' >> /home/ubuntu/.zshrc
        source /home/ubuntu/.zshrc
        echo printenv > /tmp/envvars  # To test
    EOF
    )"
    

    Once the user data is finished running, you can see the environment variables which you have added in the script are echoed to the envvars file. Reloading the bash with source /home/ubuntu/.zshrc made the newly added variables available in the bash session.

    (additional info) How to install zsh and oh-my-zsh?

    sudo apt-get install -y zsh
    sudo su ubuntu -c "$(cat << EOF 
        ssh-keyscan -H github.com >> /home/ubuntu/.ssh/known_hosts
        git clone https://github.com/robbyrussell/oh-my-zsh.git /home/ubuntu/.oh-my-zsh
        cp /home/ubuntu/.oh-my-zsh/templates/zshrc.zsh-template /home/ubuntu/.zshrc
        echo DISABLE_AUTO_UPDATE="true" >> /home/ubuntu/.zshrc
        cp /home/ubuntu/.oh-my-zsh/themes/robbyrussell.zsh-theme /home/ubuntu/.oh-my-zsh/custom
    EOF
    )"
    sudo chsh -s /bin/zsh ubuntu
    

    Wondering why I didn't added the environment variable in .bashrc? The scenario which I mentioned above (using the environment variables in the same user-data session) adding to .bashrc won't work. .bashrc is only sourced for interactive Bash shells so there should be no need for .bashrc to check if it is running in an interactive shell. So just like above,

    source /home/ubuntu/.bashrc
    

    won't reload the bash. You can check this out written right in the beginning of the .bashrc file,

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    
    0 讨论(0)
提交回复
热议问题