Created
October 12, 2021 01:46
-
-
Save hyjk2000/3d8c631217bd52547f657a00068ab910 to your computer and use it in GitHub Desktop.
Load environment variables from AWS SSM for local development
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
const { execSync, spawn } = require('child_process'); | |
const AWS_PROFILE = process.argv[2]; | |
const PARAMETERS_PATH = '/path/to/env/'; | |
const { Parameters: params } = JSON.parse( | |
execSync(`aws --profile ${AWS_PROFILE} ssm get-parameters-by-path --path "${PARAMETERS_PATH}" --with-decryption`) | |
); | |
console.log(`\n## Setting AWS env from profile: ${AWS_PROFILE}`); | |
const env = params.reduce((acc, { Name: name, Value: value }) => { | |
const key = name.replace(PARAMETERS_PATH, ''); | |
console.log(key); | |
acc[key] = value; | |
return acc; | |
}, { PATH: process.env.PATH }); | |
console.log(''); | |
const [command, ...args] = process.argv.slice(3); | |
spawn(command, args, { stdio: 'inherit', shell: process.env.SHELL, env }); |
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
node aws-ssm-env.js staging yarn dev |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment