AWS lambda 1

What is AWS Lambda Function: How it Works, Core Concepts, & Use Cases

What is AWS Lambda Function? 

AWS Lambda Function is a serverless compute service that permits you to run code without provisioning servers. Lambda runs your code on a high- availability compute infrastructure and performs all of the administration of the compute resources, including server and code maintenance, capacity provisioning and automatic scaling, code monitoring, and logging. With AWS Lambda, you can run code for any application or backend service. All you would like to undertake and do is supply your code which is written in one of the programming languages that Lambda supports. 

You can organize your code into AWS Lambda function using the lambda code editor. Lambda runs your function only when required and scales automatically, from a few requests per day to thousands of requests per second. You pay just for the compute time that you simply consume—there isn’t any charge when your code isn’t running. You can invoke your Lambda functions using the Lambda API, or events from other AWS services. 

AWS Lambda manages the compute fleet that provides a balance of memory, CPU, network, and other resources to run your code. Because Lambda manages these resources, you can not log in to any compute instances or customize any OS. Lambda performs operational and administrative activities on your behalf, including managing capacity, monitoring, and logging your Lambda functions. If you wish to manage your own compute resources, AWS has other compute services to fulfill your needs. 

What is the Serverless? 

Serverless is a software architecture design that takes advantage of cloud-managed services to make massively scalable and cost-efficient applications composed of small functions without developers wanting to give some thought to the design of the underlying infrastructure where their code runs. Serverless AWS architecture makes use of a Function as a Service(FaaS) together with other cloud-managed services. FaaS may be a specific form of services like AWS Lambda, Google Cloud Functions, and Azure Functions, that permits developers to deploy functions.

How Does AWS Lambda Work? 

Anyone can start using the AWS Lambda service by writing the code in lambda’s code editor or can upload a zip file of their code and need to mention the conditions to trigger the lambda function. The code executed on the lambda run time environment is called the lambda function. Lambda function supports different types of programming languages such as C, Python, Java, Go, and many more. Whenever an event or an API call happens which is configured as a lambda triggering condition then it triggers the lambda function which in return runs your code and completes your intended task.

Each AWS Lambda function runs in its own container. When a function is created, AWS Lambda packages it into a new container and then executes that container on a cluster of machines managed by AWS. Before the functions start executing, each function’s container is allocated with the necessary RAM and CPU capacity. One of the distinct properties of AWS Lambda is that many instances of the same function, or of different functions from the same AWS account, can be executed concurrently. 

AWS Lambda Function

Supported Languages in AWS Lambda

As of now, AWS Lambda doesn’t support all programming languages, but it does support a number of the most popular and familiar programming languages and runtimes. Here is the list of supported languages: 

  • Nodejs
  • 16.x(latest)
  • Nodejs 14.x
  • Nodejs 12.x
  • Python 3.9(latest)
  • Python 3.8 
  • Python 3.7 
  • Python 3.6
  • Java 11(latest)
  • Java 8
  • Ruby 2.7
  • .NET 6
  • .NET Core 3.1
  • Go 1.x

Core Concepts

Some of the main core concepts of AWS lambda are: 

  1. Function: Any custom code or libraries which we want to run when AWS Lambda is invoked. A function has code to process the events that you pass into the function or any other events sent by other aws services. 
  2. Trigger: A configuration that is used to invoke a Lambda function is called Trigger. Triggers include AWS services that you can configure to invoke a function and event source mappings. 
  3. Event: Any JSON-formatted document that contains data for a Lambda function to process is called an Event. When you invoke a function, you determine the structure and contents of the event but when aws services invoke then aws service defines the shape of the event. 
  4. Runtime: Runtime provides a programming language-specific environment where the aws lambda function can execute the code. You should configure your function to use a runtime that matches the programming language used in your code. 
  5. Layer: The layer contains custom code, packages, libraries, and other dependencies for our lambda function. Use can refer to these layers in your lambda function so that size of your lambda function is decreased. 

How to create AWS Lambda? 

Step1: Click on Sign to the Console to login into your AWS account. 

serverless aws

 

Step2: Enter your login credentials to log in to your AWS account console 

what is aws lambda function

Step3: Search for lambda service in the search box of the console home page and click on Lambda. 

what is aws lambda

Step4: Click on Create function. 

AWS Lambda Function

Step5: Choose Author from scratch or another option, fill in the basic information and click on Create function. 

aws lambda use cases

