I am trying to access an environment variable that I have defined in the AWS Beanstalk configuration. I need to access it within a config file in .ebextensions
or in a file that is copied in place in a config file. I have tried the following:
container_commands:
update_nginx_config:
command: "cp .ebextensions/files/nginx/nginx.conf /etc/nginx/nginx.conf"
And in my nginx.conf
file, I have tried to access $MYVAR
, ${MYVAR}
and {$MYVAR}
, some of which was suggested here and here (the latter being directly within a config file).
files:
"/etc/nginx/nginx.conf" :
mode: "000644"
owner: root
group: root
content: |
$MYVAR ${MYVAR} {$MYVAR}
This does not work either. In all cases, the variable names are just output such as $MYVAR
, so Beanstalk does not recognize my variables. I found the below in the AWS documentation about container_commands
:
They also have access to environment variables such as your AWS security credentials.
This is great, but it does not say how.
How can I access an environment variable with ebextensions, be it within a config file itself or in a separate file that is copied in place?
Thank you in advance!
Environment properties are written to the /opt/python/current/env file, which is sourced into the virtualenv stack where the application runs. For more information, see Using the Elastic Beanstalk Python platform.
Sign 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.
I reached out to the Amazon technical support for an answer to this question, and here is their reply:
Unfortunately the variables are not available in ebextensions directly. The best option to do that is by creating a script that then is run from container commands like this:
files:
"/home/ec2-user/setup.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
# Commands that will be run on container_commmands
# Here the container variables will be visible as environment variables.
container_commands:
set_up:
command: /home/ec2-user/setup.sh
So, if you create a shell script and invoke it via a container command, then you will have access to environment variables within your shell script as follows: $ENVIRONMENT_VARIABLE
. I have tested this, and it works.
If you're having issues running a script as root and not being able to read the configured environment variables, try adding the following to the top of your script.
. /opt/elasticbeanstalk/support/envvars
Depending on your use case, you might have to change your approach a bit (at least I did), but it is a working solution. I hope this helps someone!
From this answer: https://stackoverflow.com/a/47817647/2246559
You can use the GetOptionSetting function described here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions-functions.html
For instance, if you were setting the worker_processes
variable, it could look like:
files:
"/etc/nginx/nginx.conf" :
mode: "000644"
owner: root
group: root
content: |
worker_processes `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MYVAR"}}`;
Note the backticks `` in the function call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With