Create a Custom AWS CDK Construct

Tomasz Łakomy
InstructorTomasz Łakomy

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 months ago

Now that we know a lot about shipping cloud resources with CDK it's time to start creating a serverless backend for our todo application.

We could add the database and new AWS Lambda functions to our main stack but after a while it might get difficult to maintain.

Instead, in this lesson we're going learn how to create our very own custom CDK construct.

Learn more about CDK constructs here.

Tomasz Łakomy: [0:00] Now that we know a lot about using cloud resources with CDK, it is time to start creating a serverless backend for our todo application.

[0:08] There are two ways we could approach this. We could either continue adding more and more resources to this stack, or we can create a separate construct which is going to house all of the resources necessary to create our todo backend. This is exactly what we are going to do.

[0:22] To recap quickly, constructs are the basic building blocks of AWS CDK apps. They represent a cloud component which can either be a single AWS resource, such as Amazon S3 bucket, or it can also be a higher-level abstraction consisting of multiple related AWS resources.

[0:40] This is exactly what we need to keep all of the resources necessary for our serverless backend in a single place as a single construct.

[0:47] Let me go back to our stack. In the lib directory, I'm going to create a new file, which I'm going to call todo-backend.ts. I'm going to start by adding some imports.

[0:58] First of all, we need to import the CDK itself from aws-cdk-lib, and we also need to import the construct package from the constructs module. Both of those packages were installed when you created our CDK project, and you can find both of them in the dependencies in our package.json.

[1:14] Going back to my file, I'm going to export a class. I'm going to call it a TodoBackend, and this is going to extend a Construct. I'm going to take the liberty of pasting the constructor itself.

[1:25] We can see that our custom construct takes exactly the same three arguments as every other construct that we have been using before. It takes a scope, id, and props. As such, we can see that we are going to use our custom construct in exactly the same way as we are using the construct provided to us by the CDK team.

[1:42] Now we can go ahead and import the TodoBackend. Let me scroll down over here, and I'm going to create a new TodoBackend, which is going to be an instance of the TodoBackend.

[1:50] The first argument is going to be this, the second argument TodoBackend, and the third argument are the props, which I'm going to leave as an empty object because our construct does not actually take any props right now.

[2:00] With our new empty construct in place, let's see what's going to happen if I were to deploy this stack. I'm going to open up the terminal and run cdk div, and we can see that there would be no changes because our todo backend stack is currently still empty.