Env Variable in .ebextensions "files:" section
Asked Answered
C

1

20

I defined an environment variable called MY_ENVIRONMENT_VARIABLE in AWS Elastic Beanstalk's Software Configuration tab.

Now I would like to use this environment variable in the "files:" section of a .ebextensions config file.

Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Auth:
          type: S3
          buckets: arn:aws:s3:::SomeS3Bucket
          roleName: aws-elasticbeanstalk-ec2-role

files:
  "/tmp/application.properties" :
    mode: "000644"
    owner: root
    group: root
    source: { "Ref" : "MY_ENVIRONMENT_VARIABLE" }
    authentication: S3Auth

container_commands:
  01-apply-configuration:
    command: mv /tmp/application.properties .

It seems to be possible to reference environment variables in the "container_commands:" section (by using bash scripts) but I couldn't find any references that it is possible inside the "files:" section.

Does anybody have an example of how to use environment variables inside the "files:" section?

Climacteric answered 16/8, 2017 at 14:1 Comment(0)
B
38

Use Fn::GetOptionSetting to retreive environment variables. Environment variables are in aws:elasticbeanstalk:application:environment namespace

files:
  "/tmp/application.properties" :
    mode: "000644"
    owner: root
    group: root
    source: '`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MY_ENVIRONMENT_VARIABLE", "DefaultValue": "file_path"}}`'
    authentication: S3Auth

Note the backtick which perform the command substitution. DefaultValue attribute is optional, which is used in case environment variable is not found.

Above config file will create file /tmp/application.properties with content from the file referenced in environment variable MY_ENVIRONMENT_VARIABLE.

Blastula answered 14/12, 2017 at 16:6 Comment(3)
DO NOT CHANGE "aws:elasticbeanstalk:application:environment" by passing your application and environment names. Leave as is. It is clever enough to understand where it runs, and will use the environment variables you provide in your environment's configuration.Mispickel
Hi, I tried this command to get the value of an environment variable inside my filebeat configuration file. However, the filebeat service fails. Filebeat config file is in .yml format. Does that mean I have to change the syntax? Here's my question : #66686737Morpho
I can't believe it worked! Thanks for explaining the backticks. I haven't seen that documented anywhere in Elastic Beanstalk if reference to YAML syntax.Pedicab

© 2022 - 2024 — McMap. All rights reserved.