Created
April 24, 2025 07:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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