9 min read

What is AWS Lambda?

June 3, 2021

AWS_Lambda

AWS Lambda is a service that allows you to run code and containers without the need to set up, manage or provision servers. Lambda runs your code on highly available compute cloud infrastructure via Functions.  Lambda takes care of server and OS maintenance, scaling, code monitoring and activity/error logging.

Lambda functions support multiple languages which are deployed using runtimes. If you are deploying a container image, you define the runtime and Linux distribution when you create your container image.  If you are deploying using a Zip file archive as the deployment package, you choose a runtime when you create the function.

At the time of writing, Lambda supports Amazon Linux 4.14.171 and Amazon Linux2 4.14.165. When constructing a function, the runtime can support one or all of the following languages - 

  • Node.js 14.  (JavaScript SDK 2.880.0)
  • Node.js 12
  • Node.js 10
  • Python 2.7, 3.6, 3.7  (Amazon Linux)
  • Python 3.8  (Amazon Linux2)
  • Ruby 2.5  (Amazon Linux)
  • Ruby 2.7 (Amazon Linux2)
  • Java 8 (java-1.8.0-openjdk on Amazon Linux)
  • Java 8 (amazon-corretto-9 on Amazon Linux2)
  • Java 11 (amazon-corretto 11 on Amazon Linux2)
  • Go 1 (Amazon Linux)
  • .NET Core 2.1 (Amazon Linux)
  • .NET Core 3.1 (Amazon Linux2)

Other languages can be run within Lambda using a Custom Runtime.

Lambda Functions

Your code is organised into Lambda functions. Functions are resources that you can invoke to run your code.  The function contains code to process events that are passed into the function either by you or other AWS services.

You can access Lambda via your AWS ConsoleLambda_-_Functions

When you create a function, you can select from a number of creation options:

Lambda_Function_Options

Using the author from scratch option you can quickly create a function that you can add code and extensions to.

Lambda_Create_Function

You give the function a unique name and select the runtime to use. If you select either Node.js, Python or Ruby you will be able to use the AWS console code editor to enter and validate your function code.

At this point you can use the advanced settings to associate the function to a particular VPC in your AWS account.

Lambda_Function_Advanced_Settings

Pressing create function will add the new function to your Lambda service dashboard.

Lambda_Function_Dashboard

Using the test mode, you can try out some sample function code provided by AWS.

Lambda_Function_Test_Options

There are a stack of services to choose from with various events to invoke. The services include : 

  • AWS Appsync
  • AWS Batch
  • CloudFormation
  • AWS CodeCommit
  • AWS Config
  • AWS Step Functions
  • CloudFront
  • CloudWatch
  • Amazon Cognito
  • Amazon Connect
  • DynamoDB
  • AWS Kinesis
  • S3
  • SES
  • SNS
  • SQS
  • SageMaker
  • Alexa
  • Alexa Smart Home
  • Amazon Lex

There is generic a Hello-world test event:

Lambda_Test_Event

as well as the many services above, like the S3 Put event

Lambda_S3_Test_Event


Lambda Function Triggers

A trigger is an entity that invokes a Lambda function. This could be another AWS service that you can configure to invoke a function like a security event. You can develop applications that trigger Lambda functions and you can also use event source mappings to trigger functions. An event source mapping is a resource in Lambda that reads data from a stream and fires functions when certain conditions are met or detected.

You can for instance configure AWS CloudWatch to invoke a function from CloudWatch events or you could get S3 to invoke a function when is detects certain activity in a bucket, like a new upload.  Each service has varied methods to invoke a Lambda function.

You can also invoke functions synchronously via the AWS CLI:

Lambda_Synchronous_invocation

As well as the Lambda console you can invoke functions from the Lambda API, AWS SDK and AWS toolkits.

What are Events in Lambda?

A Lambda event is a JSON document that contains data for the Lambda function to process. The Lambda runtime converts the event to an object which is passed to your function code.

AWS Lambda execution environment

Lambda creates an execution environment when functions are invoked. This provides a secure and isolated runtime environment which manages the resources required to run your function and lifecycle support for the function and any external extensions associated with the function. The function runtime communicates with Lambda using the runtime API and extensions if present use the extensions API.

When you create your Lambda function you need to specify config information like the maximum execution time allowed for the function, the amount of memory required etc which Lambda uses to set up the execution environment. The function runtime and external extensions are processes that run within the execution environment which will share any permissions, resources, environment tables and credentials between the functions and extensions residing in the execution environment.

The lifecycle of the execution environment has 3 distinct phases.

The Init phase in which Lambda creates or unfreezes an execution environment, downloads the code for the function and all layers, initializes the runtime and any extensions and then runs the function's initialization code. This happens through three sub-phases being the Extension Init, Runtime Init and Function Init

The second phase in the execution environment instantiation is the Invoke phase. This is where Lambda invokes the function handler to run the function. Once complete Lambda prepares the function to be invoked again. The environment is essentially frozen until called again.

The third phase is Shutdown. If the function fails to be invoked for a nominated period of time Lambda shuts down the runtime, alerts any connected extensions to stop cleanly via a shutdown event, then removes the environment. If an attached extension does not respond within 2 seconds Lambda issues a SIGKILL 

When writing function code, you should assume a Lambda will be instantiating a new execution environment, even though it may be thawing an exiting but frozen instance.

Lambda Deployment Package

Lambda functions are deployed using a code deployment package. There are two type of compatible packages:

