5 ms·
I'm kind of surprised that the article doesn't mention aws-serverless-express [1], a node module provided by Amazon which makes it fairly trivial to expose an e
by foob 9y ago
I'm kind of surprised that the article doesn't mention aws-serverless-express [1], a node module provided by Amazon which makes it fairly trivial to expose an existing Express app as a Lambda function via AWS API Gateway. The Lambda handler module basically just consists of the following code.
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./app');
const server = awsServerlessExpress.createServer(app);
exports.handler = (event, context) => (
awsServerlessExpress.proxy(server, event, context)
);
It's really convenient to be able to develop simple APIs using Express, and then to expose them via Lambda functions using aws-serverless-express. After you've done it once or twice, it's really easy to throw together single-purpose APIs and deploy them in a matter of minutes. For example, I was recently frustrated with the fact that CircleCI's API for accessing build artifacts is perpetually broken, and I wrote a little microservice to expose the same functionality [2]. It's definitely awesome to be able to deploy one-off projects like that without needing to worry about any sort of server maintenance.
[1] - https://github.com/awslabs/aws-serverless-express https://github.com/awslabs/aws-serverless-express
[2] - https://intoli.com/blog/circleci-artifacts/ https://intoli.com/blog/circleci-artifacts/
- Meegul 9y agoHow's the latency when using this method? I'd imagine if you foresee your site being low usage, startup times would be quite bad. At least, on the order of seconds rather than milliseconds.
- foob 9y agoIt depends a little bit on whether there's a warm container or not, but I think that the overhead is much closer to ~100 milliseconds than to multiple seconds. The CircleCI artifacts API that I linked to takes less than one second to return an artifact, and there's a lot more going on there than a single request/response pair. The endpoint makes an external request to CircleCI's API, does some processing on the response, returns a 301 redirect, and then the redirect is followed by the client and another request is made to actually download the file from S3. You can test this directly by running time curl -L 'https://circleci.intoli.com/artifacts/intoli/exodus/coverage-report/total-coverage.json' which outputs something like the following (the timing will vary a bit obviously). { "coverage": "92.43%" } real 0m0.798s user 0m0.046s sys 0m0.010s
- audiolion 9y agoI just ping my functions every 4 minutes to keep them warm. I am still in the free quota doing that
- andreineculau 9y agoIt is not. You obviously get into a different set of problems than you're accustomed with, but it is not seconds, even on "cold" lambdas. https://read.acloud.guru/cold-starting-lambdas-2c663055589e https://read.acloud.guru/cold-starting-lambdas-2c663055589e
- arbie 9y agoFrom your linked article: "In the absolute worst case it could be as high as 2.8 seconds."
- andreineculau 9y agoOne is the latency of starting up the "cold" lambda, one is the latency of actually starting it up and running your code. The latter is a moving target, subject to the setup i.e. size of loaded code, available memory/CPU, etc, and subject to the actual computation and the latency of its dependencies e.g. network calls, database queries, etc.
- andreineculau 9y agoYou might be interested in trying out https://github.com/tobiipro/http-lambda https://github.com/tobiipro/http-lambda (disclaimer: I'm the author) for reasons that I've explained in a README section https://github.com/tobiipro/http-lambda#a-word-on-httpsgithubcomawslabsaws-serverless-express https://github.com/tobiipro/http-lambda#a-word-on-httpsgithu... Your snippet becomes const {httpLambda} = require('http-lambda'); const app = require('./app'); exports.handler = httpLambda((http, _event, _context) => ( http.createServer(app); ));
- slobodan_ 9y agoHey, I didn't mentioned it in this article because I have another article dedicated to serverless-express (https://medium.freecodecamp.org/express-js-and-aws-lambda-a-serverless-love-story-7c77ba0eaa35 https://medium.freecodecamp.org/express-js-and-aws-lambda-a-...), also we dedicated whole chapter of our book to it too with explanation how does it work, etc. (livebook link: https://livebook.manning.com/#!/book/serverless-apps-with-node-and-claudiajs/chapter-13 https://livebook.manning.com/#!/book/serverless-apps-with-no...)