Bash 脚本 - AWS SSM 并行导出变量

Bash script - AWS SSM export variables in parallel

我的原始脚本从 AWS 加载 SSM 变量并且工作正常,但每个变量大约需要 1 秒

#!/bin/bash

getEnvironmentVariable() {
  SECRET=
  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"

相反,我更愿意并行提取环境变量并导出它们。

我尝试将它们并行化。

#!/bin/bash

getEnvironmentVariable() {
  SECRET=
  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"

我对如何 运行 东西与子 shell 并行并仍然能够导出它们感到有点困惑。

是否可以并行获取和导出值?

您正在寻找parset (20170422推出,但在过去的一年里有了长足的发展):

#!/bin/bash

. `which env_parallel.bash`

getEnvironmentVariable() {
  SECRET=
  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'