diff --git a/tools/.env.example b/tools/.env.example new file mode 100644 index 0000000..ca300f7 --- /dev/null +++ b/tools/.env.example @@ -0,0 +1,3 @@ +STRING_PARAM_1=value1 +SECURE_STRING_PARAM_2=value2 +STRING_LIST_PARAM_3=value3a,value3b,value3c diff --git a/tools/convert_ssm_params_to_dotenv.sh b/tools/convert_ssm_params_to_dotenv.sh new file mode 100755 index 0000000..b702bc0 --- /dev/null +++ b/tools/convert_ssm_params_to_dotenv.sh @@ -0,0 +1,37 @@ +#!/bin/bash +set -ex + +# *** This script is used to convert the results get from SSM parameters from one AWS profile (or account) into .env file *** + +# *** First, you need to get the list of SSM parameters from the source AWS profile (or account) and save it to a json file *** +# *** Run this command: aws ssm get-parameters-by-path --path "/" --with-decryption --recursive > .json *** + +# *** Then you run this script to convert the SSM parameters to the .env file *** + +# Check if the input JSON file is provided +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + +json_file=$1 + +# Output file for environment variables +env_file=".env" + +# Empty the existing env file (optional) +> "$env_file" + +# Loop through each parameter in the JSON file and convert to .env format +jq -r '.Parameters[] | .Name = (.Name | gsub("^/[^/]+/"; "") | gsub("/"; "_")) | if .Value == "" then "Empty value for key: " + .Name else .Name + "=" + .Value end' "$json_file" | while read -r line; do + if [[ $line == Empty* ]]; then + echo "$line" + else + # Remove the project name prefix + cleaned_line=$(echo "$line" | sed 's/^mapsorter_//') + echo "$cleaned_line" >> "$env_file" + fi +done + +# Print success message +echo "Successfully exported parameters to '$env_file'" diff --git a/tools/example_ssm_params.json b/tools/example_ssm_params.json new file mode 100644 index 0000000..3f10758 --- /dev/null +++ b/tools/example_ssm_params.json @@ -0,0 +1,31 @@ +{ + "Parameters": [ + { + "Name": "/dev/STRING_PARAM_1", + "Type": "String", + "Value": "value1", + "Version": 1, + "LastModifiedDate": "2024-08-05T14:08:53.195000+07:00", + "ARN": "arn:aws:ssm:us-east-1:905418451816:parameter/dev/STRING_PARAM_1", + "DataType": "text" + }, + { + "Name": "/dev/SECURE_STRING_PARAM_2", + "Type": "SecureString", + "Value": "value2", + "Version": 1, + "LastModifiedDate": "2024-08-05T14:08:44.258000+07:00", + "ARN": "arn:aws:ssm:us-east-1:905418451816:parameter/dev/SECURE_STRING_PARAM_2", + "DataType": "text" + }, + { + "Name": "/dev/STRING_LIST_PARAM_3", + "Type": "StringList", + "Value": "value3a,value3b,value3c", + "Version": 1, + "LastModifiedDate": "2024-08-05T14:08:44.258000+07:00", + "ARN": "arn:aws:ssm:us-east-1:905418451816:parameter/dev/STRING_LIST_PARAM_3", + "DataType": "text" + } + ] +}