-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ssm params convert to .env script
- Loading branch information
quytranDF
committed
Sep 8, 2024
1 parent
0316449
commit a2d5249
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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'" |
This file contains 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
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" | ||
} | ||
] | ||
} |