Skip to content

Commit 90e405a

Browse files
committed
Add the option to recursively copy ssm parameters to an env file
1 parent ab1cd57 commit 90e405a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

packer/linux/conf/bin/bk-fetch.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,66 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
# fetch_ssm_parameters fetches all SSM parameters under the given path and writes them to the given output file
5+
fetch_ssm_parameters() {
6+
local ssm_path="$1"
7+
local output_file="$2"
8+
9+
# check if the ssm_path is set
10+
if [ -z "$ssm_path" ]; then
11+
echo "ssm_path is not set"
12+
return 1
13+
fi
14+
15+
# check if the output_file is set
16+
if [ -z "$output_file" ]; then
17+
echo "output_file is not set"
18+
return 1
19+
fi
20+
21+
# trim off ssm: prefix
22+
ssm_path=${ssm_path//ssm:/}
23+
24+
# Get all SSM parameter names under the given path
25+
#
26+
# NOTE: The maximum number of parameters that can be retrieved is 25 to avoid throttling
27+
# in the case of misconfigured SSM path with a large number of child parameters
28+
local ssm_parameter_names
29+
ssm_parameter_names=$(aws ssm get-parameters-by-path \
30+
--path "$ssm_path" \
31+
--recursive \
32+
--max-items 25 \
33+
--with-decryption \
34+
--query 'Parameters[].Name' \
35+
--output text)
36+
37+
# Loop through each parameter and export it as an environment variable
38+
for name in $ssm_parameter_names; do
39+
local value
40+
value=$(aws ssm get-parameter \
41+
--name "$name" \
42+
--with-decryption \
43+
--query 'Parameter.Value' \
44+
--output text)
45+
if [ -n "$name" ] && [ -n "$value" ]; then
46+
local var_name
47+
var_name=$(echo "$name" | awk -F/ '{print toupper($NF)}')
48+
echo "Exported variable: $var_name"
49+
echo "$var_name=$value" >>"$output_file"
50+
fi
51+
done
52+
}
53+
454
FROM="$1"
555
TO="$2"
656

757
case "$FROM" in
858
s3://*)
959
exec aws s3 cp "$FROM" "$TO"
1060
;;
61+
ssm:*)
62+
fetch_ssm_parameters "$FROM" "$TO"
63+
;;
1164
*)
1265
exec curl -Lfs -o "$TO" "$FROM"
1366
;;

0 commit comments

Comments
 (0)