In AWS CDK how can I tell, in app.py, if its a deploy or destroy?

Viewed 576

In AWS CDK how can I tell, in app.py, if its a deploy or destroy?

I'm wanting to set a variable with the lifecycle action, whether its deploy, destroy, synth, etc, when a CDK stack is deployed.

Ex.

cdk deploy --all

And then use that variable in my code.

It does not appear app.py can get an argv variable, and this doesn't seem to be part of the environment variables.

Does anyone know how this might be accomplished?

2 Answers

After playing around in the debugger I cannot see a way to identify this.

Could you perhaps use a parameter to get this?

https://docs.aws.amazon.com/cdk/latest/guide/parameters.html

cdk deploy YourStack --parameters cmd_type=deploy

I know it's not perfect because it can be skipped or missed but perhaps it would help in a pipeline since that's fixed?

Perhaps you can explain more about what you are trying to achieve and why you need those details?

You can use context values also, say:

npx --no-install cdk destroy -c destroy=true --require-approval=never 

And then retrieve it in the app

is_destroy = app.node.try_get_context("destroy")

A use case for this need for me was, when deploying a lambda package via CDK, I did not had handle to the the package name in the un-deployment though I need it always in the deployment step

Related