-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Nested stack support #239
Comments
@eladb status? I need nested stack support before I can start using this cdk really. |
This should not be too hard to support through assets. |
A bit more context/direction/pointers/ideas: As a user, I'd like to be able to be able to define a nested stack like any other stack in the CDK: class SmallStack extends Stack {
// ...
}
class BigStack extends Stack {
constructor(p: Construct, id: string) {
new SmallStack(this, 'NestedStack');
}
}
const app = new App();
new BigStack(app, 'my-awesome-stack');
app.run(); An instance of This technically means that:
Assets: the protocol between the CDK and the toolkit (defined under the cx-api module) allows CDK apps to emit metadata entries that instruct the toolkit to package and deploy various types of assets (s3 files, s3 directories, docker images) and then reference them through CloudFormation parameters when the stack is deployed, which is effectively what we need here. |
This has implications on how Minor:
How to distinguish the behavior of a
|
Good points. All the cross reference aspects should probably be designed in conjunction with @rix0rrr's work on #1324. Rico proposes to eliminate the need for explicit import/export, which might serve to identify which type of reference this is: between two root stacks, between two nested stacks or between a nested stack and it's parent. The All the issues you bring up are relevant and important. However, try to design this in layers. How can we provide support for nested stack with minimal magic and maximum control for the user (can we provide only runtime errors if users attempt to cross the boundaries in the wrong way?). Then, we can see how we can sprinkle magic on top... Meta: would probably be easier to discuss this design via PR under |
Keep in mind that users would expect |
@tyron yeah it sure isn't, and not only that it basically always returns changes even when nothing has changed. as a workaround I created some utilities here https://github.com/node-cfn it implements a poor-mans I would love to switch that plugin to use the cdk under the hood instead if it could support nested stacks. I don't know if any of my work there could be useful. Approach wise, it topologically sorts the root stack and then runs a diff on each nested stack, and marks changes if it detects a change in things like the template URL (which is always hashed), or any properties in actual templates themselves, and then any thing that depends on a thing that changed gets marked as changed too (so it can possibly err on the side of falsely marking something as changed, but then the visual diff will reveal that nothing really changed). But it avoids false positives when there actually are no changes. it doesn't currently support imports or nested stacks within nested stacks, though those could be added. |
Hi everyone, Is there an update on the status of this issue? I'm using CDK for a rather complex set of stacks, which will probably go over the resource limit of CloudFormation, so I'm wondering if I need to foresee issues or if there is already an out-of-the-box solution from CDK. |
@bverhoeve i am working on official support for nested stacks in #2821 but I am not even sure you will need this. You can define any number of stacks in the CDK and freely reference resources between them. The cdk will automatically synthesis exports and imports (as long as they are in the same environment). This is mostly the same as nested stacks (some would say superior since there could be more complex relationships). |
@eladb thank you for the update, this sounds like great news! So if I get this correctly, this would mean that I can define multiple |
Yes, that's already supported (docs) |
I think nested stacks could be useful for our use case:
I figure that if we do this with nested stacks then those stacks will manage the creation or new alarms and deletion of the old ones in an automated way. |
@eladb FWIW the resource_stack link you gave is great, but it took me quite awhile to find it (I nearly opened an issue asking if this capability [cross stack references] was even supported, b/c it really seems like it should be, but afaict none of the howtos pointed it out, and none of the readmes/etc. I'd found explicitly used that approach, and after combing through issues ended up here.) Would be great if this multiple stack how to, which is currently just "I'll make multiple versions of the same stack" had a little link at the bottom for "btw if you want to do cross-stack references, see [the link you just gave]". |
@eladb I see that the PR #2821 you gave mentioned earlier has been closed. Does it mean that the development has been put on hold? I think it would be incredibly useful for constructs such as DynamoDB Global Tables, which generate stacks and at the moment yield parameter errors when used inside a stack. |
@eladb Is there any update on the status for this? The PR has been re-opened which sounds like good news. Is it reasonable to expect nested stacks released within October or November? Despite the suggestion for using multiple top-level stacks, nested stacks are a completely different ball game within CloudFormation. The main problem with multiple top-level stacks is that if a stack fails to update, you do not get the waterfall rollback of all the stacks that have been changed. In some cases, this can lead to really big issues! |
Hey @kbessas, I hope I'll be able to follow up on this soon. Generally I'd like to land this for the exact reasons you described. |
I'm a bit confused now. Maybe my confusion will help someone... Currently (
and I tried to use a Construct as the main (ns-dev) that contains the sub-stacks. This doesn't seem to work either:
I guess "does not exist" means that the template it isn't uploaded to aws yet? So at the moment, multiple stacks can only be up at the app level, and any sub functionality inside those should be written with Construct. Is that about right? |
The `NestedStack` construct is a special kind of `Stack`. Any resource defined within its scope will be included in a separate template from the parent stack. The template for the nested stack is synthesized into the cloud assembly but not treated as a deployable unit but rather as a file asset. This will cause the CLI to upload it to S3 and wire it's coordinates to the parent stack so we can reference its S3 URL. To support references between the parent stack and the nested stack, we abstracted the concept of preparing cross references by inverting the control of `consumeReference` from the reference object itself to the `Stack` object. This allows us to override it at the `NestedStack` level (through `prepareCrossReference`) and mutate the token accordingly. When an outside resource is referenced within the nested stack, it is wired through a synthesized CloudFormation parameter. When a resource inside the nested stack is referenced from outside, it will be wired through a synthesized CloudFormation output. This works for arbitrarily deep nesting. When an asset is referenced within a nested stack, it will be added to the top-level stack and wired through the asset parameter reference (like any other reference). Fixes #239 Fixes #395 Related #3437 Related #1439 Related #3463
One year later it's here 💪 |
Well the issue is closed... we merged this feature yesterday and expect it to be released with the next release (up to two weeks). |
Hi, I'm trying to create a CloudFormation template which uses nested stacks. First of all I have to say that I don't use I've made some tests using NestedStack construct and CfnStack class and what I found out is:
I'm mostly sure that my template will have more than 200 CF resources so I need to use nested stacks, but the problems are:
(I have to insert the S3 Bucket url because as I mentioned before they will be stored in a specific location.) I've seen #239 (comment) but does not solve my problem. FYI I'm using @aws-cdk (v1.15.0) and Typescript |
It should be easy to define nested stacks in the CDK.
This requires a good story behind uploading synthesized templates to S3 and "plugging in" the URL of the nested stack into the parent stack (see #233).
We should have a story on how to reference resources from the parent child in the child stack (i.e. using nested stack "Parameters") and how to reference resources from the child stack in the parent stack (i.e. using "Outputs").
The text was updated successfully, but these errors were encountered: