Integrate with fastify aws lambda
felixgani opened this issue · 3 comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.10.2
Plugin version
8.3.0
Node.js version
14.x
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
20.04
Description
I tried to use this plugin at AWS lambda service.
I have tried the Fastify with regular API and it works, but when I try to hit this proxy it always returns
{
"message": "Internal server error"
}
and I got an error log like this on AWS.
Steps to Reproduce
app.js
const path = require('path')
const AutoLoad = require('@fastify/autoload')
const app = fastify()
app.get('/hello', (request, reply) => reply.send({ hello: 'world' }))
app.get('/hs/sss', (request, reply) => {
reply.send({hello: request.headers})
})
app.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
})
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
}
index.js
const awsLambdaFastify = require('@fastify/aws-lambda')
const app = require('./app')
const proxy = awsLambdaFastify(app)
exports.handler = async (event, context) => proxy(event, context)
plugins/services.js
'use strict'
const fp = require('fastify-plugin')
const fastifyProxy = require('@fastify/http-proxy')
const hyperid = require('hyperid')
const uuid = hyperid()
/**
* This plugins adds some utilities to handle http errors
*
* @see https://github.com/fastify/fastify-sensible
*/
module.exports = fp(async function (fastify, opts) {
fastify.register(fastifyProxy, {
upstream: 'https://localhost:8080',
prefix: 'user',
beforeHandler: fastify.circuitBreaker(),
})
})
Expected Behavior
I have tried all of this code locally and everything work well.
Can you help me with this? Where is my fault? I have stuck with this for a week.
It's not your fault. There seem to be a bug somewhere between this module, https://github.com/fastify/fastify-reply-from, light-my-request and @fastify/aws-lambda.
I would recommend against deploying this as a Lambda.
Unfortunately, I do not have time to fix this bug right now. I'd be happy to review a PR, but I can't guide you toward a solution right now.
I unsuccessfully tried to reproduce this issue,
I used these code snippets:
index.js
const awsLambdaFastify = require("@fastify/aws-lambda");
const app = require("./app");
const proxy = awsLambdaFastify(app);
exports.handler = async (event, context) => proxy(event, context);app.js
const path = require("path");
const fastify = require("fastify");
const AutoLoad = require("@fastify/autoload");
const app = fastify();
app.get("/hello", (request, reply) => reply.send({ hello: "world" }));
app.get("/hs/sss", (request, reply) => {
reply.send({ hello: request.headers });
});
app.register(require("@fastify/circuit-breaker"));
app.register(AutoLoad, {
dir: path.join(__dirname, "plugins"),
});
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;
}and services/plugin.js
"use strict";
const fp = require("fastify-plugin");
const fastifyProxy = require("@fastify/http-proxy");
const hyperid = require("hyperid");
const uuid = hyperid();
/**
* This plugin adds some utilities to handle HTTP errors
*
* @see https://github.com/fastify/fastify-sensible
*/
module.exports = fp(async function (fastify, opts) {
fastify.register(fastifyProxy, {
upstream: "https://localhost:8080",
prefix: "user",
beforeHandler: fastify.circuitBreaker(),
});
});deployed on lambda, and everything works as expected.
{"hello":{"sec-fetch-mode":"navigate","x-amzn-tls-version":"TLSv1.2","sec-fetch-site":"none","accept-language":"en-US,en;q=0.9","x-forwarded-proto":"https","x-forwarded-port":"443","x-forwarded-for":"2a01:cb14:7e2:aa00:84e2:7f9c:12f0:7554","sec-fetch-user":"?1","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","x-amzn-tls-cipher-suite":"ECDHE-RSA-AES128-GCM-SHA256","sec-ch-ua":"\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"","sec-ch-ua-mobile":"?0","x-amzn-trace-id":"Root=1-642d6f77-69b1aed45f2687226cd57e0d","sec-ch-ua-platform":"\"Windows\"","host":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.lambda-url.us-east-2.on.aws","upgrade-insecure-requests":"1","accept-encoding":"gzip, deflate, br","sec-fetch-dest":"document","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36","x-request-id":"38574b59-9584-45f9-ab45-f5da5e6becfa","content-length":"0"}}Thanks!
