Skip to content

Commit fa19ce7

Browse files
committed
Collapsed the loop and retrieval into one command and addressed feedback
1 parent 90e405a commit fa19ce7

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

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

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,31 @@ fetch_ssm_parameters() {
77
local output_file="$2"
88

99
# check if the ssm_path is set
10-
if [ -z "$ssm_path" ]; then
10+
if [[ -z "${ssm_path}" ]]; then
1111
echo "ssm_path is not set"
1212
return 1
1313
fi
1414

1515
# check if the output_file is set
16-
if [ -z "$output_file" ]; then
16+
if [[ -z "${output_file}" ]]; then
1717
echo "output_file is not set"
1818
return 1
1919
fi
2020

2121
# trim off ssm: prefix
22-
ssm_path=${ssm_path//ssm:/}
22+
ssm_path="${ssm_path//ssm:/}"
2323

24-
# Get all SSM parameter names under the given path
2524
#
2625
# NOTE: The maximum number of parameters that can be retrieved is 25 to avoid throttling
2726
# 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" \
27+
aws ssm get-parameters-by-path \
28+
--path "${ssm_path}" \
3129
--recursive \
3230
--max-items 25 \
3331
--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
32+
--query 'Parameters[*].{Name: Name, Value: Value}' --output json \
33+
| jq -r '.[] | [(.Name | split("/")[-1] | ascii_upcase), (["\"", .Value, "\""] | join(""))] | join("=")' \
34+
>"${output_file}"
5235
}
5336

5437
FROM="$1"
@@ -59,7 +42,7 @@ s3://*)
5942
exec aws s3 cp "$FROM" "$TO"
6043
;;
6144
ssm:*)
62-
fetch_ssm_parameters "$FROM" "$TO"
45+
fetch_ssm_parameters "${FROM}" "${TO}"
6346
;;
6447
*)
6548
exec curl -Lfs -o "$TO" "$FROM"

0 commit comments

Comments
 (0)