2 Commits
0.1.6 ... 0.1.8

Author SHA1 Message Date
Morten Olsen
14c4d5c386 fix: support more than 10 SSM parameters 2025-09-10 14:09:09 +02:00
Morten Olsen
00786d5508 chore: minor QoL improvements 2025-08-08 20:22:10 +02:00
4 changed files with 68 additions and 65 deletions

View File

@@ -117,7 +117,7 @@ override the SSM-resolved values. To avoid this:
- Use `.env.with-ssm` instead of `.env` for SSM references
- Or use environment variable substitution if your app supports it:
`${API_KEY:-SSM:/myapp/api-key}`
`API_KEY=${API_KEY:-SSM:/myapp/api-key}`
### 🚀 Deployment Considerations

View File

@@ -5,48 +5,41 @@ import { exec } from './utils/exec.js';
import { getEnv } from './utils/env.js';
import { replaceParams } from './utils/ssm.js';
const main = async () => {
const argv = await yargs(hideBin(process.argv))
.usage('Usage: $0 [options] -- <command>')
.option('region', {
type: 'string',
description: 'The AWS region to use for SSM.',
})
.option('profile', {
type: 'string',
description: 'The AWS profile to use from your credentials file.',
})
.option('file', {
alias: 'f',
type: 'string',
description: 'The file to use for environment variables. (multiple files can be specified)',
default: ['.env', '.env.with-ssm'],
})
.demandCommand(1, 'Error: You must provide a command to execute after --')
.alias('h', 'help')
.epilogue('For more information, check the documentation.')
.parse();
const argv = await yargs(hideBin(process.argv))
.usage('Usage: $0 [options] -- <command>')
.option('region', {
type: 'string',
description: 'The AWS region to use for SSM.',
})
.option('profile', {
type: 'string',
description: 'The AWS profile to use from your credentials file.',
})
.option('file', {
alias: 'f',
type: 'string',
description: 'The file to use for environment variables. (multiple files can be specified)',
default: ['.env', '.env.with-ssm'],
})
.demandCommand(1, 'Error: You must provide a command to execute after --')
.alias('h', 'help')
.epilogue('For more information, check the documentation.')
.parse();
const command = argv._[0] as string;
const commandArgs = argv._.slice(1).map(String);
const command = argv._[0] as string;
const commandArgs = argv._.slice(1).map(String);
if (!command) {
console.error('No command provided');
process.exit(1);
}
const files = argv.file && Array.isArray(argv.file) ? argv.file : [argv.file];
const hostEnv = await getEnv(files);
const env = await replaceParams(hostEnv);
exec({
command,
env,
args: commandArgs,
});
};
main().catch((err) => {
console.error(err);
if (!command) {
console.error('No command provided');
process.exit(1);
}
const files = argv.file && Array.isArray(argv.file) ? argv.file : [argv.file];
const hostEnv = await getEnv(files);
const env = await replaceParams(hostEnv);
exec({
command,
env,
args: commandArgs,
});

View File

@@ -1,14 +0,0 @@
const splitArgs = (args: string[]) => {
const separatorIndex = args.indexOf('--');
const actionArgs = args.slice(0, separatorIndex);
const command = args[separatorIndex + 1];
const commandArgs = args.slice(separatorIndex + 2);
return {
actionArgs,
command,
commandArgs,
};
};
export { splitArgs };

View File

@@ -1,4 +1,4 @@
import { GetParametersCommand, SSMClient } from '@aws-sdk/client-ssm';
import { GetParametersCommand, SSMClient, type Parameter } from '@aws-sdk/client-ssm';
import { debug } from './debug.js';
@@ -30,18 +30,42 @@ const replaceParams = async (
return env;
}
const command = new GetParametersCommand({
Names: names,
WithDecryption: true,
});
// Chunk names into groups of 10 (AWS SSM GetParametersCommand limit)
const chunks: string[][] = [];
debug(`Chunking ${names.length} names into groups of 10`);
for (let i = 0; i < names.length; i += 10) {
chunks.push(names.slice(i, i + 10));
}
const response = await ssm.send(command);
if (response.InvalidParameters?.length || 0 > 0) {
console.error('Invalid SSM parameters', response.InvalidParameters);
debug(`Processing ${chunks.length} chunks`);
// Fetch parameters in chunks and combine results
const allParams: Parameter[] = [];
const allInvalidParams: string[] = [];
for (const chunk of chunks) {
const command = new GetParametersCommand({
Names: chunk,
WithDecryption: true,
});
const response = await ssm.send(command);
if (response.Parameters) {
allParams.push(...response.Parameters);
}
if (response.InvalidParameters) {
allInvalidParams.push(...response.InvalidParameters);
}
}
if (allInvalidParams.length > 0) {
console.error('Invalid SSM parameters', allInvalidParams);
process.exit(1);
}
const params = response.Parameters ?? [];
const params = allParams;
return Object.fromEntries(
Object.entries(env).map(([key, value]) => {