Skip to content

Commit 7cd84a0

Browse files
authored
feat(cli): Allow specifying options using env vars (#1447)
Allows passing options to the `cdk` command using environment variables with the `CDK_` name prefix. This can be used, for example, for the following: - `CDK_VERSION_REPORTING=false` disables version reporting - `CDK_REQUIRE_APPROVAL=broadening` requires approval for changes to security policies that broaden the grants - `CDK_VERBOSE=true` enables verbose logging in the toolkit Added a validator in `cdk doctor` that will display the environment variables that may be picked up by the toolkit, and highlight those that are reserved for internal use by the toolkit (`CDK_CONTEXT_JSON` and `CDK_OUTDIR`).
1 parent 8c17ca7 commit 7cd84a0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/aws-cdk/bin/cdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const DEFAULT_TOOLKIT_STACK_NAME = 'CDKToolkit';
3030
async function parseCommandLineArguments() {
3131
const initTemplateLanuages = await availableInitLanguages;
3232
return yargs
33+
.env('CDK')
3334
.usage('Usage: cdk -a <cdk-app> COMMAND')
3435
.option('app', { type: 'string', alias: 'a', desc: 'REQUIRED: Command-line for executing your CDK app (e.g. "node bin/my-app.js")' })
3536
.option('context', { type: 'array', alias: 'c', desc: 'Add contextual string parameter.', nargs: 1, requiresArg: 'KEY=VALUE' })

packages/aws-cdk/lib/commands/doctor.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cxapi = require('@aws-cdk/cx-api');
12
import colors = require('colors/safe');
23
import process = require('process');
34
import yargs = require('yargs');
@@ -25,7 +26,8 @@ export async function realHandler(_options: CommandOptions): Promise<number> {
2526

2627
const verifications: Array<() => boolean | Promise<boolean>> = [
2728
displayVersionInformation,
28-
displayAwsEnvironmentVariables
29+
displayAwsEnvironmentVariables,
30+
displayCdkEnvironmentVariables,
2931
];
3032

3133
// ### Verifications ###
@@ -47,3 +49,22 @@ function displayAwsEnvironmentVariables() {
4749
}
4850
return true;
4951
}
52+
53+
function displayCdkEnvironmentVariables() {
54+
const keys = Object.keys(process.env).filter(s => s.startsWith('CDK_'));
55+
if (keys.length === 0) {
56+
print('ℹ️ No CDK environment variables');
57+
return true;
58+
}
59+
print('ℹ️ CDK environment variables:');
60+
let healthy = true;
61+
for (const key of keys.sort()) {
62+
if (key === cxapi.CONTEXT_ENV || key === cxapi.OUTDIR_ENV) {
63+
print(` - ${colors.red(key)} = ${colors.green(process.env[key]!)} (⚠️ reserved for use by the CDK toolkit)`);
64+
healthy = false;
65+
} else {
66+
print(` - ${colors.blue(key)} = ${colors.green(process.env[key]!)}`);
67+
}
68+
}
69+
return healthy;
70+
}

0 commit comments

Comments
 (0)