Reputation: 479
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 :)
Upvotes: 27
Views: 15497
Reputation: 723
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.
Also need to know that Constructs
represented by 3 levels. 1-st level Construct
mapped to AWS resources one-to-one. One-to-one means Construct
represent single AWS resource. 1st level Construct
name starts with Cfn.
2nd level Constructs also maped 1-1 to CloudFormation resources but
higher-level abstraction through an intuitive intent-based API 1
And 3rd levels Constructs
more high level. So when you define 2nd level Construct in CDK template will be created several AWS resources, when you define 3rd level Construct – even more resources will be created.
This is for abstract routine procedures. Like create S3 bucket website, Lambda app and so on.
Upvotes: 26
Reputation: 654
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.
Upvotes: 3
Reputation: 1791
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.
Upvotes: 4