Search code examples
bashshellgnu-parallel

Bash script - AWS SSM export variables in parallel


My original script loads SSM variables from AWS and works fine but each variables takes about 1 second

#!/bin/bash

getEnvironmentVariable() {
  SECRET=$1
  ssm_value=$(aws ssm get-parameter --name "/TEST_PREFIX/${SECRET}" --with-decryption --query 'Parameter.Value' --output text)
  export "${SECRET}"="${ssm_value}"
}

getEnvironmentVariable "TEST_SECRET_1"
getEnvironmentVariable "TEST_SECRET_2"

Instead I would love to pull environment variables in parallel and export them.

My attempt at parallelizing them.

#!/bin/bash

getEnvironmentVariable() {
  SECRET=$1
  ssm_value=$(aws ssm get-parameter --name "/TEST_PREFIX/${SECRET}" --with-decryption --query 'Parameter.Value' --output text)
  echo "${SECRET}"="${ssm_value}"
}

export $(getEnvironmentVariable "TEST_SECRET_1") &
export $(getEnvironmentVariable "TEST_SECRET_2") &
wait

env | grep "TEST_SECRET_2"

I'm getting a little stuck on how to run things in parallel with a subshell and still be able to export them.

Is it possible to fetch and export the values in parallel?


Solution

  • You are looking for parset (Introduced in 20170422, but has seen heavy development in the past year):

    #!/bin/bash
    
    . `which env_parallel.bash`
    
    getEnvironmentVariable() {
      SECRET=$1
      aws ssm get-parameter --name "/TEST_PREFIX/${SECRET}" --with-decryption --query 'Parameter.Value' --output text
    }
    export -f getEnvironmentVariable
    
    parset TEST_SECRET_1,TEST_SECRET_2 getEnvironmentVariable ::: TEST_SECRET_1 TEST_SECRET_2
    echo $TEST_SECRET1
    
    # And if you need it exported:
    export TEST_SECRET_1
    export TEST_SECRET_2
    bash -c 'echo $TEST_SECRET2'