AWS Lambda
Introduction
AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services (AWS). It is a computing service that lets you run code without provisioning or managing servers. You can run your code for virtually any type of application or backend service - all with zero administration.
How it Works
When an AWS Lambda function is invoked, AWS Lambda launches an execution context based on the configuration settings you provide. The execution context is a temporary runtime environment that initializes any external dependencies of your Lambda function code, like database connections or HTTP endpoints.
The code runs in response to specific AWS-defined events or triggers like changes to data in an S3 bucket or a new row being added to a DynamoDB table. AWS Lambda supports synchronous and asynchronous invocation of a Lambda function.
Workflow:
Trigger: When a specified event occurs, the service triggers your function.
Load: AWS Lambda prepares your function code and runs it.
Execute: Your function code runs on AWS infrastructure to process the event.
Scale: AWS Lambda runs your function code more often if the event rate increases.
Runtimes
A runtime is a language-specific environment that runs a Lambda function's code. The runtime environment includes an operating system, a web server, and a runtime (for example, Java, Python, or Node.js).
AWS Lambda supports the following runtime versions:
Node.js: v14.x, v16.x, v18.x
Java: Corretto 8, Corretto 11
Python: 3.6, 3.7, 3.8, 3.9
.NET Core: 2.1, 3.1
Go: 1.x
Ruby: 2.5, 2.7
PowerShell Core: 7.0
Custom Runtime: You can also provide your own runtime in a function's deployment package.
Limitations
AWS Lambda is designed to allow your applications to scale quickly and handle variable workloads, but there are some limitations:
Memory: You can allocate from 128 MB to 10,240 MB to your Lambda function.
Timeout: The maximum execution time for a function is 900 seconds (15 minutes).
Deployment Package: The total unzipped size of the function and all its layers cannot exceed 250 MB. The total size of all deployment packages that can be uploaded per region is 75 GB.
Concurrency: Each AWS account has a total concurrent execution limit per region initially set to 1,000.
Payload Size: The payload (request and response) size for synchronous invocation is 6 MB, and for asynchronous invocation, it is 256 KB.
Use Cases
AWS Lambda can be used for a multitude of tasks:
Data Transformation: AWS Lambda can process data immediately after uploading and transform it on the fly. For example, clean, validate and transform data on the fly as it loads into data lakes.
Real-time File Processing: AWS Lambda can automatically process new files uploaded to Amazon S3 buckets, processing tasks like image resizing, PDF generation, or data validation.
Real-Time Stream Processing: AWS Lambda can process real-time data streams, like in a serverless MapReduce architecture. For example, analyze real-time clickstream data for personalized user experiences.
Chatbots and Virtual Assistants: You can use Lambda to implement the logic of chatbots and virtual assistants, integrating with services like Amazon Lex.
Microservices: AWS Lambda is an ideal platform for building microservices. Each microservice can be a standalone Lambda function.
Hands-On - Create a Simple Lambda Function
Now, we'll create a simple Lambda function and run a test invocation.
Step 1: Creating a Lambda Function
Open the AWS Lambda console at AWS Lambda.
Click on Create function.
You will be given an option to Author from scratch or Use a blueprint. For this time, select the Author from scratch.
Fill out the basic information:
Function name: Enter a name for your function, e.g., "HelloServerless".
Runtime: Choose the runtime that corresponds to the language of your code. For this example, let's select Node.js 18.x.
Under Permissions, you can choose an existing role or create a new one. AWS Lambda requires these permissions to run your function on your behalf.
Click on Create function.
Step 2: Adding Function Code
Now that you've created your function, you need to add some code for it to execute.
Scroll down to the "Function code" section, and In the code editor, you'll see some boilerplate code. For this example, replace that with:
After entering the code, click "Deploy" at the top of the page to save your changes.
Step 3: Testing Your Lambda Function
You can now test the function with the code created and added. At the top of your function's page, click on Test.
A new window will pop up, asking for you to configure a test event. You can leave everything as default for this simple function.
Click Create.
Now, with your test event selected in the dropdown menu, click Test again.
Your function will be executed, and you'll see the execution result displayed. This includes logs and the returned result from your function.
Congratulations, you have successfully created a simple AWS Lambda function and performed a test invocation!
This simple example only scratches the surface of what AWS Lambda can do. As you gain more experience, you can create more complex functions, add triggers, and integrate your functions with other AWS services.
Last updated
Was this helpful?