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: Conditional Stack Evaluation Based on Context Variables #31898

Closed
2 tasks
HeavyPablo opened this issue Oct 25, 2024 · 6 comments
Closed
2 tasks

core: Conditional Stack Evaluation Based on Context Variables #31898

HeavyPablo opened this issue Oct 25, 2024 · 6 comments
Assignees
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved.

Comments

@HeavyPablo
Copy link

Describe the feature

I'm proposing a feature that allows selective evaluation and synthesis of stacks based on provided context variables, making it easier to deploy individual stacks in a multi-stack environment without the need to evaluate or synthesize the entire CDK app.

This feature would give developers the option to conditionally process only the stack they intend to deploy, avoiding errors related to missing context values required by other stacks.

Use Case

As a developer, I'm often frustrated when deploying specific stacks in a multi-stack application. Even if I only need to deploy Stack1, CDK still evaluates Stack2, causing failures if Stack2 requires context values that are not relevant to the current deployment. For example:

  • Stack1 uses a context variable zoneName, but Stack2 uses certificate.
  • When I run cdk deploy Stack1 -c zoneName=myValue, the deployment fails because Stack2 requires certificate, even though I don't want to deploy or evaluate Stack2 in this step.

This creates an inefficient and frustrating workflow where developers need to pass unnecessary dummy values for context or handle extra logic in code to avoid these issues.

The new feature would prevent stacks from being evaluated at all unless they are explicitly targeted for deployment.

Proposed Solution

A possible solution could be to introduce a new CLI flag, such as --targeted-eval or --single-stack-mode, which would prevent the evaluation of any stacks not specified in the command.

Here’s a possible workflow:

  • The developer runs cdk deploy Stack1 --targeted-eval -c zoneName=myValue.
  • CDK will only evaluate and synthesize Stack1, skipping Stack2 entirely.
  • This would eliminate the need to pass dummy context values for unused stacks during deployment.

Alternatively, this feature could automatically detect missing context values and only evaluate stacks that have the context provided, offering a smoother user experience.

Example:

cdk deploy Stack1 --targeted-eval -c zoneName=myZone
# No need to pass dummy values for other stacks

Other Information

  • Consideration could also be given to environments where different stacks are deployed across different teams. The current workaround involves either adding manual context handling in code or passing all necessary context variables for every stack, even when deploying just one.

Additional features like warnings about "context value not provided" (without failing the entire deploy) would further improve this workflow.

Acknowledgements

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

CDK version used

2.164.0

Environment details (OS name and version, etc.)

Ubuntu 22.04.5 LTS

@HeavyPablo HeavyPablo added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Oct 25, 2024
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Oct 25, 2024
@nicholaschiasson
Copy link
Contributor

Hi @HeavyPablo , I'm not 100% sure if this solves your problem, as I haven't tested it specifically for your use case, but there does exist an option --exclusively of the cdk deploy command to deploy only the stack(s) you name without deploying other stacks in the app, even if the target stack depends on those or there are code changes which would result in deploying those.

Example usage:

cdk deploy --exclusively Stack1 -c zoneName=myZone
# Note the flag position is not important but I like to put it before the stack name just for readability

I have used this from time to time, and I think it should work for what you want to do, but like I said I haven't tested it, so I don't know if it will result in true selective synthesis. Perhaps it will still attempt to synthesize all stacks for the app and break due to the missing context arguments. But try it out!

You can see the documentation for it here: https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd-deploy.html#ref-cli-cmd-deploy-options-exclusively

@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Oct 28, 2024
@khushail
Copy link
Contributor

@HeavyPablo , Thanks for reaching out.
@nicholaschiasson , thanks for your suggestion and providing a workaround.

We have had similar request created by users in the past and the corresponding solutions has been provided , from time to time. I am sharing few issues with the suggested solutions -

  1. How to only build one stack in a multistack project? #11625 , solution with using --exclusively flag -How to only build one stack in a multistack project? #11625 (comment)
  2. this is an open issue (core): cdk synth always synthesizes every stack #6743 but the going through comments might be helpful in getting a broader picture of synth process when stacks are dependent.
  3. Another solution can be implemented using tryGetContext and retrieving this value and then adding condition for stack synthesis or deployment.e..g this issue - core: 'cdk synth' always synthesizes every stack #28136 , suggsted solution with same approach - comment
// bin/cdk.ts
if (process.env.ENV=='prod') {
  new SomeStack(app, "ProdStack");
} else {
  new SomeStack(app, "DevStack");
}

Reference on context variable - https://docs.aws.amazon.com/cdk/v2/guide/get_context_var.html

let me know if this is helpful.

@khushail khushail added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Oct 28, 2024
@khushail khushail self-assigned this Oct 28, 2024
@HeavyPablo
Copy link
Author

@nicholaschiasson and @khushail Thanks! The context key aws:cdk:bundling-stacks ( #11625 (comment) ) helps with the issue I was facing. I've tried the --exclusively flag, but it didn't resolve the problem since it's more about separating the synthesis process rather than handling stack dependencies.

Until now, I was managing this by adding another context key, for example: cdk deploy Stack1 -c targetStack=Stack1, to avoid a full synthesis. This new approach will help reduce redundancy in the deployment process.

@khushail
Copy link
Contributor

@HeavyPablo that's good to know. Should I close this one now ?

@HeavyPablo
Copy link
Author

@khushail If you can close the issue, that would be great. Thanks again!

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Oct 29, 2024
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

3 participants