|
1 | 1 | #!/bin/bash
|
2 | 2 | set -euo pipefail
|
3 | 3 |
|
| 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 | + |
4 | 54 | FROM="$1"
|
5 | 55 | TO="$2"
|
6 | 56 |
|
7 | 57 | case "$FROM" in
|
8 | 58 | s3://*)
|
9 | 59 | exec aws s3 cp "$FROM" "$TO"
|
10 | 60 | ;;
|
| 61 | +ssm:*) |
| 62 | + fetch_ssm_parameters "$FROM" "$TO" |
| 63 | + ;; |
11 | 64 | *)
|
12 | 65 | exec curl -Lfs -o "$TO" "$FROM"
|
13 | 66 | ;;
|
|
0 commit comments