Hello World Serverless : Deploy an AWS Lambda Function with CDK and TypeScript

Serverless has become one of the most popular approaches for building fast, scalable and cost-effective applications. With AWS Lambda you only focus on writing code and AWS takes care of everything else: scaling, servers and availability.

But there is a tool taht makes the experiences even better : AWS CDK. It allows you to define your infrastructure using familiar programming languages such as TypeScript, Java, Python,C# or Go, instead of writing raw of CloudFormation template.

In this article, I’ll show you how to deploy your very first **AWS lambda function ** in just a feew minutes using AWS CDK and TypeScript. No complex architecture, no heavy prerequistes but just a simple and solid first step into the serverless world.

Prerequistes

Before getting started, make sure you have:

BASH
aws configure
Click to expand and view more
BASH
npm install -g aws-cdk
Click to expand and view more

installed version result

1. Initialize the SDK project

Create a new directory for your project:

BASH
mkdir cdk-lambda-demo
cd cdk-lambda-demo
Click to expand and view more

Initialize a TypeScript CDK application:

BASH
cdk init app --language typescript
Click to expand and view more

CDK generates the following structure:

PGSQL
/bin    --------------> application entry
/lib    --------------> CDK stack definitions
cdk.json -------------> CDK configuration
tsconfig.json
package.json
Click to expand and view more

2. Create the lambda function

Create a /lambda/ folder and a hello.ts file:

BASH
mkdir lambda
touch lambda/hello.ts
Click to expand and view more

Add the following code to hello.ts

TYPESCRIPT
//lambda/hello.ts

export const handler = async()=> {
    return {
        statusCode: 200,
        body: JSON.stringify({message: "hello world from aws cdk "});
    }
};
Click to expand and view more

This Labda function simply return a JSON response with a message.

3. Declare the lambda function in CDK Stack

Open lib/cdk-lambda-demo-stack.ts and replace its content with:

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

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

     new lambdaNodejs.NodejsFunction(
      this,
      'HelloHandler', { 
      entry: 'lambda/hello.ts', // path to the Lambda function code
      handler: 'handler',
      functionName: 'HelloHandler' // name of the exported handler function
    });
  }
}
Click to expand and view more

Explanation:

4. Deploy the Lambda function

Before deploying install dependencies and boostrap CDK:

SH
npm install  
cdk bootstrap
cdk synth   # generates the cloudFormation template

cdk deploy # deploys the stack
Click to expand and view more

5. Test the Lambda function

Using the AWS cli:

SH
aws labda invoke\
-- function-name HelloHandler \
response.json
Click to expand and view more

Then display the response:

SH
cat response.json
Click to expand and view more

From aws console:

result from aws console

6. Clean up resources

SH
cdk destroy
Click to expand and view more

This removes The Lambda function and all related CDK resources.

Comments

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut