What is AWS Lambda Function
Lambda functions are computation services offered by AWS that let users run their code without provisioning the infrastructure. Instead, Lambda executes code on a high-availability compute infrastructure and manages all of the administration of the compute resources. As a result, it’s hassle-free to spin up the sources, scale them, and clean them up after the job is complete. With Lambda, one can run code for virtually any type of application or backend service.
Lambdas are great for running small code snippets. It supports multiple languages as well. You take the input and apply the business logic to produce the output. It can integrate with the other services as well. On the other hand, it is stateless, so it doesn’t remember anything from the previous execution. Each record processing will have its context. If you need to leverage the state, you must use external APIs or resources.
Lambdas scale up or down automatically based on the load or number of records it needs to process. One of the fantastic things about Lambda is its seamless integration with the other AWS services. Also, it’s pay for what you use.
The cost of a lambda function depends on three things.
- The number of times a Lambda function is executed. It has $.20 per million requests
- Allocated memory to function. It ranges from 128 MB to 3008 MB with an increment of 64 MB
- The execution duration of the function has an upper limit of 900 seconds. It has 100 ms increments.
Lambda integrates with so many different AWS services. As it’s an event-driven architecture, it can capture the events from any of the associated services. Some examples are
- IoT sensors sense some data, and it can trigger a lambda function with custom logic to compute the required intelligence.
- Kafka/MSK/SNS can trigger lambda whenever a new message is received if it subscribes to that specific topic.
- S3 services can also trigger lambda, such as when a new file is written to the bucket, it will trigger the lambda function for it.
So let’s create a new lambda function using a console. Once you select Lambda → functions → create function, you will see something as follows.
You will see the following self-explanatory options.
- Author from Scratch — Create a basic example like a hello world
- Use a blueprint — You can start with the sample code for one of the common use cases such as s3-get-object-python, dynamodb-process-stream, or kinesis-analytics-output
- Container Image- You can select a container image to deploy for your function
- Browse serverless app repository — You can deploy a sample lambda application from the AWS serverless application repository.
Let’s select the first one — author from scratch and click create function at the bottom right of the page. Once a Lambda function is created, you will see the page as follows.
On this page, you can configure the trigger and the destination of the function. It’s like the input and output of the function. In the second half of the page, you can write the code for your function and test it with the sample input. For now, when we try it, it will give us the ‘hello from lambda’ message.
Some Lambda use cases are file processing, Stream processing, and triggering a schedule. Lambda should not be used for stateful applications since it’s all about computation and not storing the state. It has optimal performance for stateless computations. It shouldn’t be used for the long-running code since the max execution limit is 15 mins. Finally, we need to avoid heavy analysis as a lambda’s max memory is 3 GB.
In summary, Lambda is serverless, so you don’t have to manage the infrastructure. Lambda functions are an excellent solution for stateless use cases with medium-level use cases. Lambda functions have great integration, making them easy to use for storage and streaming use cases.