How to Import Environment Properties from One AWS Elastic Beanstalk Environment to Another

To Nha Notes | Aug. 8, 2024, 11:40 a.m.

Option 1

Step 1: Retrieve Environment Properties from the Existing Environment

aws elasticbeanstalk describe-configuration-settings \
    --application-name your-application-name \
    --environment-name your-existing-environment-name \
    --query "ConfigurationSettings[0].OptionSettings" \
    > existing-env-config.json

 

Step 2: Filter Out Environment Properties

jq '[.[] | select(.Namespace=="aws:elasticbeanstalk:application:environment")]' existing-env-config.json > env-properties.json
 

Step 3: Update the New Environment

aws elasticbeanstalk update-environment \
    --environment-name your-new-environment-name \
    --option-settings file://env-properties.json

 

Option 2

Step 1: Save EB Configuration

Use the Elastic Beanstalk saved configurations blog to save your environment configuration.

Step 2: Download Configuration from S3

Download the saved configuration file from your S3 bucket, typically located at a URI like:

s3://elasticbeanstalk-us-west-2-123456789012/resources/templates/my-app/

Step 3: Extract and Format Environment Variables

Use the following Python script to extract and format the environment variables from the aws:elasticbeanstalk:application:environment section of the downloaded file:

eb_env_options = """
<CONFIGURATION_OPTIONS_IN_DOWNLOAD_FILE_HERE>
"""
eb_env_options = eb_env_options.strip().split('\n')
eb_env_options = [x.split(': ') for x in eb_env_options]
eb_env_options = {f"\"{x[0]}\" = \"{x[1]}\"" for x in eb_env_options}
eb_env_options = '\n'.join(eb_env_options)
print(eb_env_options)

Step 4: Add to Terraform

Copy the output from the Python script into the env_vars variable in your terraform.tfvars file:

env_vars = {
   <EB_ENV_OPTIONS_OUTPUT>
}