Create a Custom AWS CDK Construct

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 6 months ago
Updated 6 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.

egghead
egghead
~ 25 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today