1) A .zip file archive containing your function code and it's dependencies. In this scenario Lambda will provide the runtime and operating system for the function.

2) A container image (OCI compliant). The container should contain your function code, dependencies and also the operating system and a Lambda runtime.

Lambda Runtimes

These are language specific environments that run inside an execution environment.

  • Node.js 14.  (JavaScript SDK 2.880.0)
  • Node.js 12
  • Node.js 10
  • Python 2.7, 3.6, 3.7  (Amazon Linux)
  • Python 3.8  (Amazon Linux2)
  • Ruby 2.5  (Amazon Linux)
  • Ruby 2.7 (Amazon Linux2)
  • Java 8 (java-1.8.0-openjdk on Amazon Linux)
  • Java 8 (amazon-corretto-9 on Amazon Linux2)
  • Java 11 (amazon-corretto 11 on Amazon Linux2)
  • Go 1 (Amazon Linux)
  • .NET Core 2.1 (Amazon Linux)
  • .NET Core 3.1 (Amazon Linux2)

When creating a container, you choose a runtime and a Linux distro at the time you create the container. To change the runtime language or Linux distribution, you will need to create a new container.

If you are using a .Zip file archive as a deployment package you choose a runtime when you create the function (via console, cli, api etc). Each runtime is paired with a linux distro and will contain additional libraries and environment tables you can access from your function code.

Lambda Layers

A Lambda Layer is a .zip file archive that contains additional code and other content like libraries, data, config files and even custom runtimes. Think of these like library packages that provide a convenient way of providing your functions with the resources they require.

You can include up to five layers per function and when you include a layer in a function, the contents are extracted to the /opt directory in the execution environment.

Layers are private to your AWS account by default however you can choose to share a layer or make it public.

Layers are not used when you deploy functions as a container image. With a container deployment, you package your preferred runtime, libraries and other code and dependencies into the container image build.

Lambda Extensions

Lambda extensions allow you augment and integrate your Lambda functions with other services, applications and services built by AWS Lambda Partners or extensions you build yourself using the Lambda Extensions API.

A typical use-case for Lambda extensions would be to integrate your Lambda function with your favourite monitoring, security and governance tools like CloudWatch, CloudTrail, AWS X-Ray and AWS config.

To access the extensions, go to the Configuration tab on your function and select monitoring and operation tools.

Lambda_Monitoring_Extensions

By selecting "Edit" you can turn on/off theAmazon CloudWatch, Amazon X-Ray, Cloudwatch Lambda Insights.

Lambda_Monitoring_Tools

From here you can access the Lambda Extensions page to integrate Lambda partner extensions. 

Lambda_-_Extensions

Instructions on integrating each of the extensions is available by following the link on each of the "marketplace" links.

Lambda Concurrency

Concurrency refers to the number of requests your Lambda function is serving at any one time.  When your function is called, Lambda provisions an instance by either unfreezing a previous instance, or instantiating a new instance to process the event.

If your function is called while already processing a request, another instance is provisioned which increases the functions "concurrency"

Lambda functions are subject to quotas at the AWS region level, so settings are supplied to configure individual functions to limit their concurrency. These are:

  • Reserved Concurrency - which guarantees the max number of concurrent instances of the function
  • Provisioned Concurrency - instantiates the specified number of execution environments ready to respond when functions are invoked.

If you want to ensure your functions can utilize an available execution environment, you can use the Reserved Concurrency setting. This prevents other functions from taking priority over your function.

The concurrency setting are found in the Function Configuration Settings.

Lambda_Edit_Concurrency

So in the above account, we have 1000 concurrent slots available for all of our Lambda Functions.  By setting the "Reserve Concurrency" on this one individual function to say 200, then this function will always have 200 concurrent execution environments slots available.  This will reduce the overall number of concurrent slots available to other functions to 800.

If you are looking to reduce latency issues when your function is invoked you can use the Provisioned Concurrency setting to ensure execution environments are spun up ready to accept function invocations.

Lambda_Provisioned_Concurrency

There are cost implications of implementing provisioned concurrency, however the costs of executing functions are separate to provisioned concurrency instances that are by nature always running.

Lambda Qualifiers

Lambda allows you to specify a qualifier when invoking or maintaining a function.

Version numbers are used to isolate immutable snapshots of function code and can be invoked individually  ie  myFunction:1 and myFunction:2 can contain and execute different code.

You can also set up an Alias to point to a specific version of the function. So for instance if you set up the alias "myFunction:LIVE", you can point it to the function version you wish to run. If you make changes and create a new function version like myFunction:43 you can edit the myFunction:LIVE alias to point to the new version without having to update any code that is invoking the function. 

AWS Lambda Pricing

There is no charge for setting up Lambda functions. AWS charge for the number of calls to your functions and also the compute time it takes to execute the code within the function.

Pricing is also governed by the amount of memory you allocate to your function. Memory is used to govern the amount of compute power allocated to the execution environments invoked by a function call.

A full run down of Lambda pricing can be found here

Hava generates diagrams from your AWS configuration when you connect a set of read-only credentials or a cross-account role. 

Lambda functions are one of the many resources that are visualized when you connect Hava to your AWS account.

Hava_Lambda

If you would like to check out what you AWS environments and Lambda functions look like in diagram form, you can take a free trial of Hava at any time using the button below.

Try Hava For Free Today! 

 

 

Read Next:  AWS App Runner 

Team Hava

Written by Team Hava

Featured