swift-aws-lambda-runtime icon indicating copy to clipboard operation
swift-aws-lambda-runtime copied to clipboard

Document what to do with AWS "Handler" name.

Open fabianfett opened this issue 5 years ago • 4 comments

We currently don't respect the chosen handler and rely on the executable being named bootstrap. We should at least document that we don't care about the given handler name, or that developers might want to use it in their lambda startup method to decide which handler they wanna attach to the runtime.

fabianfett avatar Apr 06 '20 07:04 fabianfett

I think the handler name is important as the code could contain many lambda function in the same binary even if it executes the only one selected.

Andrea-Scuderi avatar May 29 '20 21:05 Andrea-Scuderi

Hi @Andrea-Scuderi , you can easily achieve the use case you mention with the tools at hand right now:

import AWSLambdaRuntime

switch Lambda.env("_HANDLER") {
case "foo":
     Lambda.run(FooHandler())
case "bar":
     Lambda.run(BarHandler())
default:
    preconditionError("Unexpected handler name: \(Lambda.env("_HANDLER") ?? "none given")")
}

In my humble opinion it doesn't make much sense though to include two or more Handlers in one executable:

  1. If you want to have two different Lambdas, you need to upload the binary twice anyway. AWS does not allow two Lambda functions to share the same binary.
  2. Having more than one Handler within a binary increases the size of the executable, which increases download time onto the Lambda executor and dynamic linking time (cold start time).

Instead I would suggest you create an executable target for each lambda. This way you will have better performance and one less String attached. 😉

In other languages like golang the _HANDLER referrers to the name of the binary. This is something that you can easily build yourself with a bootstrap script that you ship with your lambda. But be aware including multiple binaries within your zip will dramatically worsen your cold start performance.

fabianfett avatar May 29 '20 22:05 fabianfett

@fabianfett Agreed, your considerations are right, but there are cases in which this could make sense:

  1. The binary can be stored in a lambda layer and shared among lambdas.
  2. If the size of the binary doesn't change dramatically by adding more Lambdas, the advantage of having a fatter one could pay in terms of deployment time by reducing the complexity of creating more swift targets in the package. Having a small number of target in the swift package could speed up build time, packaging and upload time. 😉

Andrea-Scuderi avatar May 30 '20 04:05 Andrea-Scuderi

Here the example with multiple handlers #125

Andrea-Scuderi avatar Jun 12 '20 21:06 Andrea-Scuderi