Skip to main content

Serverless TypeScript Functions with AWS CDK

Sebastianยท

Introduction

When deploying serverless functions using TypeScript on AWS, the AWS Cloud Development Kit offers an infrastructure as code solution while also living side by side with your business logic.

What is AWS CDK?

The AWS-CDK is an open-source library maintained by AWS that enables developers to construct AWS resources using constructs. It translates infrastructure code into CloudFormation templates, building abstractions on top of the underlying resources rather than providing one-to-one implementations.

Infrastructure as Code with AWS-CDK

This guide demonstrates creating a Lambda function exposed via a function URL, making it callable over the internet. The business logic is written in TypeScript.

Prerequisites

  • AWS access configured via aws-cli
  • Node.js installed
  • CDK bootstrap: npx cdk bootstrap

Project Setup

Initialize a CDK Project

mkdir cdk-example
cd cdk-example
npx cdk init app --language=typescript

This creates bin and lib folders. The bin folder specifies deployment instructions, while the lib folder contains stack definitions.

Bin Folder Structure

#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { CdkExampleStack } from '../lib/cdk-example-stack';

const app = new cdk.App();
new CdkExampleStack(app, 'CdkExampleStack', {});

Every CDK project outputs a CloudFormation stack that deploys to your AWS account.

Using NodejsFunction Construct

The NodejsFunction is a level three construct that handles TypeScript transpilation automatically during deployment. No manual bundling or asset uploads are required.

Initial Stack Configuration

import * as cdk from 'aws-cdk-lib';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Construct } from 'constructs';

export class CdkExampleStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const exampleFn = new NodejsFunction(this, 'example-function', {
      functionName: 'example-function',
      entry: './src/index.ts'
    })
  }
}

Create Source Directory

mkdir src
touch src/index.ts

Writing Business Logic

import { LambdaFunctionURLEvent } from 'aws-lambda';

export const handler = async (request: LambdaFunctionURLEvent) => {
  console.log("Routekey", request.routeKey)
  return { message: "Hello World" };
}

Adding Function URL

Function URLs expose Lambda functions to the public internet via a randomly assigned AWS URL. Authentication can be configured but is not required here.

import * as cdk from 'aws-cdk-lib';
import { FunctionUrlAuthType } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Construct } from 'constructs';

export class CdkExampleStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const exampleFn = new NodejsFunction(this, 'example-function', {
      functionName: 'example-function',
      entry: './src/index.ts'
    })
    exampleFn.addFunctionUrl({
      authType: FunctionUrlAuthType.NONE
    })
  }
}

CloudFormation Output

Capture the function URL and output it after successful deployment:

import * as cdk from 'aws-cdk-lib';
import { FunctionUrlAuthType } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Construct } from 'constructs';

export class CdkExampleStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const exampleFn = new NodejsFunction(this, 'example-function', {
      functionName: 'example-function',
      entry: './src/index.ts'
    })
    const functionUrl = exampleFn.addFunctionUrl({
      authType: FunctionUrlAuthType.NONE
    })
    new cdk.CfnOutput(this, 'function-url', {
      value: functionUrl.url
    })
  }
}

Deployment

Execute deployment with:

npx cdk deploy

The CLI displays the function URL after successful deployment. Open it to verify the "Hello World" response.

Cleanup

Remove all resources when done:

npx cdk destroy