‫So Constructs in CDK are very important,
‫and we've seen them before.
‫They are components that encapsulates everything
‫CDK needs to create the final CloudFormation stack.
‫And so a Construct can be a single AWS resource,
‫such as an S3 bucket,
‫or it can be a combination of multiple related resources,
‫for example, an SQS worker queue with some computes.
‫So how do we get these Constructs?
‫Well, there is a Construct Library,
‫which is a collection of Constructs
‫that is included in the CDK
‫and that contains the Constructs for every AWS resource.
‫And you have three levels of Constructs:
‫Level 1, Level 2, and Level 3.
‫We'll see them in the next slides.
‫Also, you have the Construct Hub.
‫This is when you have Constructs from AWS
‫but also third parties and open-source CDK community
‫to allow you to create your CDK stacks faster and better.
‫So let's talk about the Layer 1 Constructs.
‫They're called CFN Resources
‫because they represent all the resources
‫available within CloudFormation.
‫So here, in this example, we do a bucket.
‫And it's new s3.CfnBucket.
‫And so we define the exact same properties
‫as the one from the CloudFormation Resource Specifications.
‫And you know this is Layer 1
‫because it starts with Cfn.
‫So a CFN bucket must have all the resource properties
‫that are required for a bucket.
‫So, bucket name.
‫And you can configure all of them individually.
‫So if you wanted to just migrate
‫from CloudFormation to CDK,
‫one by one, all your resources into code,
‫you could use CFN bucket, CFN SQS queue, and so on.
‫And that would be enough.
‫But that would be using Layer 1 of CDK
‫because this is quite basic.
‫Then you go into Layer 2.
‫So Layer 2 is AWS resources
‫but with a higher level.
‫Because now we talk about intent.
‫This is a higher level Construct.
‫So if we have a look at what's in bottom,
‫here we have an s3.Bucket.
‫So the difference is that it doesn't start with Cfn.
‫And then, as we can see, we have versioned: true.
‫And then encryption, s3.BucketEncryption.KMS.
‫And then, automatically, knows how to do this.
‫And then, out of this, from the bucket,
‫we can get, for example,
‫what is the HTTP URL of a specific object out of it.
‫So we have extra methods out of it.
‫So when you use a Layer 2 Construct,
‫you have similar functionality as Layer 1,
‫but now we have convenient defaults and boilerplates.
‫So you don't need to know
‫everything about the resource properties.
‫And then you have added methods,
‫such as bucket.addLifeCycleRule.
‫And this would allow you to just add the Lifecycle rule,
‫even though, behind the scenes,
‫it may be a bit more complicated in raw CloudFormation.
‫Then you have Layer 3 Constructs.
‫And these are called Patterns
‫because they represent multiple related resources.
‫So here what we have is a Lambda Rest API.
‫And then we add resources and add resources,
‫and do an HTTP Integration, and so on.
‫And so, this Layer 3 Patterns
‫will help you complete most common tasks in AWS,
‫such as, for example, creating a Rest API
‫with an API Gateway.
‫But instead of configuring
‫every single little resource behind the scene,
‫you have a much easier API to deal with,
‫where you just define the API,
‫and then you define the resources and the methods,
‫and the Lambda functions, but very easily.
‫You can also have, for example, an ECS pattern
‫where you have an Application Load Balancer
‫with a Fargate Service,
‫which can be extremely complicated to write
‫as raw CloudFormation.
‫But with CDK, you just fill in the blanks,
‫and automatically you have your ALB and your Fargate Service
‫connected on the correct ports
‫with the correct security groups, and so on.
‫And the correct listeners.
‫So, hopefully, you now understand the differences
‫between all the layers in CDK Constructs
‫and the power of CDK.
‫So I hope you liked it,
‫and I will see you in the next lecture.