Skip to content

Commit 9e440f8

Browse files
authored
Merge pull request #1455 from buildkite/feat_add_ssm_export_for_env_file
Add the option to recursively copy ssm parameters to an env file
2 parents ab1cd57 + fa19ce7 commit 9e440f8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
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+
#
25+
# NOTE: The maximum number of parameters that can be retrieved is 25 to avoid throttling
26+
# in the case of misconfigured SSM path with a large number of child parameters
27+
aws ssm get-parameters-by-path \
28+
--path "${ssm_path}" \
29+
--recursive \
30+
--max-items 25 \
31+
--with-decryption \
32+
--query 'Parameters[*].{Name: Name, Value: Value}' --output json \
33+
| jq -r '.[] | [(.Name | split("/")[-1] | ascii_upcase), (["\"", .Value, "\""] | join(""))] | join("=")' \
34+
>"${output_file}"
35+
}
36+
437
FROM="$1"
538
TO="$2"
639

740
case "$FROM" in
841
s3://*)
942
exec aws s3 cp "$FROM" "$TO"
1043
;;
44+
ssm:*)
45+
fetch_ssm_parameters "${FROM}" "${TO}"
46+
;;
1147
*)
1248
exec curl -Lfs -o "$TO" "$FROM"
1349
;;

0 commit comments

Comments
 (0)