1 | <h1 align="center">Serverless</h1>
|
2 |
|
3 | Run serverless applications and REST APIs using your existing Fastify application.
|
4 |
|
5 | ### Attention Readers:
|
6 | > Fastify is not designed to run on serverless environments.
|
7 | The Fastify framework is designed to make implementing a traditional HTTP/S server easy.
|
8 | Serverless environments requests differently than a standard HTTP/S server;
|
9 | thus, we cannot guarantee it will work as expected with Fastify.
|
10 | Regardless, based on the examples given in this document,
|
11 | it is possible to use Fastify in a serverless environment.
|
12 | Again, keep in mind that this is not Fastify's intended use case and
|
13 | we do not test for such integration scenarios.
|
14 |
|
15 |
|
16 | ## AWS Lambda
|
17 |
|
18 | The sample provided allows you to easily build serverless web applications/services
|
19 | and 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
|
26 | const fastify = require('fastify');
|
27 |
|
28 | function init(serverFactory) {
|
29 | const app = fastify({ serverFactory });
|
30 | app.get('/', (request, reply) => reply.send({ hello: 'world' }));
|
31 | return app;
|
32 | }
|
33 |
|
34 | if (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 |
|
46 | You can simply wrap your initialization code by offering to inject an optional [serverFactory](https://www.fastify.io/docs/latest/Server/#serverfactory).
|
47 |
|
48 | When executed in your lambda function we don't need to listen to a specific port,
|
49 | so we just export the wrapper function `init` in this case.
|
50 | The [`lambda.js`](https://www.fastify.io/docs/latest/Server/#lambda.js) file will use this export.
|
51 |
|
52 | When you execute your Fastify application like always,
|
53 | i.e. `node app.js` *(the detection for this could be `require.main === module`)*,
|
54 | you can normally listen to your port, so you can still run your Fastify function locally.
|
55 |
|
56 | ### lambda.js
|
57 |
|
58 | ```js
|
59 | const awsServerlessExpress = require('aws-serverless-express');
|
60 | const init = require('./app');
|
61 |
|
62 | let server;
|
63 | const serverFactory = (handler) => {
|
64 | server = awsServerlessExpress.createServer(handler);
|
65 | return server;
|
66 | }
|
67 | const app = init(serverFactory);
|
68 |
|
69 | exports.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 |
|
78 | We 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`).
|
80 | Then 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.
|
81 | Finally inside the lambda `handler` function we wait for the Fastify app to be `ready`
|
82 | and 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 |
|
87 | An 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.
|