AWS Lambda: A Brief Introduction

AWS Lambda functions have revolutionized the way we build applications. The ability to upload code to an AWS infrastructure and utilize the necessary resources without the maintenance of a server has allowed developers to accelerate development time. Before we begin, let’s define what an AWS Lambda’s purpose.

AWS Lambda utilizes the necessary resources to perform tasks based on user requests. The Lambda function is design to start when a request is made (Which triggers the Lambda function), once the request is completed, the Lambda function shuts down eliminating unnecessary resources. Here are some of the services that an AWS Lambda function provides:

  • Run code without managing servers, AWS takes care of this for you, while you focus on the code.
  • Run your code when needed – pay for the resources that are utilized.

Here are some reasons why you would want to use an AWS Lambda function:

  • Applications involving event-based triggers
  • Applications that inherit from a serverless architecture
  • Quick deployment and release lifecycle.

Writing an AWS Lambda function in JavaScript

We’ll create our first AWS Lambda function in JavaScript. Let’s break down the necessary requirements needed to write a Lambda function in Node.Js. Within our AWS Console, we can create a Lambda function with the provided default code:

Lets start by defining the input events via JSON within the template that AWS Lambda provides:

  "message" : "$input.params('message')"

Next, Lets create the Lambda function and pass our message event to the Lambda function:

// Index.js
'use strict';
console.log("Loading Function");
exports.handler =  async function(event, context, callback) {   
   var message = event.message;
   return message; 
   callback(null, message);

We can see here that an AWS Lambda function utilizes a handler that takes in 3 parameters event, context, and callback. Listed below are how these parameters function:

event: The information that is passed into the function by the user

context: Provides information on the invocation, execution environment, and function

callback(error, success): the callback returns either a success or an error message, a unique pattern when using Node.Js

Once the API Gateway is configured and published, you can access the API along with the Lambda function to see the results passing in a string message to the URL such as ?message="MY_MESSAGE

Writing an AWS Lambda function in Python

Python utilizes the Boto3 SDK for writing AWS Lambdas. since the runtime for python is very lightweight, it seems to be one of the best options to use when writing AWS Lambda functions. spinning up containers is much faster in Python compared to C# or Java. Assuming that our JSON input will be the same, below is the :

def lambda_handler(event, context):     
  message = event['message']       
  return {      
    'message' : message     

As you can see the Lambda for python is not much different from Node.JS the slight difference involves the callback.

Writing an AWS Lambda function in C#

C# utilizes the .NET Core framework and CLI to run Lambda functions. Amazon Lambda templates can be installed using the NuGet package manager. at the time of writing this article (May 14th, 2021) AWS Lambda supports .NET Core 2.1 and .NET Core 3.1. Since C# is a statically typed language utilizing large amounts of resources and memory, the cold starts of the AWS Lambda functions can be horrific. However, there are alternatives to deal with this cold start issue using a Lambda-warmer( to invoke AWS Lambda functions before they are called.