Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] know from within the script which action is executed #9619

Closed
2 tasks
robert-hanuschke opened this issue Aug 12, 2020 · 2 comments
Closed
2 tasks

[core] know from within the script which action is executed #9619

robert-hanuschke opened this issue Aug 12, 2020 · 2 comments
Assignees
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged.

Comments

@robert-hanuschke
Copy link

I would like to know inside my CDK script which action is being executed on it - be it synth, deploy, destroy etc.

Use Case

Reusable stacks

Defining reusable stacks and resources requires to provide differences as context variables. E.g. using a service name in a resource name is possible when having it defined as (in my case Python):

name=f"{self.node.try_get_context('service')}-example-resource"

Then I can successfully deploy different instances by specifying the variable on the command line as context:

cdk deploy my-reusable-stack --context service="my-service-A"

However, now I also need to specify the context variable even for every synth to not get an error. From within the script, for that case I'd like to check if this is a synth action and set the service variable to a dummy value.

Context sensitive actions

For example, I'd like to execute additional actions if my "stage" context variable is "prod", but only if the action is "deploy" or "destroy", e.g. update an asset inventory that does not include non-prod resources or any other action dependent on a combination of those contexts.

Proposed Solution

Something like a "get_cdk_action" function, to be used like this for the use cases mentioned above:

if self.node.get_cdk_action() == "synth":
    active_service = "Dummy"
if self.node.get_cdk_action() == "deploy" and self.node.try_get_context('stage') == "prod":
    update_inventory()

Other

I realize those could be achieved by wrapper scripts but I really don't see why I should do that and have to take care of handling exit codes and outputs of the external cdk command if I can do basically everything in app.py already - just limited by not knowing the full execution context.

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@robert-hanuschke robert-hanuschke added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 12, 2020
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Aug 12, 2020
@jogold
Copy link
Contributor

jogold commented Aug 12, 2020

Related to #9540

@eladb
Copy link
Contributor

eladb commented Aug 17, 2020

It's not considered a healthy practice to change the behavior of a CDK app based on the CLI operation. CDK apps are expected to simply synthesize the output. The the resulting "cloud assembly" can later be interacted with using the CLI or in CI/CD systems.

If you wish to pass context to your CDK app based on some external state, although not recommended, you can either use simply environment variables or --context. Either way, I don't think this is something we will likely support.

#9540 is related and we may introduce some information about the operation context just as an optimization.

@eladb eladb closed this as completed Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

3 participants