Accessing Elastic Beanstalk environment properties in Docker
Asked Answered
I

4

15

So I've been looking around for an example of how I can specify environment variables for my Docker container from the AWS EB web interface. Typically in EB you can add environment properties which are available at runtime. I was using these for my previous deployment before I switched to Docker, but it appears as though Docker has some different rules with regards to how the environment properties are handled, is that correct? According to this article [1], ONLY the AWS credentials and PARAM1-PARAM5 will be present in the environment variables, but no custom properties will be present. That's what it sounds like to me, especially considering the containers that do support custom environment properties say it explicitly, like Python shown here [2]. Does anyone have any experience with this software combination? All I need to specify is a single environment variable that tells me whether the application is in "staging" or "production" mode, then all my environment specific configurations are set up by the application itself.

[1] http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options.html#command-options-docker

[2] http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options.html#command-options-python

Impersonate answered 5/8, 2014 at 23:9 Comment(0)
S
14

Custom environment variables are supported with the AWS Elastic Beanstalk Docker container. Looks like a miss in the documentation. You can define custom environment variables for your environment and expect that they will be passed along to the docker container.

Stereotomy answered 6/8, 2014 at 5:13 Comment(4)
Are these available whenever the container is started? For example, if I restart my environment (not redeploy)?Impersonate
Yes they are availableStereotomy
Is there way to access these while creating the docker image from a given docker file?Sapers
This depends on which AMI version you are using. For Amazon Linux 2 at least this does work anymore. What @Dre mentioned https://mcmap.net/q/776574/-accessing-elastic-beanstalk-environment-properties-in-docker however seems more relavantParamagnetic
H
4

I've needed to pass environment variable in moment docker run using Elastic Beanstalk, but, is not allowed put this information in Dockerrun.aws.json.

Below the steps to resolve this scenario:

  1. Create a folder .ebextensions
  2. Create a .config file in the folder
  3. Fill the .config file:

option_settings:
-option_name: VARIABLE_NAME value: VARIABLE_VALUE

  1. Zip the folder .ebextensions file along with the Dockerrun.aws.json plus Dockerfile and upload it to Beanstalk

To see the result, inside EC2 instance, execute the command "docker inspect CONTAINER_ID" and will see the environment variable.

Helminthiasis answered 22/5, 2018 at 17:17 Comment(0)
S
2

At least for me the environment variables that I set in the EB console were not being populated into the Docker container. I found the following link helpful though: https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-shell/

I used a slightly different approach where instead of exporting the vars to the shell, I used the ebextension to create a .env file which I then loaded from Python within my container.

The steps would be as follows:

  1. Create a directory called '.ebextensions' in your app root dir

  2. Create a file in this directory called 'load-env-vars.config'

  3. Enter the following contents:

     commands:
       setvars:
         command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "\(.key)=\"\(.value)\""' > /var/app/current/.env
     packages:
       yum:
         jq: []
    
  4. This will create a .env file in /var/app/current which is where your code should be within the EB instance

  5. Use a package like python-dotenv to load the .env file or something similar if you aren't using Python. Note that this solution should be generic to any language/framework that you're using within your container.

Sliest answered 9/8, 2022 at 18:35 Comment(0)
S
0

I don't think the docs are a miss as Rohit Banga's answer suggests. Thought I agree that "you can define custom environment variables for your environment and expect that they will be passed along to the docker container".

The Docker container portion of the docs say, "No DOCKER-SPECIFIC configuration options are provided by Elastic Beanstalk" ... which doesn't necessarily mean that no environment variables are passed to the Docker container.

For example, for the Ruby container the Ruby-specific variables that are always passed are ... RAILS_SKIP_MIGRATIONS, RAILS_SKIP_ASSET_COMPILATION, BUNDLE_WITHOUT, RACK_ENV, RAILS_ENV. And so on. For the Ruby container, the assumption is you are running a Ruby app, hence setting some sensible defaults to make sure they are always available.

On the other hand, for the Docker container it seems it's open. You specify whatever variables you want ... they make no assumptions as to what you are running, Rails (Ruby), Django (Python) etc ... because it could be anything. They don't know before hand what you want to run and that makes it difficult to set sensible defaults.

Solangesolano answered 16/7, 2015 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.