Single lambda for multiple API Endpoints?

Hi, I have a lambda function which takes the following body:

{
“action”: “list” | “get_object” | “put_object”,
“bucket”: “bucketname”,
“prefix”: “prefix”,
“object”: “object.png”
}

I am planning to have an API Gateway in front of this with 2 resources:

  • bucket1
    • GET - returns items names in bucket
    • {item}
      • GET - returns a pre signed url for downloading
      • PUT - returns a pre signed url for uploading
  • bucket2
    • GET - returns items names in bucket
    • {item}
      • GET - returns a pre signed url for downloading
      • PUT - returns a pre signed url for uploading

The API Gateway would them use a mapping template to map to a body for the lambda from the path parameters.

Is a single lambda fine for this use case (it’s around 100 lines long so not large)? I’ve read online that it could be an anti pattern and having an individual lambda for each endpoint seems excessive in this case.

As far as I know, reusing the actual lambda is anti-pattern.

Reusing the same code in multiple lambda functions is the equivalent of using libraries.

Granted, it might be better for code maintainability to have a logical separation between code functions, but collecting the entire code and putting it in a lambda is only hurtful for newbies who use the code editor in lambda to write code.

Lambdas need to be at a size that makes sense, not too big and not too small. They also need to be deployed atomically.

For example, let’s say you want to implement compression. If you’d split them to separate lambdas, it means that you need to coordinate two deploys at the same time, otherwise it will break. Also you’ll need to share code, such as the compression library, and generating the S3 URIs, etc.

Also, once you multiply:
Lambdas * Regions * Environments (dev/QA/prod) * Versions, it can easily become unmanageable if you split things too much.

Splitting things up makes it more difficult to reason about the system as a whole, and usually entails copy & paste.

If there are different security permissions around each lambda, that might be a good reason to split them up.

So use your judgement, and don’t follow “best practices” blindly

Don’t do that. You are complicating things for no reason splitting the routing and handling logic between the function and the API gateway and adding latency and cost to the function execution. Each function should just one thing and do it fast.

Is a single lambda fine for this use case (it’s around 100 lines long so not large)?

I say go for it, It’s not a big deal, I think Ideally you would split Lambdas accross business entities in your API, there’s nothing wrong in having a single Lambda that handles say the ‘Posts’ entity and can receive requests for all its methods (get, list, create, update, etc).

It also has the advantage of reusing the container so it’s good for cold starts.

I think the important factor here is scale though, both in terms of concurrency and codebase (If you abuse this you can easily end with a messy and huge code for the lambda where it’d be better if split into pieces) (or hit a concurrency limit by say serving all your traffic from your API with the same Lambda).

Using a single lambda might be the best option in certain cases, I don’t consider it an anti-pattern at all.

Here are some things you want to consider:

  • Updating your API gateway configuration requires multiple steps/orchestration. Changes need to be deployed to a stage. By keeping the AGW config generic and stable, you only need to update your lambda.
  • The two routes seem to do the same thing. By Using a {bucket} route parameter, you avoid duplication
  • Mapping templates are complex and hard to test (my experience). It takes a few lines to transform the request from code and you keep the logic in the lambda.

Just do what makes sense to you and your team. Everybody uses them differently. We use one lambda per service and serve all the service endpoints from one lambda running bottle or flask.

I think it is wrong to have two endpoints. It does not seem right to have /api/bucket1 and /api/bucket2 as different endpoints. It’s the same type of resource so to follow REST definitions you should make it api/{bucket}/{item} . You can still peform validations inside the function.

I use one lambda for all of my api routes, with an internal router.

I am, however, using Rust for my lambda, so this is just a consequence of the fact that it is significantly easier to deploy a monolithic lambda than 20 different binaries.

it means that you need to coordinate two deploys at the same time, otherwise it will break

You should be able to design your system and APIs such that you don’t need to do instant on/off deployments. This sounds like a very fragile system.

Your code should be able to be rolled out gradually and, maybe more important, able to be rolled back. If lambda A depends on code deployed to lambda B first then now you’re going to have to worry about rolling back lambda A before lambda B. Where are your interfaces? Where are your contracts?

If you’re going to dramatically change the behavior, you don’t always have to do an update in place. Create a new lambda for a new api. Get it working. Move traffic to it. Delete the old one if you have to.

And if you’re managing all of this with infrastructure as code, it’s not going to be very hard to deploy even a couple dozen lambdas to different stages/regions.

It can be nice to see that a particular lambda is throwing errors and have a reasonable guess at why without digging through the logs because you know what that lambda’s one job is. Else you’ll just have to make sure you instrument metrics throughout each API in that one lambda function.

One lambda function can be beneficial in that calls for lesser used APIs will be more likely to hit a warm lambda than they otherwise would have. Since traffic from the other APIs is keeping your lambda containers warm.

We’re doing something similar, maybe around 20 unique endpoints within a single Lambda. Internally routed. Using Nodejs.

Want to split into two different Lambdas though as a couple of the endpoints consume a lot more RAM than the rest and it bothers me that the single Lambda must be configured with quite higher ram for 95% of the requests. Haven’t got time to get around to do it yet.