Search code examples
amazon-web-servicesamazon-elastic-beanstalk

Copy environment variables from one Elastic Beanstalk environment to another


Is there a bash command I can run to copy all the environment variables from one AWS Elastic Beanstalk environment to another? I'm trying to copy to a new environment, so assume that the target environment has no environment variables that need removing.

I'm aware of eb printenv and eb setenv, but these were not designed to work together.


Solution

  • You can string them together with a bit of bash magic:

    eb setenv -e <TARGET_ENVIRONMENT> \
    `eb printenv <SOURCE_ENVIRONMENT> | \
    sed 's/ = /=/' | sed 's/ Environment Variables://'`
    

    If you prefer, you can create the following bash function:

    function eb-copy-env {
        # Usage: eb-copy-env <source_environment> <dest_environment>
        test "$1" && test "$2" || { echo "Usage: eb-copy-env <source_environment> <dest_environment>"; return 1; }
        SOURCE_VARS=$(eb printenv $1) || { echo $SOURCE_VARS; return 1; }
        SOURCE_VARS=$(echo $SOURCE_VARS | sed 's/Environment Variables://' | sed 's/ = /=/g')
        echo $SOURCE_VARS
        echo -e "\nBeginning copy"
        eb setenv -e $2 $SOURCE_VARS
        echo -e "\nCopy successful"
    }