UNPKG

3.67 kBMarkdownView Raw
1<h1 align="center">Serverless</h1>
2
3Run serverless applications and REST APIs using your existing Fastify application.
4
5### Attention Readers:
6> Fastify is not designed to run on serverless environments.
7The Fastify framework is designed to make implementing a traditional HTTP/S server easy.
8Serverless environments requests differently than a standard HTTP/S server;
9thus, we cannot guarantee it will work as expected with Fastify.
10Regardless, based on the examples given in this document,
11it is possible to use Fastify in a serverless environment.
12Again, keep in mind that this is not Fastify's intended use case and
13we do not test for such integration scenarios.
14
15
16## AWS Lambda
17
18The sample provided allows you to easily build serverless web applications/services
19and RESTful APIs using Fastify on top of AWS Lambda and Amazon API Gateway.
20
21*Note: This is just one possible way.*
22
23### app.js
24
25```js
26const fastify = require('fastify');
27
28function init(serverFactory) {
29 const app = fastify({ serverFactory });
30 app.get('/', (request, reply) => reply.send({ hello: 'world' }));
31 return app;
32}
33
34if (require.main !== module) {
35 // called directly i.e. "node app"
36 init().listen(3000, (err) => {
37 if (err) console.error(err);
38 console.log('server listening on 3000');
39 });
40} else {
41 // required as a module => executed on aws lambda
42 module.exports = init;
43}
44```
45
46You can simply wrap your initialization code by offering to inject an optional [serverFactory](https://www.fastify.io/docs/latest/Server/#serverfactory).
47
48When executed in your lambda function we don't need to listen to a specific port,
49so we just export the wrapper function `init` in this case.
50The [`lambda.js`](https://www.fastify.io/docs/latest/Server/#lambda.js) file will use this export.
51
52When you execute your Fastify application like always,
53i.e. `node app.js` *(the detection for this could be `require.main === module`)*,
54you can normally listen to your port, so you can still run your Fastify function locally.
55
56### lambda.js
57
58```js
59const awsServerlessExpress = require('aws-serverless-express');
60const init = require('./app');
61
62let server;
63const serverFactory = (handler) => {
64 server = awsServerlessExpress.createServer(handler);
65 return server;
66}
67const app = init(serverFactory);
68
69exports.handler = (event, context, callback) => {
70 context.callbackWaitsForEmptyEventLoop = false;
71 app.ready((e) => {
72 if (e) return console.error(e.stack || e);
73 awsServerlessExpress.proxy(server, event, context, 'CALLBACK', callback);
74 });
75};
76```
77
78We define a custom `serverFactory` function, in which we create a new server with the help of [`aws-serverless-express`](https://github.com/awslabs/aws-serverless-express)
79(make sure you install the dependency `npm i --save aws-serverless-express`).
80Then we call the `init` function (imported from [`app.js`](https://www.fastify.io/docs/latest/Server/#app.js)) with the `serverFactory` function as the only parameter.
81Finally inside the lambda `handler` function we wait for the Fastify app to be `ready`
82and proxy all the incoming events (API Gateway requests) to the `proxy` function from [`aws-serverless-express`](https://github.com/awslabs/aws-serverless-express).
83
84
85### Example
86
87An example deployable with [claudia.js](https://claudiajs.com/tutorials/serverless-express.html) can be found [here](https://github.com/claudiajs/example-projects/tree/master/fastify-app-lambda).
88
89
90### Considerations
91
92- API Gateway doesn't support streams yet, so you're not able to handle [streams](https://www.fastify.io/docs/latest/Reply/#streams).
93- API Gateway has a timeout of 29 seconds, so it's important to provide a reply during this time.