cdkoutputs2sh
v0.0.2
Published
CLI tool and library that converts AWS CDK stack output JSON files into sourceable shell environment variable export scripts
Maintainers
Readme
cdkoutputs2sh
English | 日本語
cdkoutputs2sh is a CLI tool and library that converts AWS CDK stack output JSON files (generated by cdk deploy --outputs-file var/outputs.json) into sourceable shell environment variable export scripts (var/outputs.sh).
Features
- Automatic conversion from CDK output (JSON) to
export VAR=...format - Preserves mapping between variable names and original
StackName.OutputKeyin comments - Safe shell quoting with single quotes
- Warns and skips non-scalar values (arrays/objects)
- Detects variable name collisions and exits with error
Installation
npm install -D cdkoutputs2shUsage
- Deploy with CDK and generate JSON output
cdk deploy --outputs-file var/outputs.json - Convert to shell script
cdkoutputs2sh --input var/outputs.json --output var/outputs.sh - Source the generated script
source var/outputs.sh echo "$AWSCDKP0STACK_INSTANCEID"
Options
| Option | Alias | Description | Default | | ----------------- | -------- | -------------------------------- | ---------------- | | --input | -i | Input JSON file | var/outputs.json | | --output | -o | Output shell script | var/outputs.sh | | --fail-on-missing | none | Fail if input file doesn't exist | false | | --verbose | -v / -vv | Log level (INFO / DEBUG) | none | | --help | -h | Show help | - |
Variable Name Generation Rules
- Combine StackName_OutputKey and convert to uppercase
- Replace non-alphanumeric characters with _ and merge consecutive _ into one
- Remove leading/trailing _
- Add V_ prefix if starts with a number
- Add CDK_ prefix if stack name starts with Cdk (for sample compatibility)
- Error on collision
Examples:
- AwsCdkP0Stack.InstanceId → AWSCDKP0STACK_INSTANCEID
- CdkLambdaUrls1Stack.Lambda1FunctionUrl → CDK_CDKLAMBDAURLS1STACK_LAMBDA1FUNCTIONURL
Limitations and Notes
- Array/object values are skipped
- Change CDK Output names if collision occurs
- Output file permissions are set to 0644
Node.js API
import { convertCdkOutputs } from "cdkoutputs2sh";
const { exportBlock, mapping } = convertCdkOutputs({
input: "var/outputs.json",
output: "var/outputs.sh",
verbose: 1,
});
console.log(exportBlock);
console.log(mapping["AWSCDKP0STACK_INSTANCEID"]);Development
After cloning the repository:
npm run init # Not `npm init`. See run-scripts
npm run smoketest # Shows help message
npm test # Vitest test casesLicense
MIT License
