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

You can access Lambda via your AWS Console

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

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

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.

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

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

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:

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

Lambda Function Triggers

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:

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

What are Events in Lambda?

AWS Lambda execution environment

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

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

  • 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

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

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.

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

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

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

Lambda Concurrency

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.

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.

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

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

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.

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 at https://www.hava.io

Originally published at https://www.hava.io.

Tech Writer, Developer, Marketer and Generator of Leads.