ssm-to-env-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +90 -0
- package/package.json +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# config-loader-demo
|
|
2
|
+
|
|
3
|
+
A CLI tool that loads AWS SSM Parameter Store values and outputs them as JSON, `.env` format, or shell exports. Built on top of [`aws-parameters-store`](https://www.npmjs.com/package/aws-parameters-store).
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Output as JSON (default)
|
|
9
|
+
npx ts-node src/index.ts /myapp/production/
|
|
10
|
+
|
|
11
|
+
# Output as .env key=value pairs
|
|
12
|
+
npx ts-node src/index.ts /myapp/staging/ --format env
|
|
13
|
+
|
|
14
|
+
# Output as shell exports (useful for sourcing)
|
|
15
|
+
npx ts-node src/index.ts /myapp/dev/ --format export >> .env
|
|
16
|
+
|
|
17
|
+
# Specify region
|
|
18
|
+
npx ts-node src/index.ts /myapp/production/ --region eu-west-1
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## How it works
|
|
22
|
+
|
|
23
|
+
1. Calls `getParams(namespace, { region })` from `aws-parameters-store`
|
|
24
|
+
2. Gets back a nested object based on the SSM parameter path hierarchy
|
|
25
|
+
3. Flattens + formats into the desired output (JSON, env, or export)
|
|
26
|
+
|
|
27
|
+
## Example
|
|
28
|
+
|
|
29
|
+
Given these SSM parameters:
|
|
30
|
+
```
|
|
31
|
+
/myapp/prod/database/host = "db.example.com"
|
|
32
|
+
/myapp/prod/database/port = "5432"
|
|
33
|
+
/myapp/prod/redis/url = "redis://cache:6379"
|
|
34
|
+
/myapp/prod/features/dark_mode = true
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Running:
|
|
38
|
+
```bash
|
|
39
|
+
npx ts-node src/index.ts /myapp/prod/ --format env
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Outputs:
|
|
43
|
+
```
|
|
44
|
+
DATABASE_HOST=db.example.com
|
|
45
|
+
DATABASE_PORT=5432
|
|
46
|
+
REDIS_URL=redis://cache:6379
|
|
47
|
+
FEATURES_DARK_MODE=true
|
|
48
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const aws_parameters_store_1 = __importDefault(require("aws-parameters-store"));
|
|
8
|
+
function flattenObject(obj, prefix = '') {
|
|
9
|
+
const result = {};
|
|
10
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
11
|
+
const envKey = prefix ? `${prefix}_${key}`.toUpperCase() : key.toUpperCase();
|
|
12
|
+
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
13
|
+
Object.assign(result, flattenObject(value, envKey));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
result[envKey] = String(value);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
function formatOutput(config, format) {
|
|
22
|
+
switch (format) {
|
|
23
|
+
case 'json':
|
|
24
|
+
return JSON.stringify(config, null, 2);
|
|
25
|
+
case 'env': {
|
|
26
|
+
const flat = flattenObject(config);
|
|
27
|
+
return Object.entries(flat)
|
|
28
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
29
|
+
.join('\n');
|
|
30
|
+
}
|
|
31
|
+
case 'export': {
|
|
32
|
+
const flat = flattenObject(config);
|
|
33
|
+
return Object.entries(flat)
|
|
34
|
+
.map(([key, value]) => `export ${key}="${value}"`)
|
|
35
|
+
.join('\n');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function loadConfig(namespace, region, format) {
|
|
40
|
+
console.error(`Loading parameters from: ${namespace} (region: ${region})`);
|
|
41
|
+
const config = await (0, aws_parameters_store_1.default)(namespace, { region });
|
|
42
|
+
const output = formatOutput(config, format);
|
|
43
|
+
console.log(output);
|
|
44
|
+
}
|
|
45
|
+
// --- CLI ---
|
|
46
|
+
function printUsage() {
|
|
47
|
+
console.log(`
|
|
48
|
+
Usage: npx ts-node src/index.ts <namespace> [options]
|
|
49
|
+
|
|
50
|
+
Options:
|
|
51
|
+
--region <region> AWS region (default: us-east-1)
|
|
52
|
+
--format <format> Output format: json | env | export (default: json)
|
|
53
|
+
--help Show this help
|
|
54
|
+
|
|
55
|
+
Examples:
|
|
56
|
+
npx ts-node src/index.ts /myapp/production/
|
|
57
|
+
npx ts-node src/index.ts /myapp/staging/ --region eu-west-1 --format env
|
|
58
|
+
npx ts-node src/index.ts /myapp/dev/ --format export >> .env
|
|
59
|
+
`);
|
|
60
|
+
}
|
|
61
|
+
function parseArgs(args) {
|
|
62
|
+
if (args.length === 0 || args.includes('--help')) {
|
|
63
|
+
printUsage();
|
|
64
|
+
process.exit(0);
|
|
65
|
+
}
|
|
66
|
+
const namespace = args[0];
|
|
67
|
+
let region = 'us-east-1';
|
|
68
|
+
let format = 'json';
|
|
69
|
+
for (let i = 1; i < args.length; i++) {
|
|
70
|
+
if (args[i] === '--region' && args[i + 1]) {
|
|
71
|
+
region = args[++i];
|
|
72
|
+
}
|
|
73
|
+
else if (args[i] === '--format' && args[i + 1]) {
|
|
74
|
+
const f = args[++i];
|
|
75
|
+
if (f === 'json' || f === 'env' || f === 'export') {
|
|
76
|
+
format = f;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
console.error(`Invalid format: ${f}. Use json, env, or export.`);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return { namespace, region, format };
|
|
85
|
+
}
|
|
86
|
+
const { namespace, region, format } = parseArgs(process.argv.slice(2));
|
|
87
|
+
loadConfig(namespace, region, format).catch((err) => {
|
|
88
|
+
console.error('Failed to load config:', err);
|
|
89
|
+
process.exit(1);
|
|
90
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ssm-to-env-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CLI tool that loads AWS SSM Parameter Store values and outputs them as JSON, .env, or shell exports",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ssm-to-env": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["aws", "ssm", "parameter-store", "config", "env", "cli"],
|
|
14
|
+
"author": "amit.zigelman",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"aws-parameters-store": "^1.0.11"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "^26.0.1",
|
|
25
|
+
"typescript": "^6.0.3"
|
|
26
|
+
}
|
|
27
|
+
}
|