Step6: Enter the code you want and check the outputs. 

how does aws lambda works

AWS Lambda Use Cases

AWS Lambda can be used for a wide range of applications most often used for building serverless applications. Some of the AWS lambda use cases are:

  • To Perform real-time file processing and real-time stream processing. Can be used to manipulate data in ETL process. 
  • Used to automate daily tasks like taking daily backups. 
  • Can run backends using AWS Lambda and Amazon API Gateway to authenticate and process API’s. Used to send Notifications of AWS resources to Slack and Microsoft Teams channels. 
  • Build serverless backends using AWS Lambda to handle web, mobile and other API requests. Rapidly converting document formats and serving it either in a webpage or for download. 
  • Processing of file once landed in S3 bucket like image thumbnails.
  • To transfer cloudwatch logs to tools like elastic OpenSearch and kibana. 

AWS Lambda Advantages

  • It is a highly flexible tool. 
  • Allows you to run code without worrying about infrastructure. Costs only for compute time taken. 
  • Can Scale Automatically based on the requests.
  • Easy to integrate with other services like DynamoDB, S3, and API Gateway. AWS Lambda is so fast that it can execute your code within milliseconds. Allows running different types of programming languages. 
  • Can run same or different functions concurrently. 

AWS Lambda Limitations

AWS Lambda has many advantages but you need to be aware of a few “AWS Lambda Limitations” before you lambda in a production environment. 

  • Cold Start: There can be small latency between event and function start time. If your function has not been used or invoked for more than 20 minutes then the latency can be high. This makes it hard to rely on lambda for low-latency applications. 
  • Runtime: Lambda function will terminate after 15 minutes due to time-out. There is no option to increase this time so if your function takes more than 15mins then AWS Lambda is not suitable for your needs. 
  • Memory: AWS Lambda offers RAM memory from 128MB to 10,240MB only with increments of 1MB. Keep an eye on your function resource requirement while using lambda. 
  • Code Size: The size of your code should not be more than 250MB and the zipped file should not exceed 50MB. 
  • Concurrency: The default concurrency limit for the AWS lambda function in is 1000. However, you increase this limit by making a request to aws support. 

AWS Lambda Pricing

Lambda lets you run code with zero administration, you only pay for what you use and no additional charge for creating Lambda functions. There are additional charges for data transfer between Lambda and other AWS services. Generally aws lambda costing depends on the amount of duration it took to execute code, the amount of memory allocated to the function, and the number of requests your lambda function received. Duration is calculated from the time your code begins executing until it completes or terminates and rounded up to the nearest 1 millisecond. AWS Lambda costing free tier is not limited to 12 months, every single aws account gets 1,000,000(1Million) requests plus 400,000 GB-seconds per month under the free tier, beyond this limit starts billing. 

AWS lambda costing calculation example 

Suppose your lambda function runs 5 minutes every single day for 30 days. Assume 1GB of memory is used and 1 request per day. Total compute: 30 days x 300 GB-seconds (5 minutes) = 9,000 GB-seconds 

Total requests: 30(days) x 1(requests per day) = 30 requests 

This is under the free tier limit only no there will be no cost incurred for your lambda. 

Suppose your lambda function runs 10 minutes every single hour for 30 days. Assume 1GB of memory is used and 100000 requests per day. The cost for 1GB of memory used is $0.0000000167 per millisecond. For every 1Million requests $0.20 

Total compute: 30 days x 600 GB-seconds (10 minutes) x 24(times invoke per day) = 4,32,000 GB-seconds Total compute cost = (4,32,000 – 4,00,000[free tier limit]) x $0.0000000167 => 32,000 x $0.0000000167 Total Compute cost = $0.000534 

Total requests: 30 days x 1,000,000 request per day = 3,000,000 requests Total request cost = 3-1(free tier limit) x $0.20 => 2 x $0.20 

Total request cost = $0.40 

Total AWS Lambda Pricing = Total Compute cost + Total request cost => $0.400534

Conclusion

Still, confused about what is AWS Lambda Function? Well, let’s understand it in brief here. AWS Lambda Function is a serverless compute service so you don’t need to worry about which AWS resources to launch, or how to manage them. It’s one of the most cost-optimized services in AWS which bills you based on the number of milliseconds the service run. AWS Lambda can be considered a key component while building serverless applications. It also integrates with many other AWS services and, together with API Gateway, DynamoDB and RDS form the basis for Serverless solutions for those using AWS.Â