1 Commits

Author SHA1 Message Date
Morten Olsen
64f6f6df6a docs: add compare section 2025-08-08 15:14:36 +02:00
11 changed files with 1216 additions and 1414 deletions

1
.env.with-ssm Normal file
View File

@@ -0,0 +1 @@
PASSWORD=SSM:/test/hfd/rds/DB_USER

0
.npmrc
View File

View File

@@ -25,7 +25,7 @@ that get resolved at runtime.
## Installation
```bash
npm install -g @morten-olsen/with-ssm
npm install -g @0morten-olsen/with-ssm
```
## Quick Start
@@ -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=${API_KEY:-SSM:/myapp/api-key}`
`${API_KEY:-SSM:/myapp/api-key}`
### 🚀 Deployment Considerations

View File

@@ -1,2 +1,2 @@
#!/usr/bin/env node
import '../dist/index.js';
import '../dist/start.js';

View File

@@ -6,37 +6,37 @@
"license": "GPL-3.0",
"scripts": {
"test:lint": "eslint",
"build": "ncc build src/start.ts -s -o dist",
"build:dev": "ncc build src/start.ts -s -o dist --watch",
"build": "tsc --build",
"build:dev": "tsc --build --watch",
"test:unit": "vitest --run --passWithNoTests",
"test": "pnpm run \"/^test:/\""
},
"packageManager": "pnpm@10.17.0",
"packageManager": "pnpm@10.6.0",
"files": [
"dist"
],
"devDependencies": {
"@aws-sdk/client-ssm": "^3.901.0",
"@aws-sdk/client-sts": "^3.901.0",
"@dotenvx/dotenvx": "^1.51.0",
"@eslint/eslintrc": "3.3.1",
"@eslint/js": "9.36.0",
"@eslint/js": "9.32.0",
"@pnpm/find-workspace-packages": "6.0.9",
"@types/node": "24.6.2",
"@types/node": "24.2.0",
"@types/yargs": "^17.0.33",
"@vercel/ncc": "^0.38.4",
"@vitest/coverage-v8": "3.2.4",
"eslint": "9.36.0",
"eslint": "9.32.0",
"eslint-config-prettier": "10.1.8",
"eslint-plugin-import": "2.32.0",
"eslint-plugin-prettier": "5.5.4",
"execa": "^9.6.0",
"prettier": "3.6.2",
"typescript": "5.9.3",
"typescript-eslint": "8.45.0",
"vitest": "3.2.4",
"yargs": "^18.0.0"
"typescript": "5.9.2",
"typescript-eslint": "8.39.0",
"vitest": "3.2.4"
},
"name": "@morten-olsen/with-ssm",
"version": "1.0.0"
"version": "1.0.0",
"dependencies": {
"@aws-sdk/client-ssm": "^3.859.0",
"dotenv": "^17.2.1",
"execa": "^9.6.0",
"yargs": "^18.0.0"
}
}

2501
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,20 +0,0 @@
import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts';
const ensureAWS = async (region?: string, profile?: string) => {
const sts = new STSClient({
region,
profile,
});
const command = new GetCallerIdentityCommand({});
try {
await sts.send(command);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.error('Failed to get caller identity', errorMessage);
process.exit(1);
}
};
export { ensureAWS };

14
src/utils/cli.ts Normal file
View File

@@ -0,0 +1,14 @@
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

@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { parse } from '@dotenvx/dotenvx';
import { parse } from 'dotenv';
import { debug } from './debug.js';

View File

@@ -1,7 +1,6 @@
import { GetParametersCommand, SSMClient, type Parameter } from '@aws-sdk/client-ssm';
import { GetParametersCommand, SSMClient } from '@aws-sdk/client-ssm';
import { debug } from './debug.js';
import { ensureAWS } from './aws.js';
const PREFIX = 'SSM:';
@@ -14,6 +13,11 @@ const replaceParams = async (
env: Record<string, string | undefined>,
{ region, profile }: ReplaceParamsOptions = {},
) => {
const ssm = new SSMClient({
region,
profile,
});
const names = Object.entries(env)
.filter(([, value]) => value?.startsWith(PREFIX))
.map(([, value]) => value?.slice(PREFIX.length))
@@ -26,47 +30,18 @@ const replaceParams = async (
return env;
}
await ensureAWS(region, profile);
const ssm = new SSMClient({
region,
profile,
});
// 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));
}
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,
Names: names,
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);
if (response.InvalidParameters?.length || 0 > 0) {
console.error('Invalid SSM parameters', response.InvalidParameters);
process.exit(1);
}
const params = allParams;
const params = response.Parameters ?? [];
return Object.fromEntries(
Object.entries(env).map(([key, value]) => {

View File

@@ -10,7 +10,6 @@
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"noEmit": true,
"outDir": "dist",
"jsx": "react-jsx",
"isolatedModules": true,