Can a CDK application discover which command is being invoked?

Viewed 79

In my CDK application, I would like to use different logic for validating some context parameters during CDK destroy. Is there a way for the CDK application to determine which command is being invoked?

1 Answers

Unfortunately, there doesn't seem to be a good way to achieve that at the moment.

At the very least in the case of the TypeScript CDK application, there will be a child process spawned that renders the CDK object graph. However, that child process doesn't receive the original arguments you passed to CDK.

There's a way to get around that by accessing process.ppid that will give you parent process PID. Then, on Linux-based systems you can do readFileSync(`/proc/${process.ppid}/cmdline`) to access the parent process command line arguments.

However, that approach is very brittle.

If you truly need to vary your code based on the command executing I'd recommend setting an environment variable. e.g. in your package.json scripts section

"cdk:synth": "CDK_COMMAND=synth cdk synth"
Related