-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate to ESM and reduce cold start by preloading the fastify instance #89
Comments
interesting... so we could just call This will probably be a breaking change... I don't know if it will be possible to have esm and commonjs compatibility with the same source... Are you already working on a POC? |
Not yet, I just saw this today. However it's a great improvement that matches Fastify philosophy. |
If I've enough time next week, I'll first migrate all our locize and localistars lambdas to ESM. |
I did a first very simple local test, without any changes to aws-lambda-fastify: https://gist.github.com/adrai/39fa41bda8249f0645c6087efdc5c789
So for cold starts using provisioned concurrency there would be a performance optimization of about 882ms (910ms - 57ms) (approx. 80%). So if I understand this correctly, we do not need to do any change in aws-lambda-fastify. |
This is a great blog post BTW ^^. I think we should only update the documentation. |
Will test this first on our production environments... an then, I'll update the readme... |
On a project I'm working on I ran into problems using ES modules due you our tooling not supporting it yet (jest and ts-node specifically), and found that it's possible to make a commonjs module look like an ES module to the lambda runtime so you can do
Posting this here in case it proves useful to anyone else. |
I've modified the above for concurrent calls with async initialisation, again, posting for anyone else who might find it useful: const app = require("./app");
const awsLambdaFastify = require("@fastify/aws-lambda");
let lambdaHandler;
let isInitializing = false;
const main = async () => {
if (!lambdaHandler && !isInitializing) {
isInitializing = true;
lambdaHandler = awsLambdaFastify(app);
await app.ready();
isInitializing = false;
}
// If initialization is in progress, wait until it's complete
while (isInitializing) {
await new Promise(resolve => setTimeout(resolve, 10));
}
return lambdaHandler;
};
module.exports.handler = async (event, context) => {
const handler = await main();
return handler(event, context);
}; |
Prerequisites
🚀 Feature Proposal
Check out https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/. By separating instantiation from execution we can significantly reduce cold starts.
Motivation
No response
Example
No response
The text was updated successfully, but these errors were encountered: