Elastic Beanstalk environment variables for Docker host

后端 未结 5 671
春和景丽
春和景丽 2021-02-08 04:51

I have an EB env with Docker web app (rails) properly deployed. I set several EB env variables and they are properly visible in the container. Now - I\'d like these EB env varia

相关标签:
5条回答
  • 2021-02-08 05:28

    I haven't tested this on all versions of Elasticbeanstalk. But on at least 64bit Amazon Linux 2015.03 v2.0.1 running Multi-container Docker 1.6.2 (Generic) there's a nicer way to get the envars from your config. There's a ruby script on the instance already that provides a proper json representation of the envars { "SOME_ENV_VAT" : "VALUE" }

    # returns literal null from jq
    sudo /opt/elasticbeanstalk/bin/get-config environment | jq -r '.MY_ENVVAR'
    
    # returns empty string. Usefull for bash -z
    sudo /opt/elasticbeanstalk/bin/get-config environment | jq -r '.MY_ENVVAR // empty'
    
    0 讨论(0)
  • 2021-02-08 05:30

    This was a though one, so I'm posting my solution for those who encounter this.
    Elastic Beanstalk Docker instance does not expose the environment variables to the docker host. It does that only to the docker container.
    If you'd like to get the env variables on the host, they are located at /opt/elasticbeanstalk/deploy/configuration/containerconfiguration.
    This is one large JSON file, conveniently disobeying the JSON structure for the env vars.
    I wrote a small ruby script to parse it and extract the env vars from it:

    require 'json'
    container_config = JSON.parse(File.read('/opt/elasticbeanstalk/deploy/configuration/containerconfiguration'))
    raw_vars =  container_config['optionsettings']['aws:elasticbeanstalk:application:environment']
    envs = ''
    raw_vars.each do |raw_var|
      pair = raw_var.split('=')
      envs << "export #{pair[0]}=#{pair[1]}\n" if pair[1]
    end
    puts envs
    

    this script yields a set of export commands to console that sets the env vars. I adapted it a bit to write ENV commands into my Dockerfile.

    0 讨论(0)
  • 2021-02-08 05:43

    Adding to @Patrick H McJury's answer.

    Here is how it worked for me in a multi container beanstalk environment -

    .ebextensions/newrelic.config -

    container_commands:
      setup-nr-infra:
        command: |
          NRIA_LICENSE_KEY=$(sudo /opt/elasticbeanstalk/bin/get-config environment | jq -r '.NEW_RELIC_LICENSE_KEY')
          NRIA_DISPLAY_NAME=$(sudo /opt/elasticbeanstalk/bin/get-config environment | jq -r '.APPNAME')
          touch /etc/newrelic-infra.yml && \
          echo "license_key: ${NRIA_LICENSE_KEY}" > /etc/newrelic-infra.yml && \
          echo "display_name: ${NRIA_DISPLAY_NAME}" >> /etc/newrelic-infra.yml && \
          chmod 644 /etc/newrelic-infra.yml
          sudo initctl start newrelic-infra || true
    
    commands:
    # Create the agent’s yum repository
      "01-agent-repository":
        command: sudo curl -o /etc/yum.repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/yum/el/6/x86_64/newrelic-infra.repo
    #
    # Update your yum cache
      "02-update-yum-cache":
        command: yum -q makecache -y --disablerepo='*' --enablerepo='newrelic-infra'
    #
    # Run the installation script
      "03-run-installation-script":
        command: sudo yum install newrelic-infra -y
    

    NEW_RELIC_LICENSE_KEY & APPNAME variable must be populated before in beanstalk environment.

    0 讨论(0)
  • 2021-02-08 05:44

    King of the one liners.

    eval $(sudo ruby -e 'require "json"; container_config = JSON.parse(File.read("/opt/elasticbeanstalk/deploy/configuration/containerconfiguration")); raw_vars = container_config["optionsettings"]["aws:elasticbeanstalk:application:environment"]; envs = ""; raw_vars.each do |raw_var| envs << "export #{raw_var};\n" end; print envs;')
    

    This will automatically export all the variables. You cannot use puts to output environment variables with Ruby.

    0 讨论(0)
  • 2021-02-08 05:54

    I ran into the same issue, but needed the environment variables to be available during the execution of a post-deployment Bash script.

    Since the jq parser is available on the (current) Amazon Linux AMIs, I was able to acheive something similar using it to parse the JSON and then export the environment variables on the host (this is an excerpt from an ebextensions config file):

    files:
      "/opt/elasticbeanstalk/hooks/appdeploy/post/export_env_vars_on_host.sh":
      mode: "000755"
      owner: root
      group: root
      content: |
        #!/usr/bin/env bash
        echo Defaults:root \!requiretty >> /etc/sudoers
        for envvar in `jq '.optionsettings | {"aws:elasticbeanstalk:application:environment"}[] | .[]' /opt/elasticbeanstalk/deploy/configuration/containerconfiguration`
        do
          temp="${envvar#\"}";
          temp="${temp/=/=\"}";
          export temp;
        done
    
    0 讨论(0)
提交回复
热议问题