Keep bragging

Notes on technologies, coding, and algorithms

CDK for Lambda

In this example, we will add:

Create an CDK app

mkdir MyWidgetService
cd MyWidgetService
cdk init --language typescript

There are 2 files, one to attach the main stack to the app, the other to load stack:

Add Lambda

mkdir resources
vi resources/widgets.ts

The code can be find widgets.ts.

Add widget service

npm install @aws-cdk/aws-apigateway @aws-cdk/aws-lambda @aws-cdk/aws-s3

The code can be find widget_service.ts.

More details on different options to set code path:

import * as cdk from '@aws-cdk/core';
import * as widget_service from '../lib/widget_service';

export class MyWidgetServiceStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // The code that defines widgets stack goes here
    new widget_service.WidgetService(this, 'Widgets');
  }
}

Bootstrap and deploy

Before you can deploy your first AWS CDK app containing a lambda function, you must bootstrap your AWS environment. This creates a staging bucket that the AWS CDK uses to deploy stacks containing assets.

cdk bootstrap
cdk deploy
# cdk destroy

We can test all methods with curl:

curl -X GET 'https://GUID.execute-api-REGION.amazonaws.com/prod'
curl -X POST 'https://GUID.execute-api-REGION.amazonaws.com/prod/example'
curl -X GET 'https://GUID.execute-api-REGION.amazonaws.com/prod'
curl -X GET 'https://GUID.execute-api-REGION.amazonaws.com/prod/example'
curl -X DELETE 'https://GUID.execute-api-REGION.amazonaws.com/prod/example'
curl -X GET 'https://GUID.execute-api-REGION.amazonaws.com/prod'