Serverless computing is a cloud-based service where the provider manages the server, and dynamically allocates compute storage and resources as needed to execute each line of code.
With serverless computing, the service provider takes care of the infrastructure which means all your team needs to do is write code.
What is AWS Lambda?
AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). It lets you run code without provisioning or managing servers.
It allows you to create functions and self-contained applications which can be uploaded to AWS Lambda and executed efficiently and flexibly.
Lambda functions can perform any kind of computing task, from serving web pages and processing streams of data to calling APIs and integrating with other AWS services.
Components of Lambda Function
Three components comprise AWS Lambda:
A function. This is the actual code that performs the task.
A configuration. This specifies how your function is executed.
An event source (optional). This is the event that triggers the function. You can trigger with several AWS services or a third-party service.
When you specify an event source, your function is invoked when an event from that source occurs. The diagram below shows what this looks like:
How does AWS Lambda work?
When a function is created, Lambda packages it into a new container and then executes that container on a multi-tenant cluster of machines managed by AWS.
Before a function is executed, each function’s container is allocated its necessary RAM and CPU capacity.
After the function is executed, the RAM allocated at the beginning is multiplied by the amount of time the function spent running. Based on this calculation, the customer gets charged.
Also, many instances of the same function can be executed concurrently. This makes AWS Lambda a good fit for deploying highly scalable cloud computing solutions.
Creating a Lambda Function
There are a few ways to create a lambda function in AWS, but the most common way to create it is with the console but this method should only be used if testing in dev.
For production, it is best practice to automate the deployment of the lambda. There are some third-party tools to set up automation, such as Terraform, but since we are specifically talking about an AWS service, AWS recommends using Serverless Application Model (SAM) for this task which is pretty much built on top of AWS CloudFormation.
Benefits of using AWS Lambda
AWS Lambda has a few unique advantages over maintaining your own servers in the cloud. The main ones are:
Pay per use - In AWS Lambda, you pay only for the compute your functions use, plus any network traffic generated
Automatic scaling - AWS Lambda creates the instances of your function as they are requested. There is no pre-scaled pool and at the same time, your functions are available whenever the load increases or decreases.
High Availability and Fault Tolerance - AWS Lambda is designed to be highly available and fault-tolerant. Functions are automatically replicated across multiple availability zones within a region, ensuring resilience and minimizing the risk of downtime.
Tight integration with other AWS products - AWS Lambda integrates with services like DynamoDB, S3 and API Gateway, allowing you to build functionally complete applications within your Lambda functions.
Fully managed infrastructure - Since the Lambda functions run on the managed AWS infrastructure, you don’t need to think about the underlying servers. This also results in significant savings on operational tasks that would have otherwise been performed manually by DevOps.
Event-Driven Architecture - AWS Lambda allows you to build applications using an event-driven architecture. You can trigger Lambda functions in response to events from various AWS services, such as changes to data in Amazon S3, updates to a DynamoDB table, or events from AWS Step Functions.
Limitations of AWS Lambda
Execution Duration: By default, AWS Lambda functions have a maximum execution duration of 900 seconds. If your function needs to run for a longer duration, you need to implement services like Step Functions.
Temporary Storage: Lambda functions have a limited amount of temporary disk space available. This storage is not meant for long-term persistence and should only be used for temporary data.
Stateless Execution: Lambda functions are designed to be stateless, which means they do not retain any in-memory state between invocations.
Concurrency Limits: AWS Lambda has concurrency limits that determine the maximum number of function invocations that can be processed simultaneously. These limits vary based on the AWS Region and account level, and you may need to request a limit increase for high-volume applications.
Cold Start Latency: If a Lambda function is inactive for some time, it may experience a cold start when invoked again. Cold starts can introduce additional latency due to the need to initialize the execution environment.
VPC Restrictions: When you run a Lambda function inside a Virtual Private Cloud (VPC), there are certain limitations. For example, the function cannot access the internet directly, and it requires appropriate configuration of subnets, security groups, and network access control lists (ACLs).
Debugging and Monitoring: Debugging and monitoring serverless applications are more difficult than traditional architectures. Although AWS provides services like CloudWatch Logs and X-Ray, the debugging experience may not be as straightforward as debugging locally.
AWS Lambda pricing
Several AWS Lambda executions are included with the AWS Free Tier with every AWS account.
Unlike some other services, the Lambda free tier isn’t limited to 12 months. Both existing and new accounts get 1 million AWS Lambda requests plus 400,000 GB seconds per month.
Beyond the free tier, the pricing for AWS Lambda is as follows:
$0.20 per 1M requests
The free tier includes 1M requests.
Function memory and run time
$0.0000166667 per GB-second
The free tier includes 400,000 GB seconds.
Inbound network traffic
Outbound network traffic - within the same AWS region
Outbound network traffic - to other AWS regions
Outbound network traffic - to the public internet
Lower pricing per GB applies starting at 10TB/month.
Amazon API Gateway
$3.50 per 1M requests
If you are using API Gateway with Lambda functions. Lower pricing from 333M requests per month and up.