Installations
npm install @fastify/aws-lambda
Score
99.5
Supply Chain
100
Quality
85.4
Maintenance
100
Vulnerability
100
License
Developer
fastify
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
20.17.0
NPM Version
10.8.2
Statistics
511 Stars
300 Commits
34 Forks
11 Watching
3 Branches
26 Contributors
Updated on 17 Nov 2024
Bundle Size
4.59 kB
Minified
1.54 kB
Minified + Gzipped
Languages
JavaScript (94.41%)
TypeScript (5.59%)
Total Downloads
Cumulative downloads
Total Downloads
2,933,927
Last day
12%
15,104
Compared to previous day
Last week
7.3%
72,517
Compared to previous week
Last month
12.3%
274,665
Compared to previous month
Last year
99.4%
1,859,039
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Introduction
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 😉
👨🏻💻Installation
1$ npm i @fastify/aws-lambda
Options
@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 |
pathParameterUsedAsPath | Use a defined pathParameter as path (i.e. 'proxy' ) | false |
parseCommaSeparatedQueryParams | Parse querystring with commas into an array of values. Affects the behavior of the querystring parser with commas while using Payload Format Version 2.0 | true |
📖Example
lambda.js
1const awsLambdaFastify = require('@fastify/aws-lambda') 2const app = require('./app') 3 4const proxy = awsLambdaFastify(app) 5// or 6// const proxy = awsLambdaFastify(app, { binaryMimeTypes: ['application/octet-stream'], serializeLambdaArguments: false /* default is true */ }) 7 8exports.handler = proxy 9// or 10// exports.handler = (event, context, callback) => proxy(event, context, callback) 11// or 12// exports.handler = (event, context) => proxy(event, context) 13// or 14// exports.handler = async (event, context) => proxy(event, context)
app.js
1const fastify = require('fastify') 2 3const app = fastify() 4app.get('/', (request, reply) => reply.send({ hello: 'world' })) 5 6if (require.main === module) { 7 // called directly i.e. "node app" 8 app.listen({ port: 3000 }, (err) => { 9 if (err) console.error(err) 10 console.log('server listening on 3000') 11 }) 12} else { 13 // required as a module => executed on aws lambda 14 module.exports = app 15}
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.
📣Hint
Lambda arguments
The original lambda event and context are passed via Fastify request and can be used like this:
1app.get('/', (request, reply) => { 2 const event = request.awsLambda.event 3 const context = request.awsLambda.context 4 // ... 5})
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
:
1app.get('/', (request, reply) => {
2 const event = JSON.parse(decodeURIComponent(request.headers['x-apigateway-event']))
3 const context = JSON.parse(decodeURIComponent(request.headers['x-apigateway-context']))
4 // ...
5})
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:
1import awsLambdaFastify from '@fastify/aws-lambda' 2import app from './app.js' 3export const handler = awsLambdaFastify(app) 4await app.ready() // needs to be placed after awsLambdaFastify call because of the decoration: https://github.com/fastify/aws-lambda-fastify/blob/master/index.js#L9
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
⚠️Considerations
- 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 or serverless-adapter
🎖Who is using it?
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).
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
14 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/fastify/.github/SECURITY.md:1
- Info: Found linked content: github.com/fastify/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/fastify/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/fastify/.github/SECURITY.md:1
Reason
SAST tool is not run on all commits -- score normalized to 9
Details
- Warn: 23 commits out of 25 are checked with a SAST tool
Reason
Found 3/9 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
6.2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to @fastify/aws-lambda
aws-lambda-fastify
`aws-lambda-fastify@2.2.0` has been deprecated. Please use `@fastify/aws-lambda@3.0.0` instead.
@travetto/rest-fastify-lambda
Fastify AWS Lambda provider for the travetto rest module.
@aws-sdk/client-lambda
AWS SDK for JavaScript Lambda Client for Node.js, Browser and React Native
@whatwg-node/server
Fetch API compliant HTTP Server adapter