6

I thought that this would be answered quite clearly out there on the internet. But I have found no sufficient answer to when to use one type of stacks and when to use another type of stacks.

  • So the main question is what is the difference between regular stacks and nested stacks?

I can perfectly model my infrastructure using only regular stacks. Also, I can perfectly model my infrastructure using only nested stacks and one root regular stack. From a project perspective - the only difference is stack type names. Everything else is the same.

For example, I am using AWS CDK - a synthesizer that can, for example, synthesize python to CloudFormation templates. I can do everything using Stacks and I can simply find-and-replace Stack to NestedStack. The infrastructure would be redeployed but nothing essentially has changed.

  • So why would I use nested stack over a regular stack? What are their own advantages?
Laimonas Sutkus
  • 3,247
  • 2
  • 26
  • 47
  • I'm not sure I understand this question. Nested stacks allow you to compose together stacks. I.e. you can refactor common infrastructure templates and use it in many other stacks. If you don't need that, don't use it – dpwr Dec 21 '20 at 20:02
  • 1
    Hey @dpwrussell. Thanks for the answer. That is the problem with "If you don't need that, don't use it.". I don't know if I don't need it. So you say nested stacks allow you to compose together stacks. But you can compose stacks with regular stacks too. – Laimonas Sutkus Dec 22 '20 at 07:42
  • No you can't, if you nest a stack in your template, then you are using nested stacks – dpwr Dec 22 '20 at 09:10

3 Answers3

8

The biggest "difference" with nested stacks is that they become part of the parent stack. This means that if anything fails to deploy all stacks (parent and nested) get rolled back as one. By contrast with multiple stacks you could have the first one succeed to deploy, the second fail and roll back and the third not even attempt a deployment.

Alex
  • 355
  • 5
  • 11
0

Nested stacks are like functions in your code, which bundle re-usable code and which you can parametrized.

Like programing in python, you can defined a method, which provides a functionality which you use across your entire application or different applications. Off course, you could just not used any methods in your python code, and just copy-and-paste the same code across multiple locations, and it would also work and be fine.

Just like functions in python, nested stacks allow you to reduce the amount of copy-and-paste across your various templates, as you can bundle common patterns into nested stacks. Just like functions, the nested stacks can be parametrized, so you can create resources from a single nested stack tailored to your needs.

why would I use nested stack over a regular stack?

To avoid copy-and-paste of the same functionality across multiple stacks. The answer is similar to "why I should use functions in python, if I can just copy-and-paste the same code everywhere when its needed?".

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 1
    Hi @Marcin. Thanks for the answer. But I want to challenge you by saying that you can bundle common patterns with regular stacks too. Regular stacks can also be parameterized. And you can reuse stacks with different parameters. Am I correct or am I missing some fundamentals? – Laimonas Sutkus Dec 22 '20 at 07:47
  • @LaimonasSutkus One common pattern is to have a bastion host in a public subnet, as a jump host to private subnets. For many VPCs in different stacks, definition of bastion host will be usually same along with an accompanying security group. So how would you define this bastion host and security group in multiple stacks? For many in this scenario, nested stacks are the choise because you don't have to copy-and-paste same definition of bastion host and security group. You just refer to one definition as nested stack. – Marcin Dec 22 '20 at 09:15
  • Parameterization is not the same thing as functions at all. Functions encapsulate logic. Yes you could reuse the same stack template several times with different parameters, but this doesn't help you when you come to need another template with some of tbe same functionality, but overall a totally different objective. – dpwr Dec 22 '20 at 09:20
  • @dpwrussell I never wrote that they are "the same", only that there are "like" functions, because CFN is not a programming language in traditional sense of it. If you have different objective, then you can create a different version of a nested stack template. But anyway, please feel free to provide your answer and I will upvote it. I'm happy to remove mine. – Marcin Dec 22 '20 at 09:28
  • 1
    @marcin I was responding to the OP's comment. Your answer is great. – dpwr Dec 22 '20 at 10:12
  • @dpwrussell Oh. thanks :-) I think its better to reference OP name in the comment, as otherwise I don't think he/she gets notified and also it can get confusing who the recipient of the comment should be like in this case:-) – Marcin Dec 22 '20 at 10:18
  • @marcin Agreed, an oversight on my part! – dpwr Dec 22 '20 at 10:20
  • Do Nested Stacks or Regular Stacks have any significant performance advantage? Any of them can be deployed faster? I am especially curious from AWS CDK perspective. – Laimonas Sutkus Dec 27 '20 at 19:42
0

So why would I use nested stack over a regular stack? What are their own advantages?<<

The main reason is if you have over 500 resources in a stack, you have no choice but to break it apart into nested stacks. This limit of 500 used to be 200 resources until a Oct 2020.

Tim Bassett
  • 1,325
  • 1
  • 12
  • 23