- Newest
- Most votes
- Most comments
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:
-
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.
-
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.
-
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:
-
Increased coupling: It can create tighter coupling between stacks, which might make independent deployment more challenging.
-
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.
-
Potential circular dependencies: If not managed carefully, you could create circular dependencies between stacks, which CDK cannot resolve.
-
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
Relevant content
- asked 3 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 5 months ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 18 days ago