Q: is there any way to attach a shutdown hook fn in HL? i.e. when lambda is unloaded because it’s idle too long, I want a fn to run which disposes network connections
Shutdown event is not supported via internal extension. If I create an external extension still you would not be able to access the resources, since the extension runs in a separate process.
{:headers {nil #object[java.util.Collections$UnmodifiableRandomAccessList 0x176ce308 [HTTP/1.1 403 Forbidden]], Date #object[java.util.Collections$UnmodifiableRandomAccessList 0x3ce7b033 [Fri, 05 Nov 2021 13:29:08 GMT]], Content-Length #object[java.util.Collections$UnmodifiableRandomAccessList 0x1ccbf9ac [116]], Content-Type #object[java.util.Collections$UnmodifiableRandomAccessList 0xd4b0649 [application/json]]}, :success? false, :status 403, :body {:errorMessage SHUTDOWN: ShutdownEventNotSupportedForInternalExtension, :errorType Extension.InvalidEventType}}ok. not possible at all then?
You mean something like: 1. When the lambda has handled the payload. 2. And there is no more payloads, but Lambda is alive. 3. Call the hook?
Or maybe something like: 1. Lambda called the user handler. 2. Lambda is waiting for the response. 3. It's close to timeout, so call the hook?
Nope. Only via external extension that run in the different process.
But this is not something I want to do
thanks for looking into it
my use-case for this was cleaning up Redis connections but I’ve discovered (through load testing) that they are cleaned up automatically as the lambdas are unloaded
so don’t really need to do it in a shutdown hook
Cool. Also great you’re doing load testing. You prove how HL is a stable runtime
neither. when the lambda is shutting down because it’s been idle for too long. then I want to be able to call a hook so I can clean up network connections held in an atom
This is not possible.
would be useful for anyone wanting to see when their lambdas are shutdown since it could write to the logs
I thought I read somewhere that it is possible but only with a custom runtime
Basically AWS Lambda does not provide any CPU or memory to the idle lambda. All the operations are stopped.
Otherwise the runtime loop white true would run forever
But it doesn't.
and no events while shutting down?
yeah, that makes sense
Hmm. Only the regular Linux shutdown hooks, but I don't know whether the code will be invoked. I can try.
cool. I’m sure I’m not the only person who would like to use this if it’s possible
I will have some more time in few hours, so I will investigate it. 🙂
I'm reading this: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html. It seems that I would need to create an extension for this.
It looks like the runtime is killed immediately, while the extensions have 500ms to shutdown gracefully. ;O
Ok I do understand what to do, but be aware that you will pay additional $ for this.
https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html#runtimes-extensions-registration-api The hook will be in:
(h/entrypoint ~lambdas {:shutdown-hook (fn [])})sure. it would only be for the extra cpu time of the hook fn right? these hooks should be very small/fast so that this cost is low
even just an extra log line will be useful for most people because then they can see how quickly lambdas are unloaded i.e. when cold starts will be required again
this will force me to upgrade HL 🙂
I'm not sure how this gonna work. I think after the shutdown-hook is called the AWS will not wait? Or maybe I should manually exit the process.
> sure. it would only be for the extra cpu time of the hook fn right? these hooks should be very small/fast so that this cost is low Yeah
Cold start would be a little bit higher, since I have to register the internal extension.
Probably few ms, when shutdown-hook is used
It would be optional? If so, we can choose the small trade-off
Yeah. It will be optional.
cool. they are small tradeoffs for this benefit