Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing env variables from Elastic Beanstalk .ebextensions config files

Is it posssible to reference the PARAM1 / PARAM2 etc.. container environment properties from the .ebextensions config files. If so, how? I tried $PARAM1 but it seemed to be an empty value.

I want to set the hostname on startup to contain DEV, QA or PROD, which I pass to my container via the PARAM1 environment variable.

commands:   01-set-correct-hostname:     command: hostname myappname{$PARAM1}.com 
like image 488
Kevin Avatar asked May 16 '13 11:05

Kevin


People also ask

How do I access Elastic Beanstalk environment variables?

Elastic Beanstalk lets you enter the environment variables for each environment using the management panel. 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.

Where does Elastic Beanstalk store configuration files?

ebextensions, see the Elastic Beanstalk Configuration Files Repository . Location – Place all of your configuration files in a single folder, named . ebextensions , in the root of your source bundle.

What are the two environments available in 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.

Which file is required for creating links between environments in Beanstalk?

Each component application must have an env. yaml configuration file in its application source bundle that specifies the parameters used to create its environment.

How to access environment variables in Elastic Beanstalk?

And to access environment variables use Fn::GetOptionSetting. Environment variables are in aws:elasticbeanstalk:application:environment namespace. Below example access an environment variable ENVIRONMENT in source option of files:

How do I deploy an ebextension to Elastic Beanstalk?

Create a .ebextension file in your application source bundle and include the following: Note: The configuration file in step 1 is called setvars.config. 2. Save the .ebextension file, and then deploy it to your Elastic Beanstalk environment.

How do I change the default configuration files in Elastic Beanstalk?

However, you can make any configuration change in Elastic Beanstalk configuration files identically using either YAML or JSON. When you are developing or testing new configuration files, launch a clean environment running the default application and deploy to that.

How do I configure beanstalk to work with different environments?

1. Open the Elastic Beanstalk console. 2. Select your application, and then choose Configuration from the navigation pane. 3. For the Software category, choose Modify. 4. In the Environment properties section, enter the key-value pairs for the environment properties that you want to pass to your instances. 5.


2 Answers

It turns out you can only do this in the container_commands section, not the commands section.

This works:

container_commands:   01-set-correct-hostname:     command: "hostname myappname{$PARAM1}.com" 

See http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-container_commands for more details.

like image 158
Kevin Avatar answered Sep 23 '22 16:09

Kevin


Here is what worked for me. I tried the accepted approach and it did not produce the desired result (curly braces were included in the output). Troubleshooting commands that are executed from a .config file when uploading to Elastic Beanstalk is also a bit of a challenge (or I just don't know exactly where to look).

AWS Environment:

  • Type - Elastic Beanstalk
  • Platform - 64bit Amazon Linux 2015.09 v2.0.4 running PHP 5.6

Elastic Beanstalk Environment Properties (Configuration -> Software Configuration -> Environment Properties):

  • Property Name - HELLO_VARIABLE
  • Property Value - test

Sample .config File included in the .ebextensions folder in the deployment artifact:

container_commands:   0_test-variable:     cwd: /tmp     command: "touch ${HELLO_VARIABLE}_0_.txt"   1_test-variable:     cwd: /tmp     command: "touch {$HELLO_VARIABLE}_1_.txt"   2_test-variable:     cwd: /tmp     command: "touch $HELLO_VARIABLE_2_.txt" 

After the artifact has been deployed using Elastic Beanstalk the /tmp directory within an EC2 instance will contain the following files (note curly braces and position of $):

  • touch ${HELLO_VARIABLE}_0_.txt creates /tmp/test_0_.txt
  • touch {$HELLO_VARIABLE}_1_.txt creates /tmp/{test}_1_.txt
  • touch $HELLO_VARIABLE_2_.txt creates /tmp/.txt
like image 23
Maciej Avatar answered Sep 21 '22 16:09

Maciej