Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Beanstalk environment variables for Docker host

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 variables to be visible to the EC2 instance host, so that I can use them in the docker build process. However, they are not exposed to the docker host, only to the container.
How do I expose EB env variables to the Docker host?

like image 238
Tal Avatar asked Feb 03 '15 11:02

Tal


People also ask

How do you use Elastic Beanstalk with Docker?

Deploy to the Cloud Using the Elastic Beanstalk Console Choose “AWS Cloud9” and “Go To Your Dashboard.” Choose “Services” and “Elastic Beanstalk.” At the top right, choose “Create Application.” Paste flask-app for the “Application Name.” For “Platform,” choose “Docker.” Leave the Docker settings as is.

Does Elastic Beanstalk support Docker?

Elastic Beanstalk supports the deployment of web applications from Docker containers. With Docker containers, you can define your own runtime environment.

What two types of environments can be created when using Elastic Beanstalk?

In AWS Elastic Beanstalk, you can create a load-balanced, scalable environment or a single-instance environment. The type of environment that you require depends on the application that you deploy.

How does Docker work with Elastic Beanstalk?

All environment variables that are defined in the Elastic Beanstalk console are passed to the containers. By using Docker with Elastic Beanstalk, you have an infrastructure that handles all the details of capacity provisioning, load balancing, scaling, and application health monitoring.

What are environment variables in a docker environment?

Environment properties are passed in as key-value pairs to the application. In a Docker environment, Elastic Beanstalk passes environment properties to containers as environment variables. Your application code running in a container can refer to an environment variable by name and read its value.

Can I set dynamic environment variables in Elastic Beanstalk containers?

Because Elastic Beanstalk variables aren't accessible by the other containers in the Dockerfile that are getting built in earlier stages, you can follow the steps in the resolution to set dynamic environment variables during the build stage.

What is {EB_log_base_Dir} in Elastic Beanstalk?

${EB_LOG_BASE_DIR} is an environment variable set by Elastic Beanstalk with the value /var/log/eb-docker/containers . Elastic Beanstalk automatically creates the /var/log/eb-docker/containers/<service name> directory for each service in the docker-compose.yml file.


2 Answers

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
like image 104
Kristopher Avatar answered Sep 19 '22 03:09

Kristopher


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.

like image 35
Tal Avatar answered Sep 20 '22 03:09

Tal