Recently, Lambda has gained wide popularity, thanks to AWS. Even with this spread, folks still use Lambda and serverless interchangeably. In this AWS Lambda Tutorial, we will shed some light on AWS Lambda, including its features and ties to serverless architecture, how to create it, and when to use it.
Contents
Describing AWS Lambda
AWS Lambda is an event-driven, high-scale, provision-free, serverless computing platform service Amazon offers as part of Amazon Web Services. It is based on the functions, runs code in response to events, automatically manages computer resources required, and is used only for the computing layer of the serverless application. AWS Lambda was solely built to develop event-driven applications that different events in AWS can trigger. You can learn AWS Lambda for free with Great Learning Academy.
Features
- Runs code without provisioning or managing infrastructure. Users can write code and simply upload it as a .zip file or container image.
- Responds automatically to code execution requests of any scale, from half a dozen events per day to hundreds of thousands every second.
- Saves costs by paying only for the compute time per millisecond rather than provisioning infrastructure upfront for peak capacity.
- Optimizes code execution performance and time with correct function memory size. Responds to high demand in two-digit milliseconds with provisioned concurrency.
Components
There can be multiple simultaneous events. In those cases, Lambda simply spins up multiple replicas of the functions to handle the events, meaning, Lambda can be defined as a type of function as a service (FaaS).
Three of AWS Lambda include:
- A function. The coding part that performs the tasks.
- A configuration. The configuration defines how the functions are executed.
- An event source. This is an optional component. It is an event that triggers the function. Users can choose to either trigger with different AWS services or a third-party service. You can invoke the function of an event from that source when you specify an event source.
Running a Lambda Function
You specify a runtime environment to run your code when you configure a lambda function. Each environment proffers a unique set of binaries available for use in your code, depending upon the language you use. You can also package any libraries and binaries as long as you can use them in your environment. All lambda environments are based on Amazon Linux AMI.
Currently available environments include:
- NodeJS
- Python
- Go
- Java
- Ruby
- .Net
- C#
We only focus on the code when running a lambda function because AWS manages capacity and all the updates. AWS Lambda can be invoked synchronously through ResponseRequest InvocationType and asynchronously through EventInvocationType.
Event Source
AWS Lambda can be triggered through the Invoke API, but however, we recommend you to trigger lambda using event sources from within AWS.
There are two models supporting invocation:
- Push. Push gets triggered by other events such as API gateway, a new object in S3, or Amazon Alexa.
- Pull. In Pull, the lambda function polls an event source for new objects, for example, DynamoDB and Amazon Kinesis.
Lambda Configurations
A few configuration settings can be used with lambda functions. They are:
- Memory dial. It not only controls the memory but also affects CPU and network resource allocation to the function.
- Version or Aliases. These revert functions back to older versions. It plays a key role in implementing a deployment strategy like blue/green or segregating production from lower environments.
- IAM Role. It permits lambda functions to interact with other lambda services and APIs.
- Lambda functions permission. Decides which model event sources are allowed to invoke in lambda functions.
- Network configuration for outbound connectivity. You can choose between the two available choices:
- Default. Allows internet connectivity to private resources in VPC services.
- VPC. Allows the function to be provisioned in the VPC and use an ENI. It can then be attached security like in any other ENIs.
- Environment variables. These are used to dynamically inject values consumed by code. One of the 12-factor app methodologies around cloud-native applications is to separate code from the configuration.
- Dead letter queue. A queue where all the failed invocation events are stacked can be either an SNS topic or SQS.
- Timeouts. Defines the allowed amount of time a function can run before it is timed out.
Use Cases
- Processes data at scale. Allows you to execute at your expected capacity and as you need it. It scales to match the data volume automatically and enables custom event triggers.
- Runs interactive web and mobile backends. Capable of combining AWS Lambda with other AWS services to build secure, stable, and scalable online experiences.
- Enables strong ML insights. It preprocesses data prior to sending it to the machine learning (ML) models. AWS Lambda can handle infrastructure management and provisioning to simplify scaling using Amazon Elastic File System (EFS) access.
- Creates event-driven applications. It develops event-driven functions to communicate between decoupled services easily and reduces costs by running applications during peak demand without crashing or over-provisioning resources.
Creating AWS Lamba
There are a few approaches to creating a lambda function in AWS, but the common ways include creating lambda functions with a console. However, the console must only be used when testing in dev. It is best practice to automate the deployment of lambda for production.
A few third-party tools, such as Terraform, set up automation, but because we are explicitly focusing on AWS service, AWS suggests using Serverless Application Model (SAM) to carry out these tasks. SAM is almost developed on top of AWS CloudFormation. The template is similar to that of the CloudFormation template, except that it has a transform block that explicitly mentions the SAM template instead of the normal CloudFormation template. You can look at some examples of templates in the AWS Lambda Tutorial.
Understanding Serverless
You have to understand serverless architecture to understand AWS Lambda fully. In general, unlike any other applications, serverless applications don’t require any provisioning of servers to run. You don’t have to worry about OS setup, patching, and scaling of servers that you will have to consider while running your applications on the physical server when you run a serverless application.
Characteristics of Serverless applications or platforms:
- No server management
- Flexible scaling
- No idle capacity
- High availability
Serverless applications comprise many components layers. They include:
- Compute
- API
- Storage
- Interprocess messaging
- Orchestration
Being a cloud provider, AWS offers services used for each of these components to make up a serverless architecture. AWS Lambda contributes to this.
Wrapping Up
As we learned in this article, AWS Lambda is an event-driven, serverless compute service to virtually run your code for any kind of application or back-end service without either provision or managing servers. It is good to pay only for what you use.
You can learn more about serverless infrastructure, automated management and provisioning, and the basics of running code on AWS Lambda and other cloud servers through Great Learning’s Cloud Computing courses online. Register today and explore Cloud Computing.