Cloud lambda functions, a new computing concept

by Mike Sweetman
12 December 2019

In cloud systems, a variety of services and computation models are springing up like mushrooms in the warm and rainy autumn ... This is natural, evolutionary growth related to the development of the cloud systems themselves, as well as the consumers of these services. We have long moved away from the basic trinity of cloud services - IaaS, PaaS, SaaS, and have many types of "Something" as a Service. 

Today a very fashionable direction of so-called serverless computing has appeared. They are called lambda functions (similar to programming languages). One of the first widely known such cloud system is  Amazon Lambda, named exactly what it is.   We will consider in this text what these lambda functions are, why they are needed and why lambda functions have also appeared in cloud systems. We will write specifically about how Amazon Lambda works.

What is a lambda function?

A lambda expression in programming is a special syntax for defining functional objects, borrowed from a λ-calculus. It is usually used to declare anonymous functions at the place of their use, and usually allows closure to the lexical context in which this expression is used.

As wiki defines lambda functions (or anonymous functions): “Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus, in which all functions are anonymous, in 1936, before electronic computers. In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions.”

It seems all modern computer languages support lambda functions, so the list of compatible languages is very long. Really long: C (non-standard extension), Objective-C, Objective-C++, C++ (since C++11), C#, D, Dart, Delphi, PascalABC.NET, Elixir, Erlang, Go, Haskell, Haxe, Java, JavaScript, Julia, Lisp, Scheme, Clojure, Lua, Mathematica, MATLAB, Octave, Maxima, OCaml, F#, Standard ML, Next Generation Shell, Perl, Perl 5, PHP, Prolog's dialects, Logtalk, Python, R, Raku, Ruby, Rust, Scala, Smalltalk, Swift, Tcl, Visual Basic .NET, Z Shell. Plus you can check for the presence of lambda functions in your favorite programming languages.  For example:  

For Python the lambda function will look like:

>>> foo = lambda x: x*x
>>> print(foo(10))
100

Or in Java lambda functions will look like

// A Java program to demonstrate simple lambda expressions 
import java.util.ArrayList; 
class Test 
{ 
    public static void main(String args[]) 
    { 
        // Creating an ArrayList with elements 
        // {1, 2, 3, 4} 
        ArrayList<Integer> arrL = new ArrayList<Integer>(); 
        arrL.add(1); 
        arrL.add(2); 
        arrL.add(3); 
        arrL.add(4); 
  
        // Using lambda expression to print all elements 
        // of arrL 
        arrL.forEach(n -> System.out.println(n)); 
  
        // Using lambda expression to print even elements 
        // of arrL 
        arrL.forEach(n -> { if (n%2 == 0) System.out.println(n); }); 
    } 
} 

Serverless computing and lambda functions

Serverless computing is a cloud computing model in which the platform dynamically manages the allocation of machine resources. Sometimes serverless computing is also called Function as a Service (FaaS) because the unit of code is a function that is executed by the platform. In fact, to execute each request, a separate container is quickly created, and then destroyed after execution.

Of course, serverless computing requires hardware, so this term should not be taken literally. This name is used because users of such a platform do not have to deal with setting up servers to run code. All server settings and scheduling of computing resources are hidden from users and are managed by the platform. Serverless code can be part of applications built on traditional architecture.

Function as a service (FaaS) is an architectural template that assumes the ability to call an instance of the control code without the need for server and server application management.

The idea is that the server part is divided into a set of stateless functions for processing events. These events may include incoming HTTP requests, the appearance of new messages in the queue, the occurrence of the time specified in the scheduler, and other events. The state, in this case, is understood in the context of deployment. This means that the result of the function should not depend on the state of the server’s memory and the contents of the local file system (except for the transferred call parameters). With these restrictions, horizontal scaling is performed automatically by the provider. 

The provider can use any computing device currently available that provides the required level of performance. Also, as a rule, the execution time of a function is forcibly limited to several minutes, since the lambda function implies a small and completed action.

How do lambda functions work in the cloud?

In general, cloud lambda functions work in the same manner as lambda functions in programming languages. When an application runs a lambda function in the cloud then some part of the code works on the cloud side with minimal runtime environment and the user pays only for running time.

The main idea of using lambda function is based on HTTP API endpoints which are accessible by applications, mobile, and IoT devices.

Fig. 1. General lamda functions in the cloud systems

General lamda functions in the cloud systems

Triggers are events that trigger lambdas in the cloud. According to the lambda trigger, the function in the cloud is executed, returns the result of its work and terminates until the next call. Further in the text, you will see the triggers in the Amazon Lambda.

There are many cloud vendors that provide FaaS, or lambda functions. For instance, AWS Lambda was introduced by Amazon in 2014. This service was the first public cloud infrastructure provider for serverless computing. The main advantage of this approach is price because the “pay-as-you-go” model is applied. Users pay only when running the code.

