Skip to content

Commit

Permalink
feat: add ssm params convert to .env script
Browse files Browse the repository at this point in the history
  • Loading branch information
quytranDF committed Sep 8, 2024
1 parent 0316449 commit a2d5249
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tools/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
STRING_PARAM_1=value1
SECURE_STRING_PARAM_2=value2
STRING_LIST_PARAM_3=value3a,value3b,value3c
37 changes: 37 additions & 0 deletions tools/convert_ssm_params_to_dotenv.sh
Original file line number Diff line number Diff line change
@@ -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 "/<path_to_params>" --with-decryption --recursive > <name_of_the_profile>.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 <input_json_file>"
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'"
31 changes: 31 additions & 0 deletions tools/example_ssm_params.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}

0 comments on commit a2d5249

Please sign in to comment.