AWS Elastic Beanstalk: How to use environment variables in ebextensions?

We are trying to save environment-specific application configuration files in s3. Files are stored in different subdirectories named after the environment, and also have the environment as part of the file name.

Examples

dev/application-dev.properties stg/application-stg.properties prd/application-prd.properties 

The elastic Beanstalk environments are called dev, stg, prd , and I also have an environment variable defined in Elastic Beanstalk called ENVIRONMENT, which can be dev, stg or prd ,

Now my question is: how do I refer to the environment name or the ENVIRONMENT variable when loading the configuration file from the configuration file in .ebextensions?

I tried to use the link {"Ref": "AWSEBEnvironmentName" } in .ebextensions / myapp.config, but when deploying I get a syntax error.

The contents of .ebextensions / myapp.config:

 files: /config/application-`{"Ref": "AWSEBEnvironmentName" }`.properties: mode: "000666" owner: webapp group: webapp source: https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Ref": "AWSEBEnvironmentName" }`.properties authentication: S3Access Resources: AWSEBAutoScalingGroup: Metadata: AWS::CloudFormation::Authentication: S3Access: type: S3 roleName: aws-elasticbeanstalk-ec2-role buckets: com.mycompany.api.config 

The error I am getting is:

 The configuration file .ebextensions/myapp.config in application version manualtest-18 contains invalid YAML or JSON. YAML exception: Invalid Yaml: mapping values are not allowed here in "<reader>", line 6, column 85: ... .config/stg/application-`{"Ref": "AWSEBEnvironmentName" }`.prop ... ^ , JSON exception: Invalid JSON: Unexpected character (f) at position 0.. Update the configuration file. 

What is the correct way to reference an environment variable in a .ebextensions configuration file in AWS Elastic Beanstalk?

+5
source share
3 answers

I struggled to get this to work until I found that the Sub function does not seem to be available in ebextensions: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions-functions.html

This means that you need to go back to Fn::Join and Ref , at least until Sub support is introduced in ebextensions. It also seems that the files attribute requires a fixed path (and I could not use Fn::Join in this context).

My general solution for this was as follows:

 Resources: AWSEBAutoScalingGroup: Metadata: AWS::CloudFormation::Authentication: S3Auth: type: S3 buckets: arn:aws:s3:::elasticbeanstalk-xxx roleName: aws-elasticbeanstalk-ec2-role files: "/tmp/application.properties" : mode: "000644" owner: root group: root source: { "Fn::Join" : ["", ["https://s3-xxx.amazonaws.com/elasticbeanstalk-xxx/path/to/application-", { "Ref" : "AWSEBEnvironmentName" }, ".properties" ]]} authentication: S3Auth container_commands: 01-apply-configuration: command: mkdir -p config && mv /tmp/application.properties config 

This will create an application.properties file (without an environment name qualifier) ​​in the config directory next to the deployed application instance.

If you want to save the environment name as part of the file name using this approach, you will need to adjust the command that moves the file to use a different Fn::Join expression to control the file name.

+4
source

You are almost there. Somewhere YAML formats are used while you are trying to use JSON. Use Ref: AWSEBEnvironmentName . Alternatively, you can use the Sub function to avoid pesky Join :

!Sub "/config/application-${AWSEBEnvironmentName}.properties"

+1
source

Your .ebextensions configuration file was almost correct. Substitution of the file name of the environment variable or the name of the AWS resource will not work, because for this, do as in the answer of Mark to rename the file created in the container_commands section.

The value of the source parameter, trying to access the AWS resource name using Ref , was correct, it just needed to be surrounded by a single quote ' , as shown below:

 files: /config/application.properties: mode: "000666" owner: webapp group: webapp source: 'https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Ref": "AWSEBEnvironmentName" }`.properties' authentication: S3Access 

And to access the environment variables use Fn :: GetOptionSetting . Environment variables are in aws:elasticbeanstalk:application:environment namespace .

The following is an example of the ENVIRONMENT environment ENVIRONMENT in the source files option:

 files: "/tmp/application.properties" : mode: "000666" owner: webapp group: webapp source: 'https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "ENVIRONMENT ", "DefaultValue": "dev"}}`.properties' authentication: S3Auth 
+1
source

Source: https://habr.com/ru/post/1264892/


All Articles