How do I get environment variables from SSM Parameter Store and use them from an Elastic Beanstalk instance shell?

2 minute read
2

I want to get environment variables from Parameter Store, a capability of AWS Systems Manager, and print them in an AWS Elastic Beanstalk instance shell.

Short description

Amazon Linux 2 uses the get-config script to load environment variables. Elastic Beanstalk doesn't support Parameter Store. You must integrate Parameter Store with either the .ebextension or .platform script.

Note: The following resolution uses two .ebextension files. The first configuration file, ssm.config, reads the value from the Parameter Store and adds it to the Elastic Beanstalk environment variables. The second configuration file, shell.config, exports the value to the instance shell.

Resolution

To get and use environment variables from Parameter Store in your Elastic Beanstalk instance shell, complete the following steps:

1.    Open the AWS Systems Manager console.

2.    Create the test variables in Parameter Store with string as the type.

3.    Create the .ebextension ssm.config file in your application source bundle, and then add the following configurations:
option_settings: aws:elasticbeanstalk:application:environment:
MY_ENV_VAR: '{{resolve:ssm:test:1}}'

4.    Create another .ebextension shell.config file in your application source bundle, and then add the following configurations:

commands:
    setvars:
        command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries |                   
                          .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/local.sh
packages:
    yum:
        jq: []

5.    Run the following command to test that the variables export. Close any current sessions and use SSH to connect to your instance.

Note: Replace example-variable-name with the variable that's defined in your environment.

env | grep example-variable-name

6.    Review the output to make sure that the environment variables are correctly set.

AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago