Skip to content
This repository has been archived by the owner on Jul 11, 2020. It is now read-only.

Latest commit

 

History

History
34 lines (22 loc) · 1.26 KB

15-create-a-custom-aws-cdk-construct.md

File metadata and controls

34 lines (22 loc) · 1.26 KB

Create a custom AWS CDK construct

📹 Video

Let's start creating a serverless backend for our todo application.

We're going to create a custom construct, where we are going to put our database and a lambda function.

Custom Construct Illustration

Create a new file next to our stack file (in the lib directory), called todo-backend-ts.

Import aws-cdk/core then, let's type our custom construct (which is going to look a lot like the logoBucket code from our stack file).

export class TodoBackend extends cdk.Construct {
    // so we can export it later
    public readonly handler: lambda.Function;

    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id);
    }
}

Then import the construct into our main stack app and create an instance of it:

import { TodoBackend } from "./todo-backend";

const todoBackend = new TodoBackend(this, "TodoBackend");

Let's cleanup the file a bit: delete logoBucket, the s3Notifications import, lastly, swap helloLambda for todoBackend.handler.