1

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?

Dave
  • 332
  • 1
  • 7

1 Answers1

2

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"
miensol
  • 39,733
  • 7
  • 116
  • 112
  • This matches my understanding as well, unfortunately. As a work around in my particular case, I'll use the AWS cli to destroy the stack(s) instead of the CDK to avoid re-invoking the context validation logic I'm trying to avoid at destroy time. – Dave Dec 04 '21 at 14:59