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.
Tutorial goals
We will:
- Create and AWS CDK project in TypeScript
- Write a simple ‘Hello world’ Lambda function
- Deploy the lambda function
- Test it
- Clean up ressources to avoid unnecessary costs
Prerequistes
Before getting started, make sure you have:
- Node.js installed
- AWS CLI configured :
aws configure- AWS CDK installed globally:
npm install -g aws-cdk
- An active AWS account
1. Initialize the SDK project
Create a new directory for your project:
mkdir cdk-lambda-demo
cd cdk-lambda-demoInitialize a TypeScript CDK application:
cdk init app --language typescriptCDK generates the following structure:
/bin --------------> application entry
/lib --------------> CDK stack definitions
cdk.json -------------> CDK configuration
tsconfig.json
package.json2. Create the lambda function
Create a /lambda/ folder and a hello.ts file:
mkdir lambda
touch lambda/hello.tsAdd the following code to hello.ts
//lambda/hello.ts
export const handler = async()=> {
return {
statusCode: 200,
body: JSON.stringify({message: "hello world from aws cdk "});
}
};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:
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
});
}
}Explanation:
- runtime -> execution environment (Node.js 18 by default)
- code -> dpath to the lambda source file
- handler -> exported handler function
- functionName -> lambda name used when invoking it via aws cli
Warning
Run the following command npm install -D @types/aws-lambda esbuild
@types/aws-lambda provide TypeScript typings
esbuild is used to transpile TypeScript to javaScript
Whithout esbuild, CDK will use a Docker image for bundling, which slows down deployment.
4. Deploy the Lambda function
Before deploying install dependencies and boostrap CDK:
npm install
cdk bootstrap
cdk synth # generates the cloudFormation template
cdk deploy # deploys the stack5. Test the Lambda function
Using the AWS cli:
aws labda invoke\
-- function-name HelloHandler \
response.jsonThen display the response:
cat response.jsonFrom aws console:
6. Clean up resources
cdk destroyThis removes The Lambda function and all related CDK resources.
Conclusion
In this article, we:
- Created and AWS CDK project using TypeScript
- Wrote our first Lambda function
- Deployed a CDK stack
- Tested the Lambda function
- Source Code: gitlab
This is an important first step into the serverless world.
A natural next step would be to add Amazon API Gateway as a trigger for our Lambda function.
Comments