Yeah, weād love to add official support
The main blocker is that AWS lambdas contain a āstage pathā in the URL, for example /dev
or /latest
, and itās not consistent when you use a custom/non-custom domain.
https://ov8mwmh2xa.execute-api.us-east-1.amazonaws.com/latest
There doesnāt seem to be an standard way to get rid of it, but Iāve been investigating and using event.pathParameters.proxy
seems to be one of the best ways, so Iād like to try that first.
This scaffold is a bit old, but implements the replacement of pathParameters.proxy
:
module.exports = (ctx, next) => {
ctx.lambdaEvent =
(ctx.headers["x-apigateway-event"] &&
JSON.parse(decodeURIComponent(ctx.headers["x-apigateway-event"]))) ||
{};
ctx.lambdaContext =
(ctx.headers["x-apigateway-context"] &&
JSON.parse(decodeURIComponent(ctx.headers["x-apigateway-context"]))) ||
{};
ctx.env = (ctx.lambdaEvent && ctx.lambdaEvent.stageVariables) || process.env;
// Workaround an inconsistency in APIG. For custom domains, it puts the
// mapping prefix on the url, but non-custom domain requests do not. Fix it by
// changing the path to the proxy param which has the correct value always.
if (ctx.lambdaEvent.pathParameters && ctx.lambdaEvent.pathParameters.proxy) {
const dummyBase = "zz://zz";
const url = new URL(ctx.url, dummyBase);
url.pathname = "/" + ctx.lambdaEvent.pathParameters.proxy;
ctx.url = url.href.replace(dummyBase, "");
}
return next();
};
Apart from that, any āKoa/Express to Lambdaā library should do the trick. That scaffold is using aws-serverless-express
which seems to be one of the most popular ones, so I guess we can try with that.
Frontity uses Koa, not Express, but exports a req/res function by using app.callback()
so thereās not much difference.
This is the code of the scaffold:
const awsServerlessExpress = require("aws-serverless-express");
const app = require("./app");
app.proxy = true;
const server = awsServerlessExpress.createServer(app.callback());
exports.handler = (event, context) =>
awsServerlessExpress.proxy(server, event, context);
In Frontity, the server.js
file has already the app.callback()
, so itād be something like this:
const awsServerlessExpress = require("aws-serverless-express");
const frontity = require("./build/server.js").default;
frontity.proxy = true;
const server = awsServerlessExpress.createServer(frontity);
exports.handler = (event, context) =>
awsServerlessExpress.proxy(server, event, context);
If you want to give it a try let me know what you find out!