'The difference between a Stack and Construct in AWS CDK
I'm new to CDK and confused about the difference between a Construct and a Stack. With CDK, we can define reusable cloud components known as Construct, and we can further compose these together into a Stack or Apps. See the diagram from AWS website below,
However, I've seen class where a construct is created by extending the Construct base class, and also class where a Stack is created by extending the Stack base class. Both child classes can be used later to create the main stack. For example, see the code below, I can create a Construct or a Stack called HitCounter class that creates the same set of resources and use them the same way in the main Stack. So what's the difference between using a Stack or a Construct?
import * as cdk from '@aws-cdk/core';
export class HitCounterConstruct extends cdk.Construct {} // imagine this construct creates a bunch of related resources
export class HitCounterStack extends cdk.Stack {} // imagine this stack creates the same resources as the construct class above
// In main stack file App.ts
new HitCounterConstruct(cdk.App, "construct");
new HitCounterStack(cdk.App, "stack");
please correct me if I made any mistake in code. Thanks in advance :)
Solution 1:[1]
Stack represents CF template in CDK terms. While Construct represents AWS resources which you want to create, like Lambda function, S3 bucket, Api gateway, etc.
Or if you want, Stack is your text file, when we write CF template, in yaml or json, Constructs are resources defined in this file.
In your case, if you try to deploy HitCounterStack
without any Constructs, it will be an empty cf template with no resources.
To render CF template by CDK, need follow this structure:
Stack -> Constructs, like file->resources, but on top of it should be also defined App
which also extends base class Construct
, so final right schema for CDK code should be:
App -> Stack -> Constructs.
Solution 2:[2]
Stack is a CloudFormation concept. It’s deployed as a unit.
Construct is a CDK OOP class that just holds similar things together for separation of concerns and has no effect on CloudFormation result.
Solution 3:[3]
According to the AWS CDK API Reference:
A
Stack
is the smallest physical unit of deployment, and maps directly onto a CloudFormation Stack.
When your application grows, you may decide that it makes more sense to split it out across multiple
Stack
classes. This can happen for a number of reasons.
As soon as your conceptual application starts to encompass multiple stacks, it is convenient to wrap them in another construct that represents your logical application. You can then treat that new unit the same way you used to be able to treat a single stack: by instantiating it multiple times for different instances of your application.
You can define a custom subclass of
Construct
, holding one or moreStack
s, to represent a single logical instance of your application.
As a final note:
Stacks
are not a unit of reuse. They describe physical deployment layouts, and as such are best left to application builders to organize their deployments with. If you want to vend a reusable construct, define it as a subclasses ofConstruct
: the consumers of your construct will decide where to place it in their own stacks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | moltar |
Solution 3 | Kris Dover |