Skip to content

Tochukz/Serverless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless

AWS Serverless

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.

Frameworks

  1. AWS Serverless Application Model (AWS SAM)
  2. AWS Cloud Development Kit (AWS CDK)
  3. The Serverless Framework (serverless.com)
  4. Chalice framework for Python apps (github.com/aws/chalice)
  5. Arc.code (arc.codes/docs)
  6. Claudia.js (claudiajs.com/)

AWS Serverless Application Model (SAM)

AWS SAM
Developer Guide
CLI Reference

NestJS + AWS Lambda + Serverless framework

Tech Stack

  1. Nest.js
    A powerful framework for creating sever-side applications, learn more.
  2. Serverless Framework
    Easy to use framework for developing and deploying serverless apps, learn more.
  3. Serverless Jetpack
    A low-config plugin that packages our code to be deployed to AWS Lambda, learn more.
  4. Serverless Express
    A library that makes our "plain" NestJS API play nicely with Serverless learn more
  5. AWS managed services
    Services like Lambda, API Gateway and RDS.

Setup

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

About

Serverless applications

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors