skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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"
    }
    

  2. Zag’s answer gave me the right idea, but didn’t quite work for me. Possibly because of a newer ebcli version. I have:

    $ eb --version
    EB CLI 3.20.10 (Python 3.11.7 (main, Dec  8 2023, 14:22:46) [GCC 13.2.0])
    

    The following function works with the above version:

    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; }
      vars=$(eb printenv "$1") || { echo "$vars"; return 1; }
      vars=$(echo "$vars" | sed '/Environment Variables:/d;s/ = /=/;s/ = /=/;s/^ *//')
      echo "$vars"
      eb setenv "$vars" --environment "$2"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search