Skip to content

Instantly share code, notes, and snippets.

@ricbra
Created April 24, 2025 07:11
Show Gist options
  • Save ricbra/1c4ab35bc292e38a46651345fd107333 to your computer and use it in GitHub Desktop.
Save ricbra/1c4ab35bc292e38a46651345fd107333 to your computer and use it in GitHub Desktop.
Fetch value for SSM parameter or if not found show list of possible alternatives
#!/bin/bash
# Check if parameter path is provided
if [ -z "$1" ]; then
echo "Usage: $0 <SSM_PARAMETER_PATH>"
exit 1
fi
PARAMETER_PATH="$1"
# Try to fetch the parameter value
PLAINTEXT_VALUE=$(aws ssm get-parameter \
--name "$PARAMETER_PATH" \
--with-decryption \
--query "Parameter.Value" \
--output text 2>/dev/null)
# If fetch failed, search and list alternatives
if [ $? -ne 0 ]; then
echo "Parameter not found: $PARAMETER_PATH"
# Remove the last part after the final slash
BASE_PATH="${PARAMETER_PATH%/*}/"
echo "Listing parameters under: $BASE_PATH"
aws ssm get-parameters-by-path \
--path "$BASE_PATH" \
--recursive \
--query "Parameters[].Name" \
--output text | tr '\t' '\n'
else
# Print the found value
echo "$PLAINTEXT_VALUE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment