1. 16
    Create a custom AWS CDK construct
    1m 49s
⚠️ This lesson is retired and might contain outdated information.

Create a custom AWS CDK construct

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

Social Share Links

Send Tweet
Published 5 years ago
Updated a year 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 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

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

[0:06] The thing is that our stack is kind of large right now, and if we keep on adding resources to this stack is going to be difficult to maintain. Instead, we're going to create a custom construct where we're going to put the database and Lambda functions for our serverless backend.

[0:19] To recap quickly, constructs are the basic building blocks of AWS CDK apps, and they can either represent a single resource, such as an S3 bucket, or they can represent a higher-level component consisting of multiple CDK resources. This is exactly what we need in order to keep all the resources necessary for our serverless backend in a single place, as a single construct.

[0:38] We're going to go back to our account, and in the lib directory next to our stack we're going to create a new file, which I'm going to call todo-backend.ts. Inside of it, I'm going to import * as cdk from '@aws-cdk/core'. Next up, I'm going to export a class, and this class is going to be called todo-backend. This is going to extend an CDK construct, and I'm going to paste in the constructor of this class.

[1:01] As all construct that we used before is going to take the scope as an argument and id and also some optional props. We can see that we are going to use this construct exactly the same way as we are using this S3 bucket for instance. It's going to take the scope and id and also some optional props.

[1:17] In order to create an instance of this construct, we're going to import todo-backend from 'todo-backend'. Next up, I'm going to create a new instance over here. I'm going to do const todo-backend = new-todo-backend. I'm going to pass in this and also an id of todo-backend. That is going to create a new instance of our todo-backend construct.

[1:38] Let's see what's going to happen if we deploy it. Let's open up the terminal and run cdk diff. Nothing new is going to be deployed because our new-todo-backend construct is not going to provision any resources in AWS cloud.

Mannuel Ferreira
Mannuel Ferreira
~ 2 years ago

For CDK version 2, the custom construct can look like this:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class TodoBackend extends Construct {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id);
  }
}
Markdown supported.
Become a member to join the discussionEnroll Today