Google provides cloud functions such as “Event-driven serverless compute platform”: Cloud Functions. This approach allows:

  • The simplest way to run your code in the cloud
  • Automatically scales, highly available and fault-tolerant
  • No servers to provision, manage, or patch
  • Pay only for running the code
  • Connects and extends other cloud services

Microsoft Azure also provides such service: “Azure Functions is an event-driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in Azure or third party service as well as on-premises systems.”

IBM offers Apache OpenWhisk is an open-source serverless runtime. OpenWhisk has support for Node.js, Python, Java, and Swift.

Amazon Lambda

Since Amazon Lambda was the first system for FaaS computing and is now widespread enough for solving intended tasks, let's analyze this service in more detail. The remaining service providers have some differences in the operation of such services, but the idea remains approximately the same.

AWS Lambda provides built-in support for Java, Go, PowerShell, Node.js, C #, Python, and Ruby, and also provides the Runtime API with which you can use any additional programming language to create functions.

The code that runs in AWS Lambda is loaded as a function of Lambda. Each function has corresponding configuration information, such as name, description, entry point, etc.

Very important point !!! The code should be written without state preservation, that is, it should not depend on specific computing infrastructure. Access to the local file system, child processes, and similar artifacts cannot be stored longer than the time it takes for the request itself to be executed.

How can you put your Lambda function on Amazon:

  1. The Lambda console. When using Node.js or Python, you can write the function code using the code editor in the AWS Lambda console, which allows you to create and test functions, as well as view the results of their execution as in the cloud IDE.
  2. You can archive the code (and any dependent libraries) in ZIP format and download the archive from the local computer using the AWS Lambda console, or specify this ZIP file in Amazon S3. The size of the downloaded files should not exceed 50 MB. You can use the AWS Eclipse plug-in to create and deploy Lambda functions in Java. You can use the Visual Studio plug-in to create and deploy Lambda functions in C # and Node.js.
  3. You can create an AWS Lambda function using the Lambda command-line interface. To do this, you need to archive the code and dependent libraries in ZIP format and download the archive from your computer using AWS CLI (or via ZIP in Amazon S3). The size of the downloaded files likewise should not exceed 50 MB.

Fig. 2. AWS lambda cloud console
Image source

AWS lambda cloud console

Services That Lambda Reads Events From:

  • Amazon Kinesis
  • Amazon DynamoDB
  • Amazon Simple Queue Service

Services That Invoke Lambda Functions Synchronously:

  • Elastic Load Balancing (Application Load Balancer)
  • Amazon Cognito
  • Amazon Lex
  • Amazon Alexa
  • Amazon API Gateway
  • Amazon CloudFront (Lambda@Edge)
  • Amazon Kinesis Data Firehose
  • AWS Step Functions

Services That Invoke Lambda Functions Asynchronously:

  • Amazon Simple Storage Service
  • Amazon Simple Notification Service
  • Amazon Simple Email Service
  • AWS CloudFormation
  • Amazon CloudWatch Logs
  • Amazon CloudWatch Events
  • AWS CodeCommit
  • AWS Config
  • AWS IoT Events

AWS Lambda automatically monitors the Lambda function on behalf of the user, sending real-time metrics to Amazon CloudWatch in real-time, including:

  • total number of requests
  • used parallel execution resource at the account and function levels
  • delay
  • error rate
  • the number of requests missed due to restrictions

To show how simply you can use Amazon Lambda let’s see Python’s simple code example:

# Python code for Amazon Lambda is here
def handler_name(event, context): 
    # Place your Python code here
    ...
    return some_value

Where event – AWS Lambda uses this parameter to pass in event data to the handler. And context is AWS Lambda uses this parameter to provide runtime information to your handler. 

Amazon also provides an effective mechanism for testing and debugging Lambda functions.

Each Lambda function can run in the cloud up to 15 minutes, then the timeout will appear.

You can look at the availability of the Amazon Lambda service in a special table, and decide which of the locations will suit you best.

You can read about the pros and cons of Amazon Lambda in the following articles: The Pros and Cons of AWS Lambda and Pros and Cons of AWS Lambda.

Conclusion

Well, what can be said in the conclusion of this article? We have a new, effective, inexpensive, and powerful tool for processing actions in the cloud. The concept of serverless computing is based on the well-known paradigm of lambda functions. This concept fits very well in cloud computing. You do not need to keep a working instance full time and spend money on it if you do not have user requests. The lambda function in the cloud will start upon request at the right time, quickly complete its task, send the result of its work further and go into a frozen state awaiting the next request. This is a profitable and efficient method of calculation both from a technical point of view and from an economic point of view. Use cloud lambda functions in your wonderful systems and develop your capabilities.
 

Related articles.