Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Lambda constructor not called in consecutive lambda calls

Tags:

c#

aws-lambda

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
like image 770
mentat Avatar asked Apr 18 '17 03:04

mentat


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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.

like image 183
Udo Held Avatar answered Sep 22 '22 12:09

Udo Held