9

I have a c# aws lambda class with some code in its constructor. The lambda method itself is getting called every time I initiate it (with an SNS message post), however, I cannot see the constructor getting called (added log calls to observe from cloudwatch). The constructor only gets called at first launch (after the aws stack creation/update).

Is this an expected behaviour? Does aws somehow cache my lambda instances?

public class MyLambda
{
     public MyLambda()
     {
          Console.WriteLine("Hello from ctor");
     }

     // This is the method assigned in CloudFormation
     public bool Execute(SNSEvent snsEvent)
     {          
          Console.WriteLine("Lambda called");
          return true;
     }
}

And here is the outcome in cloudwatch log; First time initiate Lambda:

Hello from ctor
Lambda called

And second time initiation of Lambda

Lambda called
2
  • Add your code to the question. We can't help you without it. Commented Apr 18, 2017 at 5:12
  • 2
    I think this is really nice behaviour. It means that you can re-use data and objects between subsequent Lambda function calls. Commented May 7, 2018 at 7:46

2 Answers 2

15

AWS reuses the instances as described in this blog post, in the FAQ and the official documentation.

In general the instances are reused and replaced every now and then. If you have a higher load AWS will create more concurrent instances. So usually it's very likely that your instances get reused, but you cannot count on it as they get recycled. When the instance is reused than the constructor won't be called again as the constructor was already called during the initialization.

Usually the first call to a new instance is quite slow, as the run-time does initialization like loading itself, class loading, etc and calling the constructor. The subsequent calls are usually much faster as the Lambda is already fully initialized. However, if you haven't called your Lambda for a while it needs some warm-up from its "freeze" as well. This still constitutes a reuse, so the constructor won't be called again.

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

1 Comment

sure, but the definition of instance should not map to a c# class in my understanding. aws lambda spins up a .net environment, or container, which I call as the actual instance. Otherwise, the c# class is inherently a singleton impl, which i think not documented anywhere at all. However, your last link vaguely describes sth similar to what I am observing..
0

This means your lambdas must be idempotent, and you shouldn't do anything else than initialization in your class constructor (as it always should be), because you can't be certain it will be called again in subsequent calls...

I think it's a good thing that will speed up the process... for example all the services you configure and inject in your constructor are one of the most expensive things performance wise, and they won't be done again and again.

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.