6

Is it possible for multiple Lambda function definitions to be deployed to the same container instance?

I understand that a given Lambda container will only execute at most one function at a time, but wanted to understand the composition relationship between functions and the host container.

For example, in the Serverless App project type for Visual Studio with the AWS Toolkit Extensions, it's possible to define multiple functions in a single project, but do these get deployed via CloudFormation into separate containers or a single container representing the project?

4 Answers 4

4

I think it might help to separate out the process:

  • A lambda deployment is a zip file of code, and a matching configuration. In the case of your Serverless App project type, when you have multiple lambda functions to a project, you're creating multiple deployments.
  • A lambda instance is a running version of a deployment hosted inside of a container. Only one lambda instance is allowed in a container, that is an AWS guarantee. This means that you can never get access to code/memory/files outside of the currently running instance (either yours or anyone else's!)

As an optimisation AWS does re-use instances by freezing and thawing the container. This is because it's expensive to start a fresh container, copy the deployment, and do the init code of the deployment (known as the cold start).

Sign up to request clarification or add additional context in comments.

4 Comments

Can you have multiple handlers per deployment? I need to understand if different handlers can be hosted together and re-use the same execution context, eg static initialization / in-memory cache, such as an API Gateway mapped to independent Lambda function handlers for different endpoints mapped through the gateway instance.
You can have whatever code you want in a deployment, but only the handler you configured to be so will get called. So you can share compile-time information between handlers (e.g. a file of constants, or common functionality), but you cannot share runtime information (e.g. connection pools, or variables set on init). (n.b. runtime information is shared by the container- not the instance, so two concurrent instances of the same handler will be running in separate containers).
Each individual function in your project is stateless and self-contained.
Do you know where i can find any docs from AWS that confirm "Only one lambda instance is allowed in a container, that is an AWS guarantee"?
0

There's not an official document on the matter, but I will share what I have gathered the past years from Docs/Posts/Conferences:

Lambda functions are "framework" (or something like it, just that word is the closest I can think of and the only one I have heard from AWS representative) on top of a Containerization service. Every time you call lambda function a container is being run (it could be an existing one, put on hold - adds performance boost, or an entirely new one, you can never know which one it is.)

You could assume, the container instance (using instance as it is used in ECS, a host machine) is the same, having in mind there are some workarounds for DB Connection pooling and things of the like, but nobody guarantees you that.

Comments

0

CloudFormation will deploy the functions to the same AWS account. This is happening at the AWS user account level. It's not running the functions.

Lambdas are event-driven and only run when they are triggered. Every instance of a Lambda is standalone as far as the user experiences it, and is "deployed in its own container" when triggered.

Maybe something deeper is happening under the abstraction layers but that's how you'll experience it.

Comments

0

I did this by using ExpressJS+NodeJS. Every function is a different ExpressJS route. However, so far I have not been able to do it with Spring Cloud Function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.