Skip to main content Link Menu Expand (external link) Document Search Copy Copied

AWS Lambda

Table of contents

  1. Installation
  2. Usage
    1. Parameters
  3. Localstack

Installation

npm install @serverless-framework/aws-lambda

AWS Lambda with Node.js


Usage

  
import { RequestInterface, ResponseInterface, HttpStatusCode } from '@serverless-framework/core';
import { Api } from '@serverless-framework/aws-lambda';
import { APIGatewayProxyEvent, APIGatewayProxyEventV2, Context } from 'aws-lambda';

const API_VERSION = '1.0.0';

const api = new Api({
  base: '/api',
});

api.get('/version', (req: RequestInterface, res: ResponseInterface) => {
  req.status(HttpStatusCode.OK).json({
    version: API_VERSION,
  });
});

export const handler = api.handle((event: APIGatewayProxyEvent | APIGatewayProxyEventV2, context: Context) => await api.run());

Parameters

api.get('/users/:id', (req: RequestInterface<{params: {id: string}}>, res: ResponseInterface) => {
      req.status(HttpStatusCode.OK).json({
      id: params.id,
  });
});

Localstack


Next: Learn more about routing.