Aws Lambda Fastify

Insipired by aws-serverless-express to work with Fastify with inject functionality.
Alternatives To Aws Lambda Fastify
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Serverless Express4,8926601764 months ago33December 06, 202076apache-2.0JavaScript
Run Node.js web applications and APIs using existing application frameworks on AWS #serverless technologies such as Lambda, API Gateway, Lambda@Edge, and ALB.
15 hours ago3JavaScript
Practical Node.js, 1st and 2nd Editions [Apress] 📓
Grant3,771210469 months ago98March 09, 202223mitJavaScript
OAuth Proxy
Serverless Http1,6524422057 days ago50March 11, 202359otherJavaScript
Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Serverless Boilerplate Express Typescript490
a day ago2mitTypeScript
🚀🎉📚 Boilerplate and Starter for Serverless framework, ExpressJS, TypeScript, Prisma and MongoDB ⚡️ Made with developer experience first: Serverless framework + Live reload + Offline support + ExpressJS + TypeScript + ESLint + Prettier + Husky + Commitlint + Lint-Staged + Jest + Dotenv + esbuild + VSCode
6 years ago4mitJavaScript
A collection of frameworks that I love with a strong focus on clean code, testing, software architecture/design and devops.
Aws Lambda Fastify44443 days ago28June 05, 20224mitJavaScript
Insipired by aws-serverless-express to work with Fastify with inject functionality.
2 years ago30otherJavaScript
⚡ Take existing Express.js apps and host them easily on cheap, auto-scaling, serverless infrastructure (AWS Lambda and AWS HTTP API).
Amazon Cognito Example For External Idp258
2 months ago2mit-0TypeScript
An example for using Amazon Cognito together with an external IdP
Fullstack App253
3 years ago12JavaScript
⚡ Ready-to-use, serverless, full-stack application built with AWS Lambda, Express.js, React, AWS DynamoDB and AWS HTTP API.
Alternatives To Aws Lambda Fastify
Select To Compare

Alternative Project Comparisons


CI NPM version js-standard-style

Inspired by the AWSLABS aws-serverless-express library tailor made for the Fastify web framework.

No use of internal sockets, makes use of Fastify's inject function.

Seems faster (as the name implies) than aws-serverless-express and aws-serverless-fastify 😉


$ npm i @fastify/aws-lambda


@fastify/aws-lambda can take options by passing them with : awsLambdaFastify(app, options)

property description default value
binaryMimeTypes Array of binary MimeTypes to handle []
enforceBase64 Function that receives the response and returns a boolean indicating if the response content is binary or not and should be base64-encoded undefined
serializeLambdaArguments Activate the serialization of lambda Event and Context in http header x-apigateway-event x-apigateway-context false (was true for <v2.0.0)
decorateRequest Decorates the fastify request with the lambda Event and Context request.awsLambda.event request.awsLambda.context true
decorationPropertyName The default property name for request decoration awsLambda
callbackWaitsForEmptyEventLoop See: Official Documentation undefined
retainStage Retain the stage part of the API Gateway URL false



const awsLambdaFastify = require('@fastify/aws-lambda')
const app = require('./app')

const proxy = awsLambdaFastify(app)
// or
// const proxy = awsLambdaFastify(app, { binaryMimeTypes: ['application/octet-stream'], serializeLambdaArguments: false /* default is true */ })

exports.handler = proxy
// or
// exports.handler = (event, context, callback) => proxy(event, context, callback)
// or
// exports.handler = (event, context) => proxy(event, context)
// or
// exports.handler = async (event, context) => proxy(event, context)


const fastify = require('fastify')

const app = fastify()
app.get('/', (request, reply) => reply.send({ hello: 'world' }))

if (require.main === module) {
  // called directly i.e. "node app"
  app.listen({ port: 3000 }, (err) => {
    if (err) console.error(err)
    console.log('server listening on 3000')
} else {
  // required as a module => executed on aws lambda
  module.exports = app

When executed in your lambda function we don't need to listen to a specific port, so we just export the app in this case. The lambda.js file will use this export.

When you execute your Fastify application like always, i.e. node app.js (the detection for this could be require.main === module), you can normally listen to your port, so you can still run your Fastify function locally.


Lambda arguments

The original lambda event and context are passed via Fastify request and can be used like this:

app.get('/', (request, reply) => {
  const event = request.awsLambda.event
  const context = request.awsLambda.context
  // ...

If you do not like it, you can disable this by setting the decorateRequest option to false.

Alternatively the original lambda event and context are passed via headers and can be used like this, if setting the serializeLambdaArguments option to true:

app.get('/', (request, reply) => {
  const event = JSON.parse(decodeURIComponent(request.headers['x-apigateway-event']))
  const context = JSON.parse(decodeURIComponent(request.headers['x-apigateway-context']))
  // ...

Lower cold start latency

Since AWS Lambda now enables the use of ECMAScript (ES) modules in Node.js 14 runtimes, you could lower the cold start latency when used with Provisioned Concurrency thanks to the top-level await functionality.

We can use this by calling the fastify.ready() function outside of the Lambda handler function, like this:

import awsLambdaFastify from '@fastify/aws-lambda'
import app from './app.js'
export const handler = awsLambdaFastify(app)
await app.ready() // needs to be placed after awsLambdaFastify call because of the decoration:

Here you can find the approriate issue discussing this feature.

⚡️Some basic performance metrics

@fastify/aws-lambda (decorateRequest : false) x 56,892 ops/sec ±3.73% (79 runs sampled)

@fastify/aws-lambda x 56,571 ops/sec ±3.52% (82 runs sampled)

@fastify/aws-lambda (serializeLambdaArguments : true) x 56,499 ops/sec ±3.56% (76 runs sampled)

serverless-http x 45,867 ops/sec ±4.42% (83 runs sampled)

aws-serverless-fastify x 17,937 ops/sec ±1.83% (86 runs sampled)

aws-serverless-express x 16,647 ops/sec ±2.88% (87 runs sampled)

Fastest is @fastify/aws-lambda (decorateRequest : false), @fastify/aws-lambda


  • For apps that may not see traffic for several minutes at a time, you could see cold starts
  • Stateless only
  • API Gateway has a timeout of 29 seconds, and Lambda has a maximum execution time of 15 minutes. (Using Application Load Balancer has no timeout limit, so the lambda maximum execution time is relevant)
  • If you are using another web framework beside Fastify (i.e. Connect, Express, Koa, Restana, Sails, Hapi, Restify) or want to use a more generic serverless proxy framework, have a look at: serverless-http

🎖Who is using it?

locize is using @fastify/aws-lambda
localistars is using @fastify/aws-lambda

The logos displayed in this page are property of the respective organisations and they are not distributed under the same license as @fastify/aws-lambda (MIT).

Popular Amazon Web Services Projects
Popular Express Projects
Popular Cloud Computing Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Amazon Web Services
Cloud Computing
Lambda Functions
Api Gateway