Skip to content

Instantly share code, notes, and snippets.

@hyjk2000
Created October 12, 2021 01:46
Show Gist options
  • Save hyjk2000/3d8c631217bd52547f657a00068ab910 to your computer and use it in GitHub Desktop.
Save hyjk2000/3d8c631217bd52547f657a00068ab910 to your computer and use it in GitHub Desktop.
Load environment variables from AWS SSM for local development
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 });
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