Skip to content

Instantly share code, notes, and snippets.

@gryzinsky
Last active January 6, 2024 19:14
Show Gist options
  • Save gryzinsky/4bab74c04c43ffb3ad794aadd831fd12 to your computer and use it in GitHub Desktop.
Save gryzinsky/4bab74c04c43ffb3ad794aadd831fd12 to your computer and use it in GitHub Desktop.
Script to convert a JSON to .env

AWS Secrets Manager to environment

This script is a utility to easily convert any JSON into a KEY=VALUE format (like env files).

Installation

Copy the contents of to_env.sh to /usr/local/bin and make it executable:

curl https://gist.githubusercontent.com/gryzinsky/4bab74c04c43ffb3ad794aadd831fd12/raw/b8fe744eb819bd380b59e402d3001c33885a9f3c/to_env.sh > /usr/local/bin/to_env && chmod 755 /usr/local/bin/to_env

Usage

Store your environment as a KV pair in AWS Secrets Manager.

Now, using the AWS CLI, retrieve and convert the secret to env format:

aws secretsmanager get-secret-value --secret-id "my-secret" | to_env.

You can modify the script to support any JSON input, just remove jq -r .SecretString | from line 10.

#!/bin/bash
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Please install jq first."
exit 1
fi
# Run the jq and sed command
jq -r .SecretString | jq -r 'to_entries | .[] | "\(.key | ascii_upcase)=\"\(.value)\""' | sed '/^null=/d'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment