Lambda warmer optimize aws lambda function cold ways to make a lot of money starts – jeremy daly

At a recent AWS startup day event in boston, MA, chris munns, the senior developer advocate for serverless at AWS, discussed lambda cold starts and how to mitigate them. According to chris (although he acknowledge that it is a “hack”) using the cloudwatch events “ping” method is really the only way to do it right ways to make a lot of money now. He gave a number of really good tips to pre-warm your functions “correctly”:

I deal with cold starts quite a bit, especially when using API gateway. So following these “best practices”, I created lambda warmer. It’s a lightweight node.Js module that can be added to your AWS lambda ways to make a lot of money functions to manage “warming” ping events. It also handles automatic fan-out for warming concurrent functions. Just instrument your code and schedule a “ping”.

Here is an example usage. You simply require the lambda-warmer package and then use it to inspect the event. And that’s it! Lambda warmer will either recognize that this is a “warming” event, scale out if need be, and then short-circuit your function – OR – it will detect that this is an actual request and ways to make a lot of money pass execution off to your main logic.

It’s important to note that you DO NOT HAVE TO ways to make a lot of money WARM all your lambda functions. First of all, according to chris, cold starts account for less than 0.2% of all function invocations. That is an incredibly small percentage that will only affect ways to make a lot of money a tiny number of invocations. Secondly, unless cold starts are causing noticeable latency issues with synchronous ways to make a lot of money invocations, there probably isn’t a need to warm them.

For example, if you have functions responding to asynchronous or streaming events, no one is going to notice if there is a ways to make a lot of money few hundred millisecond start up delay 0.2% of the time. There probably isn’t any reason to warm these functions. If, however, your functions are responding to synchronous API gateway requests and ways to make a lot of money users periodically experience 5 to 10 seconds of latency, then lambda warmer might makes sense.

So, if you have to pre-warm your functions, why would you choose lambda warmer? First of all, it’s open source. I built lambda warmer to solve my own problem and ways to make a lot of money shared it with the community so that others can benefit ways to make a lot of money from it. Pull requests, suggestions and feed back are always welcome. Secondly, it is super lightweight with only one dependency ( bluebird for managing delays). Dependency injection scares me, and given the string of recent NPM hacks, minimizing dependencies is becoming a best practice.

As with single concurrency warming events, lambda warmer inspects the event to determine if it is ways to make a lot of money a “warming” invocation. If it is, lambda warmer attempts to invoke copies of the function to ways to make a lot of money simulate concurrent load. It does this by using the AWS-SDK, this means that your function needs the lambda:invokefunction permission. The invocations are asynchronous and use the event type to ways to make a lot of money avoid blocking. Each invoked function executes with a delay that keeps that ways to make a lot of money function busy while others are starting. The last invocation uses the requestresponse type so that the ways to make a lot of money initial function doesn’t end before the final invocation is made. This prevents the system from reusing any of the running ways to make a lot of money containers.

Logs are automatically generated unless the log configuration option is ways to make a lot of money set to false. I personally like the logs because they contain useful information ways to make a lot of money beyond just invocation data. The warm field indicates whether or not the lambda function ways to make a lot of money was already warm when invoked. The lastaccessed field is the timestamp (in milliseconds) of the last time the function was accessed by a ways to make a lot of money non-warming event. Similarly, the lastaccessedseconds gives you a counter (in seconds) of how long it’s been since it has been accessed.

Interested to hear any further results. I also have experienced this behavior when X-ray is involved. In short, after many different setups and iterations, I haven’t been able to get “affinity” between a container being warmed by lambda-warmer (i.E., on the lambda side), and one being invoked via API gateway, if the lambda is built with X-ray. Tried things like limiting subnets, creating the warmed container first, creating the container via API gateway first, etc. No luck. When using plain old aws-sdk without X-ray, it is possible (my lambdas are nodejs fwiw), though it does seem that API gateway is too quick ways to make a lot of money to create a new container, which it often does even when there are quiescent, warmed lambdas that are not apparently busy. This involves lambdas in a VPC, so maybe I’m just getting unlucky with subnets, but anyway X-ray-wrapped lambdas called from API gateway never pick a warmed ways to make a lot of money lambda in my experience.

The X-ray problem is most disturbing because really it has nothing ways to make a lot of money to do with lambda-warmer, rather it implies that API gateway and lambdas must be ways to make a lot of money tightly coupled from a design/test standpoint in order to achieve pre-warming, i.E., when using X-ray you cannot do anything exclusively from the lambda side ways to make a lot of money (at least not if the lambda is in a VPC), to pre-warm. You have to pre-warm by simulating the entire event chain starting from API ways to make a lot of money gateway, which is much more involved than a simple rule timer.