Reading Time: 11 minutes
Serverless computing is a cloud-computing model in which the cloud provider dynamically manages the allocation of computing resources. AWS Lambda is a serverless computing platform that Amazon provides as part of Amazon Web Services (AWS). It’s also event-driven, meaning that Lambda only executes code in response to events and automatically manages the computing resources that the code requires. These capabilities allow developers to focus on an application’s business logic, rather than provisioning resources and managing access control.
AWS introduced Lambda in 2014 for the purpose of allowing AWS targets to execute code within milliseconds of a qualifying event. Lambda supports a variety of programming languages, including C#, Go, Java, Python, Ruby and Node.js as of 2018. It also supported custom runtimes by late 2018, allowing developers to run Lambda in their preferred language. AWS added provisioned concurrency for Lambda’s capabilities in 2019, which keeps functions initialized and able to respond to requests in less than 0.1 seconds. This feature is most beneficial for latency-sensitive applications like mobile back ends and synchronous APIs.
AWS specifically designed Lambda for uses such as uploading images and objects to Amazon S3, which stores objects through a web service interface. Lambda is also well-suited for updating DynamoDB tables, responding to inputs from internet of things (IoT) devices and updating DynamoDB tables. Developers can also use Lambda to automatically provision back-end services triggered by HTTP requests, which Lambda can also release to save resources. AWS API Gateway configures these requests, which it can also authorize and authenticate through AWS Cognito. Additional uses of Lambda include running native Linux programs in a secure environment via a supported runtime.
Amazon Linux AMI is Amazon’s own distribution of Linux based on Red Hat Enterprise Linux (RHEL). Each instance of Lambda is a container that AMI creates that includes up to 3008 Mb of RAM allocated in 64 Mb increments. These containers also have 512 MB of ephemeral storage, meaning the data is only stored for the duration of the instance. Additional features of a Lambda instance include an execution time configurable from 1 to 900 seconds.
The traditional approach to software design involves an application directly instantiating and controlling executable code. However, Lambda creates a package containing the required tasks and uploads it, typically to an S3 bucket. An AWS component such as DynamoDB, Kinesis or SQS then instructs Lambda to execute the code when a triggering event occurs.
Lambda runs each execution in a new environment, preventing access between consecutive runs. This architecture makes the instances essentially stateless since the program must store to an external media such as Dynamo DB or S3. The maximum size of an uncompressed Lambda package is 250 Mb with a maximum compressed size of 50 Mb.
Lambda uses several concepts that may be unfamiliar to software developers or have different meanings in serverless computing. These include the following:
- Lambda layer
- Log streams
- Event source
Software developers routinely use functions, but a Lambda function is distinctly different from other development environments. Lambda passes triggering events to functions, which may be a program or script. The function then uses that event as input and returns a response to the calling program.
Lambda functions are always ready to run once triggered, which is similar to the way formulas in spreadsheets work. In addition to code, each function also contains configuration information such as the function’s name and its resource requirements. Furthermore, Lambda functions have no dependency on the platform’s underlying infrastructure, allowing Lambda to launch as many instances of that function as it needs to handle incoming events.
Developers can associate their functions with specific AWS resources once they upload them to Lambda, which could be a DynamoDB table, Kinesis stream, Kinesis stream, Amazon S3 bucket or Amazon SNS notification. Lambda can then execute the function and manage those resources as needed to meet incoming requests.
AWS Lambda allows developers to add their own logic to AWS resources, making it easy to process data as it moves through the cloud. The first step in creating a function is to build the code and upload it to the Lambda platform, or build it directly in the Lambda console. Developers can then specify configuration options for the functions such as AWS Identity, Access Management (IAM) role, memory and timeout period. The next step is to specify the resource that will trigger that function. Lambda runs the function and launches the resource each time it changes, while continuing to manage incoming requests for the function.
A Lambda layer is an AWS component that allows developers to distribute functions for dependencies such as custom runtimes and libraries. Developers can also use a Lambda air to separate code and resource management.
A Lambda runtime allows functions to run in the same execution environment, even when they’re written in different languages. This capability allows developers to configure functions to match their preferred programming language.
A log stream lets Lambda developers annotate code with customized statements, generally for the purpose of analyzing the flow and performance of their functions.
An event source is an AWS service, which could be a standard service like Amazon SNS or a custom service. They trigger the execution of functions.
The following diagram shows the basic operation of Lambda in relation to other AWS services:
- The developer writes code in a language that Lambda supports, with C#, Go, Java and Python being some of the most common choices.
- An AWS service triggers Lambda code. The diagram above shows the major services, but there are many others.
- The developer uploads code to Lambda, along with details on the events that can trigger that code.
- Lambda executes the code when an AWS service generates a triggering event.
- The users is only charged when the code executes.
The events that can trigger the execution of code in Lambda include the addition, modification and deletion of data in a DynamoDB table. Modifications to objects in S3 buckets and notifications from Amazon SNS can also trigger Lambda code. Furthermore, HTTP GET and POST methods can trigger Lambda via API Gateway. In addition, developers can schedule Lambda code to execute at regular intervals, which they often use to process CloudTrail logs.
Lambda offers a number of features of interest to developers who are new to serverless computing.
Provisioned concurrency provides Lambda developers with greater control over their applications’ performance. This feature keeps functions ready to respond very quickly, making it particularly useful in cases where functions require a high degree of control over their start times. Developers can configure the specific concurrency they want for each application, typically increasing it as demand increases. Provisioned concurrently doesn’t require changes to code, so it’s an efficient way for latency-sensitive applications to maintain a desired performance level without managing resources.
Lambda allows code to securely access AWS services with AWS Identity and Access Management (IAM), which provides Lambda with built-in integration and SDK capability. It already runs within its own virtual private cloud (VPC) by default, but developers can also configure Lambda to access resources in other VPCs. This capability involves the use of custom security groups and access control lists, which is useful for compliance with regulations such as HIPAA, ISO, PCI and SOC. Lambda’s current compliance status is available on Lambda’s service page.
The Amazon Elastic File System (EFS) allows Lambda developers to securely read, write and store large volumes of data at low latency and on any scale. This process doesn’t require the developer to write code, which saves time and simplifies the development process. EFS is thus ideal for applications that involve backing up data, voting large reference models, machine learning, sharing files between serverless and container-based applications, and hosting web content.
Lambda has built-in fault tolerance, allowing it to maintain the desired compute capacity across multiple availability zones within each geographical region. This feature helps protect code against failures of individual servers or even entire data centers, providing Lambda applications with reliable and predictable performance. Lambda also has a high degree of availability for the service itself and its functions, since it doesn’t require scheduled maintenance.
Lambda doesn’t require developers to learn new languages, frameworks or other tools, since they can use code that Lambda natively supports in addition to third-party tools. Developers can also package code in a Lambda layer, including frameworks, libraries and Software Development Kits (SDKs). This approach allows developers to manage code and easily share it across multiple functions.
Lambda automatically manages its infrastructure, allowing developers to focus on building back-end services. Users don’t need to update the OS each time a patch is released or add new servers as usage increases, as is the case with other development platforms. Lambda also uses Amazon CloudWatch to automatically perform tasks such as logging and monitoring deployed code.
Lambda developers have access to fully managed connection pools for relational databases via Amazon RDS Proxy. It can manage thousands of database connections to relational databases, making it easy to develop serverless applications with database connectivity through the Amazon RDS and AWS Lambda consoles. RDS Proxy currently supports Aurora and MySQL.
Lambda can easily scale its operations since it only invokes code as needed to support requests. There is no theoretical limit to the number of requests it can handle, meaning Lambda’s performance remains consistent as the event frequency increases. Furthermore, the code’s stateless nature means that it doesn’t require lengthy delays for configuration and deployment.
Developers can use Lambda to create new services for their applications with the Amazon API Gateway. The triggers for these services may be existing Lambda API endpoints or endpoints from a custom API. Using Lambda rather than the client to process custom events allows developers to avoid the problems caused by variations in client platforms.
Lambda can also execute code in response to Amazon CloudFront events. CloudFront has a feature called [email protected] that runs code closer to edge users, thus improving performance and reducing latency. These events often consist of content requests from servers and viewers, making it easier to deliver personalized content to end users.
Orchestrate Multiple Functions
Developers can use Lambda to perform complex tasks by coordinating multiple functions. This process involves the use of AWS Step Functions to build workflows to trigger these functions through a series of error handling steps that Lambda can perform in sequence or parallel, including branching. Step Functions also allows developers to build state for processes for both applications and back ends.
The most significant advantages of Lambda over server-based platforms include its rapid scalability, which can range from a few requests per day to thousands per second without losing performance. Lambda’s ability to respond to events in milliseconds without the need to build infrastructure or provision servers is also a major benefit for developers. They can also configure Lambda with external event timers, making it useful for scheduling. Pricing is another significant advantage for Lambda, since users only pay for the compute time the code actually uses.
Developers can also monitor the performance of their Lambda code in real time with CloudWatch. Lambda also provides access to VPC and other resources useful for developers. Additional benefits of Lambda include its use as a WYSIWYG editor and plugin for Eclipse and Visual Studio.
Developers must bear a number of considerations in mind to obtain the best results from Lambda. For example, CloudWatch is the only tool developers can use to monitor functions since they only write their logs in CloudWatch. Lambda is also completely dependent on AWS for its infrastructure, so developers can’t install additional software. Furthermore, Lambda is best suited for large projects.
Performance considerations for Lambda include its memory volume, which can range from 128 to 1536 MB. However, the size of individual event requests shouldn’t exceed 128 Kb. The default concurrency limit for functions is 100 units, and the timeout for executing code is five minutes.
The specific tasks that Lambda is well suited for include the following:
- Real-time file processing
- Real-time stream processing
- Web applications
- Mobile back ends
- IoT back ends
Real-time File Processing
Amazon S3 can trigger Lambda, causing it to process data after it’s uploaded. Lambda functions can also directly connect to an existing EFS file system, which enables massively parallel shared access. Specific file processing tasks that Lambda can perform in real time include indexing files, filtering data, processing logs, creating thumbnail images and transcoding videos.
The following diagram shows the workflow for resizing images:
Fig. 2: Resizing Images
The image is created and uploaded to an S3 Bucket. This event which triggers the Lambda code, which resizes the image to the appropriate size based on the display device.
Real-time Stream Processing
Developers can also use Lambda and Amazon Kinesis to stream data in real time, which is a common requirement for tasks such as tracking application activity, processing transactions, analyzing click streams and generating metrics.
The following diagram shows the workflow for streaming social media:
Fig. 3: Streaming Social Media
The social media steam is loaded into Amazon Kinesis in real time, which triggers a Lambda function. This function executes code to generate trend data for each hashtag and stores it in DynamoDB. Users are then able to query this data immediately.
Developers can build web applications in Lambda by combining it with other services. These applications are readily available from multiple data centers since they don’t require administrators to perform scaling, backups and redundancy tasks.
The following diagram shows the process for building a weather application in Lambda:
Fig. 4: Building a Web App
The developer writes the front-end code for the weather app and hosts it in S3. The user clicks a link on a web page, which causes the app to make a representational state transfer (REST) API call to the app’s endpoint. This event triggers Lambda, which executes the code to retrieve weather information from DynamoDB and return it to the user.
Mobile Back Ends
Lambda allows developers to create mobile back ends with personalized app experiences. These uses rely on Amazon API Gateway to authenticate and process the API requests from the backend, which AWS Amplify can easily integrate into front ends for Android, iOS, React Native and Web.
The following diagram shows the workflow for a mobile backend that provides updates for a user’s social media account:
Fig. 5: Status Update for Social Media
The user posts a status update for a social media account, which the app uses to make a REST API call an endpoint. This event triggers a Lambda function to retrieve a list of the user’s friends for that social media platform. Amazon SNS generates messages for the status updates and sends them to the user’s friends.
IoT Back Ends
Lambda developers can build back ends to handle requests from IoT devices and third-party APIs. This process proceeds as follows:
Fig. 6: IoT Back End
An IoT device like a tractor sends sensor data to Kinesis, which streams the data and triggers a Lambda function. Lambda executes code to analyze trends in the sensor data and performs some action such as ordering a replacement part based on the results.
The following steps will help you get started with using AWS Lambda.
- Read the information at https://aws.amazon.com/lambda/getting-started/
- Create a Lambda account and sign on to it.
- Edit the code on the next page and click run to execute a simple Lambda program.
- Observe the output from the program.
Serverless computing is an increasingly popular cloud-computing model in which the service provider is responsible for maintaining servers, rather than the platform user. The provider also dynamically allocates and manages the servers’ resources, allowing developers to focus on developing code. AWS Lambda is a serverless computing service that allows developers to create functions in various languages. It also has its own runtime, so functions can run in the same execution environment.
Lambda code only executes when triggered by an event, and the user only pays for the processing resources needed to execute the code. Standard AWS services generate triggering events, but the developer can also create custom services to trigger Lambda code. Changes to the data in DynamoDB tables are one of the most common triggering events for Lambda.
Lambda also provides developers with tools to monitor and analyze the performance of their functions. These include custom logging statements to CloudWatch, which displays metrics for AWS services in real time. Developers can also use Lambda layers to distribute their code to libraries and custom runtimes.
Originally founded in 2007, Baytech has provided enterprise application development solutions for Fortune 500 companies in a wide range of industries, completing more than 100 separate projects.