AWS Lambda Function Invocation

Invoke AWS Lambda Function programmatically using .NET, C# and AWS Lambda SDK

Adityanand Pasumarthi
The AWS Coder

--

In this small story I’ll show you how to invoke an AWS Lambda Function programmatically using .NET, C# and AWS Lambda SDK.

AWS Lambda is a serverless platform that allows us to run our application docker images or raw code on a dynamically assigned container/compute fleet inside AWS. This allows us to focus on our code functionality and let AWS take care of spinning up required compute resources, which automatically scales up and down based on the load on our code.

I created a small AWS Lambda Function in .NET & C#. The function takes in a number and returns true if the number is a prime number, and returns false if the number is not a prime number. This sample function code is then packaged into a Docker image, which then is uploaded to AWS as Lambda Function using Visual Studio 2019. The sample has a console program, which calls into our AWS Lambda Function that was uploaded to our AWS account.

Below is the link to the sample on GitHub. You have to put your AWS related Api & Secret Keys and other settings in the appsettings.json file in the console application project, based on your AWS account configuration.

https://github.com/apasumarthi1999/AWSLambdaDotNet

Coming back our story, the console program invokes the AWS Lambda Function programmatically using AWS Lambda SDK. The console program tries to check for all prime numbers within a range of numbers.

There are much much efficient ways of checking for prime numbers within a range of numbers. This sample code is designed this way to showcase how a AWS Lambda Function can be programmatically invoked, and how to use TPL (Task Parallel Library) of .NET to parallelize AWS Lambda Function invocations, where required and makes sense.

Below is the prime number function and console program code. There are a few points to note in the console program code.

First, see how the Parallel for loop is used to call into the AWS Lambda Functions parallelly. This helps us verify the prime number in parallel. We did not use async/await in calling the AWS Lambda Function within the parallel loop. This is because, we want to wait until all the numbers (all the invocations) are verified. And if we use async action for the Parallel for loop, it will initiate the execution of all the numbers and come out before the actual AWS Lambda Function invocations are complete. So we use GetAwaiter and GetResult to wait for the completion of the AWS Lambda Function invocations.

Second, we used some global static variables to track the concurrent executions and max concurrent executions. These data points will show us, how many of the AWS Lambda Function invocations are happening in parallel at any given point of time. This is important because, we do not want to spin up more functions and hit resource limits on our AWS account and also we want to make sure we use the AWS Lambda Function resources efficiently. Since our prime number function is packaged as a Docker image, it takes a few hundred milliseconds to few seconds to spin up the compute resources to run our function. The more functions are invoked in parallel, the more docker image containers have to be spun up by AWS. This will take time and also incur some additional cost. So, by keeping the concurrency to a limited number, we ensure we re-use the function compute resources that are spun up by AWS for further invocations and thus make our function invocations faster. This also keeps the AWS cost in check (though minimal).

Finally below is the code for the prime number function…

Below is the code for invoking the above AWS Lambda Function from a console program…

Happy AWS coding!

--

--