Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an environment variable in Amazon Elastic Beanstalk (Python)

I have been working on a Django application lately, trying to get it to work with Amazon Elastic Beanstalk.

In my .ebextensions/python.config file, I have set the following:

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name:  ProductionBucket
    value: s3-bucket-name
  - namespace: aws:elasticbeanstalk:application:environment
    option_name:  ProductionCache
    value:  memcached-server.site.com:11211

However, whenever I look on the server, no such environment variables are set (and as such, aren't accessible when I try os.getenv('ProductionBucket')

I came across this this page which appears to attempt to document all the namespaces. I've also tried using PARAM1 as the option name, but have had similar results.

How can I set environment variables in Amazon Elastic Beanstalk?

EDIT:
I have also tried adding a command prior to all other commands which would just export an environment variable:

commands:
 01_env_vars:
  command: "source scripts/env_vars"

... This was also unsuccessful

like image 821
NT3RP Avatar asked Jan 08 '13 01:01

NT3RP


People also ask

How do you check environment variables in Elastic Beanstalk?

On AWS, open Elastic Beanstalk. Go to your Application > Environment > Configuration > Software Configuration . Under Environment Properties you will find a list of properties you can configure. These variables will be attached to the process.

How do I use an environment variable in AWS?

To set environment variablesSign in to the AWS Management Console and open the Amplify console . In the Amplify console, choose App Settings, and then choose Environment variables. In the Environment variables section, choose Manage variables. In the Manage variables section, under Variable, enter your key.

How do I get an Elastic Beanstalk environment ID?

The EB environment name and id is present in your instance tags. You could run the AWS CLI: aws ec2 describe-tags to get the environment name. The tag keys would be: elasticbeanstalk:environment-id.


1 Answers

I did the following to also get my environment variables that I configure in cloudformation in the non-container phase, eg the regular commands

/opt/elasticbeanstalk/bin/get-config environment | python -c "import json,sys; obj=json.load(sys.stdin); f = open('/tmp/eb_env', 'w'); f.write('\n'.join(map(lambda x: 'export ' + x[0] + '=' + x[1], obj.iteritems())))"

Once you execute this command you will have a file in /tmp/eb_env with all your environment variables. Just execute the following before a command that needs the environment variables

source /tmp/eb_env

Example

source /tmp/eb_env && echo $MY_CUSTOM_ENV

In the config file of elastic beanstalk, it looks like this:

commands:
    02-make-sure-we-can-get-our-env-in-the-instance-itself:
        command: "/opt/elasticbeanstalk/bin/get-config environment | python -c 'import json,sys; obj=json.load(sys.stdin); f = open(\'/tmp/eb_env\', \'w\'); f.write(\'\n\'.join(map(lambda x: \'export \' + x[0] + \'=\' + x[1], obj.iteritems())))'"
like image 145
user16423 Avatar answered Sep 28 '22 01:09

user16423