What is the best practice how to pass parameters between stacks in CDK?

0

If I want to reference information from one stack to another in CDK, how can I best pass parameters between stacks?

The basic premise is that I understood that CDK best practice is not to split the stack without a fight. On the other hand, we believe there are situations where you may want to split the stack.

https://docs.aws.amazon.com/cdk/v2/guide/best-practices.html#best-practices-apps

Separate your application into multiple stacks as dictated by deployment requirements

We recognize that there are multiple possible ways to pass parameters between stacks. For example, cross-stack references with Props passing, and references via the parameter store.

https://docs.aws.amazon.com/cdk/v2/guide/best-practices.html#best-practices-apps

Use generated resource names, not physical names

...Alternatively, you can generate a configuration file on your Amazon EC2 instance on startup, or write the actual table name to the AWS Systems Manager Parameter Store so your application can read it from there. ...If the two stacks are in the same AWS CDK app, pass a reference between the two stacks.... ... When the two stacks are in different AWS CDK apps, use a static from method to use an externally defined resource based on its ARN, name, or other attributes....

If we look at the code alone, I think the cross-stack reference to Props is better because the dependencies are easier to understand, but is there any disadvantage to this method?

profile picture
asked 4 months ago321 views
1 Answer
0

When it comes to passing parameters between stacks in CDK, there are several approaches to consider, each with its own advantages and use cases.

The best practice generally depends on your specific scenario, but here are some recommended methods:

  1. Cross-stack references with Props passing: This is often the preferred method when stacks are part of the same CDK app. It allows for clear dependency management and type-safety. You can pass references as props when instantiating stacks, making the relationships between stacks explicit in your code. This approach is particularly useful for tightly coupled stacks within the same application.

  2. Using AWS Systems Manager Parameter Store: This method is beneficial when you need to share information between stacks that are not necessarily deployed at the same time or are in different CDK apps. It's also useful for storing configuration that might change over time without requiring a redeployment of your stacks.

  3. Using static 'from' methods: When working with resources that exist outside your CDK app or in different apps, you can use static 'from' methods to reference these resources based on their ARN, name, or other attributes.

While cross-stack references with Props passing is often preferred for its clarity and type-safety, it does have some potential drawbacks:

  1. Increased coupling: It can create tighter coupling between stacks, which might make independent deployment more challenging.

  2. Deployment order dependencies: You need to ensure that stacks are deployed in the correct order, as dependent stacks cannot be deployed before the stacks they depend on.

  3. Potential circular dependencies: If not managed carefully, you could create circular dependencies between stacks, which CDK cannot resolve.

  4. Limited to single CDK app: This method only works for stacks within the same CDK app.

In contrast, using Parameter Store can provide more flexibility, especially for cross-account or cross-region scenarios, but it may introduce additional complexity in terms of parameter management and retrieval.

The choice between these methods often comes down to your specific use case, deployment requirements, and how tightly coupled your stacks need to be. If your stacks are part of the same application and deployed together, cross-stack references with Props passing is often the cleaner and more maintainable approach. However, for looser coupling or cross-account/cross-region scenarios, using Parameter Store or other external configuration methods might be more appropriate.

Remember, while it's generally advisable to keep resources in a single stack when possible, there are valid reasons to split stacks, such as different lifecycle management, permissions boundaries, or deployment requirements. The key is to make these decisions based on your specific needs and to use the appropriate method for passing parameters that best fits your architecture.
Sources
Community | Three Tips for Writing CDK Tests
Community | Multi-region Deployments with CDK
Community | CDK Cloud Development Kit
AWS CDK – Resources

profile picture
answered 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions