In this article, you will see the AWS lambda overview and its core concepts, This tutorial is all about theory because before working with the AWS lambda function, we must have knowledge of AWS lambda and its concepts in a later tutorial we will explore more about the AWS lambda function along with practical.
AWS Lambda is one of the most popular serverless computing services. Basically, It is used to execute a piece of code written in any programming language including Nodejs, Python, Java, .NET, Ruby, Go, and any custom runtime.
Let’s go detail into about the AWS lambda and its concepts.
Headings of Contents
What is AWS Lambda?
Before moving on to AWS Lambda, let’s look at a simple example. If you have come to this article to learn AWS Lambda then you must have an idea about the server.
When you develop an application as a developer, During the development of the application, you test the application’s functionality on your local machine which is also called localhost or local environment. But in the end, You will have to deploy that application on the production server for your end users.
To deploy the application, you will configure the server by installing some software like Apache2, Nginx, MySQL, etc. Basically, here you managed the server by installing or uninstalling application software as per your application requirement, with everything in your hand.
But AWS Lambda is different from them.AWS Lambda is serverless, event-driven computing service that allows you to run your code for virtually any type of application without provisioning or managing servers.
You can trigger AWS Lambda with over 200 AWS services and SaaS (Software as a Service) Applications. You will pay for what you will use.
AWS Lambda runs your code on high-availability compute infrastructure and all the administration of compute resources including server, operating system maintenance, capacity provisioning, automatic scaling, and logging.
AWS will not charge when your code is not running.
When to use AWS Lambda?
AWS Lambda is an ideal computing service that is used for application scenario that needs to scale up rapidly and scale down when not in demand.
AWS Lambda is used for:
- File Processing:- Use AWS S3 ( Simple Storage Service ) to trigger the AWS lambda function to process the file in a real-time after the data is uploaded.
- Stream Processing:- Use AWS kinesis to process real-time data for application activity tracking, transaction order processing, IoT devices, etc.
- Web applications:- Combine AWS lambda with other AWS services to build a powerful web application that automatically scales up and down in highly available configurations across multiple data centers.
- Mobile Backends:- Build backend using AWS Lambda and Amazon API Gateway to authenticate and process API requests.
When using AWS lambda, you are responsible for only your code.AWS Lambda manages compute fleet that offers a balance of memory, Network, CPU, and other resources to execute your code.
Concepts of AWS Lambda
These are some important concepts that you should know about especially when you are working with AWS Lambda Function.
AWS Lambda Function
The lambda function is a priniciple resource of AWS lambda service.In other words, A function is a lambda resource that you can invoke to run or execute your code in lambda. A function has business logic to process event that passes into the function or other service sent to the function.
Trigger
A trigger is a resource or configuration that invokes a lambda function. The trigger includes AWS services that you can configure to invoke the AWS lambda function.
AWS Lambda Runtimes
AWS Lambda supports multiple languages through the use of runtimes. A runtime provides languages specific environment that relays invocation events, context information, and response between AWS lambda and function. You can runtimes that lambda provides or you can build your own.
AWS Lambda supports the following runtimes:
👉 Learn more about runtimes:- Click Here
Handler
The lambda function handler is a method in your code that is responsible for processing events. When your function is invoked, Lambda runs the handler method.
This is the basic syntax that you can use to create a handler function in Python.
def handler_name(event, context):
.....
.....
return some_value
Naming
A function handler can be any name; However, the default name in the console is lambda_function.lambda_handler where lambda_function indicates the file_name where code is stored and lambda_handler indicates the name of the method that is stored into lambda_function filename.
If you want to create a function name with a different filename or function handler name then must edit the default handler name.
Event
It is the first argument of the function handler that is an event object. It contains information about the event. It is a JSON formatted document that contains data for the lambda function to process. The lambda runtimes convert that JSON document to an object and pass it to your code so that you can process them. It is usually a Python dictionary type. It can be also list, tuple, str, float, and NoneType.
You can explore extra information about event objects from here
Context
It is the second argument of the handler function. A context object is passed to your function or handler by Lambda at runtime. The context object provides methods and properties that provide information about invocation, function, and runtime environment.
You can explore more about context from here
Layers
Layers in aws are features that allow us to package libraries and other dependencies that we are about to use in AWS lambda functions. Using the layers reduces the uploaded deployment archive and makes it faster to deploy code.
Layers is a .zip file that contains additional code or data. A Layer can contain libraries, custom runtime, data, or configuration files. You can use layers only with the AWS lambda function in the form of a zip file archive.
Environment Variables
An Environment Variables is a pair of strings that are stored in functions’ version-specific configurations. The runtimes make your environment variable available in your code and set additional environment variables that contain information about the function and invocation request. AWS lambda environment variables will not be evaluated before function invocation.
Version
The version in aws lambda is one of the greatest features to manage the deployment of AWS lambda functions. For example, You can publish a new version of your function for beta testing without affecting users of the stable production version.
AWS Lambda creates a new version of the function each time when you publish the function. The new version is a copy of the unpublished version of the function. The unpublished version is named $LATEST.
Alias
You can create one or more alias for your function. An AWS lambda alias is a kind f pointer to a specific function version. Users can access the function version by using the AWS function ARN ( Amazon Resource Name ).
Lambda Function URL
A function URL is a dedicated HTTP endpoint for your lambda function. You can easily create a function URL for the lambda function using the lambda console or Lambda API. When you create a function URL for lambda function, lambda automatically generates a unique endpoint URL for you. It url endpoint will never change.
The format of the function URL will be:
https://<url-id>.lambda-url.<region>.on.aws
AWS Lambda Limitations
AWS Lambda has some limitations which you should remember.
- Function Memory Allocation:- The memory allocation of the function will be between 128 MB to 10,240 MB.
- Function Execution timeout:- The maximum execution time of the function must be less or less than 15 minutes or 900 seconds.
- Functions Environment Variables:- The size of environment variables of the function should be up to 4KB.
- Layers:- A function can have up to five layers.
- Deployment Package Size:- 50MB ( Zipped, Direct Upload), 250MB ( Unzip ). Remember, this size will be applied to all the files including layers and custom runtime.
Deploying AWS Lambda Function
You can deploy your AWS lambda function by creating a zip file archive and container image.
Zip file archives:
A .zip file archive includes your function code and its dependencies. When you author your AWS lambda function code using the AWS management console or a toolkit, Lambda automatically creates .zip files of your code.
When you create an aws lambda function using AWS CLI ( Command Line Interface ), AWS Lambda API, and SDKs, You must have to create a deployment package. To deploy your function’s code you upload the deployment package to AWS S3 or your local machine.
Container Images:
You have another method to deploy your function code using container images. You can package your function code and its dependencies using tools such as docker command line tools and then upload the image to AWS ACR ( Amazon Container Repository ).
Invoking Lambda Function
You can invoke the AWS lambda function using the following ways:
- Using AWS Management Console
- Using AWS CLI ( Command Line Interface )
- Using HTTP endpoint
- Using AWS SDKs
You can also configure other AWS services to invoke your lambda function in response to the event for example, Amazon Storage Service ( S3 ) can invoke your function whenever a new object will be uploaded into your S3 bucket.
When you will invoke your AWS lambda function you can choose it to invoke a function synchronously or asynchronously. When you choose synchronous invocation, you have to wait for the function to process the event and then return a response. With asynchronous invocation, AWS lambda queue the function to process the event and return a response immediately.
This is only the theoretical tutorial, In a later tutorial we will see a complete practical tutorial with the help of a proper guide and screenshots.
You can follow the following tutorial guides to enhance your AWS lambda function skills practically.
Conclusion
I hope this AWS lambda overview tutorial was helpful to understand the basics of the AWS lambda function because before doing practical you must have knowledge about the AWS lambda theoretical. We will update this article from time to time whenever we will get extra useful information on AWS Lambda.
If you found this article helpful please share it with someone who wants to learn AWS lambda function.
Happy Coding….