Getting started with AWS Serverless
Introduction
A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. A serverless application is more than just a Lambda function, it can include additional resources such as APIs, databases, and event source mappings.
- AWS Serverless Application Model (AWS SAM)
- AWS Cloud Development Kit (AWS CDK)
- The Serverless Framework (serverless.com)
- Chalice framework for Python apps (github.com/aws/chalice)
- Arc.code (arc.codes/docs)
- Claudia.js (claudiajs.com/)
AWS SAM
Developer Guide
CLI Reference
- Nest.js
A powerful framework for creating sever-side applications, learn more. - Serverless Framework
Easy to use framework for developing and deploying serverless apps, learn more. - Serverless Jetpack
A low-config plugin that packages our code to be deployed to AWS Lambda, learn more. - Serverless Express
A library that makes our "plain" NestJS API play nicely with Serverless learn more - AWS managed services
Services like Lambda, API Gateway and RDS.
Install the Serverless Framework CLI
$ npm install -g serverless
Install the serverless-jetpack plugin
$ npm install --save-dev serverless-jetpack
Install the serverless-express library
$ npm install @vendia/serverless-express
Add a serverless.yaml file
service: songs-api
frameworkVersion: '3'
plugins:
- serverless-jetpack
provider:
name: aws
runtime: nodejs14.x
region: eu-central-1 # or whatever your region is
functions:
api:
handler: dist/lambda.handler
events:
- http:
method: any
path: /{proxy+}
Create a lambda.ts file in the src folder
The lambda.ts file contains the Lambda handler function, which is the entry point, as referenced in the above serverless.yaml.
import { configure as serverlessExpress } from '@vendia/serverless-express';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
let cachedServer;
export const handler = async (event, context) => {
if (!cachedServer) {
const nestApp = await NestFactory.create(AppModule);
await nestApp.init();
cachedServer = serverlessExpress({ app: nestApp.getHttpAdapter().getInstance() });
}
return cachedServer(event, context);
}
Deploy the application
$ serverless deploy
Cleanup
$ serverless remove
Very recently AWS announced Lambda function URL feature, which eliminates the need for API Gateway but has other trade-offs.
Resources
Deploy a NestJS API to AWS Lambda with Serverless Framework