Lambda Developer Guide
Lambda Developer Guide
Developer Guide
AWS Lambda Developer Guide
Amazon's trademarks and trade dress may not be used in connection with any product or service that is not
Amazon's, in any manner that is likely to cause confusion among customers, or in any manner that disparages or
discredits Amazon. All other trademarks not owned by Amazon are the property of their respective owners, who may
or may not be affiliated with, connected to, or sponsored by Amazon.
AWS Lambda Developer Guide
Table of Contents
What is AWS Lambda? ........................................................................................................................ 1
When should I use Lambda? ........................................................................................................ 1
Lambda features ........................................................................................................................ 2
Getting started with Lambda ....................................................................................................... 2
Related services ......................................................................................................................... 3
Accessing Lambda ...................................................................................................................... 3
Pricing for Lambda ..................................................................................................................... 4
Setting up ......................................................................................................................................... 5
AWS Account ............................................................................................................................. 5
AWS CLI .................................................................................................................................... 5
AWS SAM .................................................................................................................................. 5
AWS SAM CLI ............................................................................................................................ 6
Tools for container images .......................................................................................................... 6
Code authoring tools .................................................................................................................. 6
Getting started .................................................................................................................................. 8
Create a function ....................................................................................................................... 9
Create a Lambda function with default function code ............................................................. 9
Create a function defined as a container image .................................................................... 11
Code editor ............................................................................................................................. 17
Working with files and folders ........................................................................................... 17
Working with code ........................................................................................................... 19
Working in fullscreen mode ............................................................................................... 22
Working with preferences .................................................................................................. 22
AWS CLI .................................................................................................................................. 23
Prerequisites .................................................................................................................... 23
Create the execution role .................................................................................................. 23
Create the function .......................................................................................................... 24
List the Lambda functions in your account .......................................................................... 27
Retrieve a Lambda function ............................................................................................... 27
Clean up ......................................................................................................................... 28
Concepts ................................................................................................................................. 29
Function .......................................................................................................................... 29
Trigger ............................................................................................................................ 29
Event .............................................................................................................................. 29
Execution environment ...................................................................................................... 30
Deployment package ........................................................................................................ 30
Runtime .......................................................................................................................... 30
Layer .............................................................................................................................. 30
Extension ........................................................................................................................ 31
Concurrency ..................................................................................................................... 31
Qualifier .......................................................................................................................... 31
Features .................................................................................................................................. 32
Programming model ......................................................................................................... 32
Scaling ............................................................................................................................ 34
Concurrency controls ........................................................................................................ 35
Asynchronous invocation ................................................................................................... 36
Event source mappings ..................................................................................................... 37
Destinations ..................................................................................................................... 38
Function blueprints ........................................................................................................... 39
Testing and deployment tools ............................................................................................ 40
Application templates ....................................................................................................... 40
Deployment packages ............................................................................................................... 41
Container images ............................................................................................................. 41
.zip file archives ............................................................................................................... 41
iii
AWS Lambda Developer Guide
Layers ............................................................................................................................. 42
Using other AWS services .................................................................................................. 42
Quotas .................................................................................................................................... 44
Permissions ..................................................................................................................................... 46
Execution role .......................................................................................................................... 47
Creating an execution role in the IAM console ...................................................................... 48
Grant least privilege access to your Lambda execution role .................................................... 48
Managing roles with the IAM API ....................................................................................... 49
AWS managed policies for Lambda features ........................................................................ 50
Resource-based policies ............................................................................................................. 51
Granting function access to AWS services ............................................................................ 52
Granting function access to other accounts .......................................................................... 53
Granting layer access to other accounts .............................................................................. 54
Cleaning up resource-based policies .................................................................................... 55
User policies ............................................................................................................................ 56
Function development ...................................................................................................... 56
Layer development and use ............................................................................................... 59
Cross-account roles ........................................................................................................... 60
Condition keys for VPC settings ........................................................................................ 60
Resources and conditions .......................................................................................................... 61
Function resource names ................................................................................................... 62
Function actions ............................................................................................................... 64
Event source mapping actions ............................................................................................ 66
Layer actions ................................................................................................................... 66
Permissions boundaries ............................................................................................................. 67
Managing functions .......................................................................................................................... 69
Configure console ..................................................................................................................... 70
Functions defined as container images ........................................................................................ 72
Function version $LATEST ................................................................................................. 72
Container image deployment ............................................................................................. 72
Amazon ECR permissions ................................................................................................. 72
Override the container settings .......................................................................................... 73
Creating a function (console) ............................................................................................. 73
Updating the function code (console) ................................................................................. 74
Overriding the image parameters (console) .......................................................................... 74
Using the Lambda API ...................................................................................................... 75
AWS CloudFormation ........................................................................................................ 75
Environment variables ............................................................................................................... 77
Configuring environment variables ..................................................................................... 77
Configuring environment variables with the API ................................................................... 78
Example scenario for environment variables ........................................................................ 78
Retrieve environment variables .......................................................................................... 79
Defined runtime environment variables ............................................................................... 80
Securing environment variables .......................................................................................... 81
Sample code and templates .............................................................................................. 83
Concurrency ............................................................................................................................. 84
Configuring reserved concurrency ....................................................................................... 85
Configuring provisioned concurrency .................................................................................. 87
Configuring concurrency with the Lambda API ..................................................................... 90
Versions .................................................................................................................................. 95
Creating function versions ................................................................................................. 95
Managing versions with the Lambda API ............................................................................. 95
Using versions .................................................................................................................. 96
Granting permissions ........................................................................................................ 96
Aliases .................................................................................................................................... 97
Creating a function alias (Console) ..................................................................................... 97
Managing aliases with the Lambda API ............................................................................... 97
iv
AWS Lambda Developer Guide
v
AWS Lambda Developer Guide
vi
AWS Lambda Developer Guide
vii
AWS Lambda Developer Guide
viii
AWS Lambda Developer Guide
ix
AWS Lambda Developer Guide
x
AWS Lambda Developer Guide
xi
AWS Lambda Developer Guide
xii
AWS Lambda Developer Guide
xiii
AWS Lambda Developer Guide
xiv
AWS Lambda Developer Guide
xv
AWS Lambda Developer Guide
xvi
AWS Lambda Developer Guide
xvii
AWS Lambda Developer Guide
When should I use Lambda?
You organize your code into Lambda functions (p. 29). Lambda runs your function only when needed
and scales automatically, from a few requests per day to thousands per second. You pay only for the
compute time that you consume—there is no charge when your code is not running.
You can invoke your Lambda functions using the Lambda API, or Lambda can run your functions in
response to events from other AWS services. For example, you can use Lambda to:
• Build data-processing triggers for AWS services such as Amazon Simple Storage Service (Amazon S3)
and Amazon DynamoDB.
• Process streaming data stored in Amazon Kinesis.
• Create your own backend that operates at AWS scale, performance, and security.
Lambda is a highly available service. For more information, see the AWS Lambda Service Level
Agreement.
Sections
• When should I use Lambda? (p. 1)
• Lambda features (p. 2)
• Getting started with Lambda (p. 2)
• Related services (p. 3)
• Accessing Lambda (p. 3)
• Pricing for Lambda (p. 4)
When using Lambda, you are responsible only for your code. Lambda manages the compute fleet that
offers a balance of memory, CPU, network, and other resources to run your code. Because Lambda
manages these resources, you cannot log in to compute instances or customize the operating system on
provided runtimes (p. 195). Lambda performs operational and administrative activities on your behalf,
including managing capacity, monitoring, and logging your Lambda functions.
If you need to manage your own compute resources, AWS has other compute services to meet your
needs. For example:
1
AWS Lambda Developer Guide
Lambda features
• Amazon Elastic Compute Cloud (Amazon EC2) offers a wide range of EC2 instance types to choose
from. It lets you customize operating systems, network and security settings, and the entire software
stack. You are responsible for provisioning capacity, monitoring fleet health and performance, and
using Availability Zones for fault tolerance.
• AWS Elastic Beanstalk enables you to deploy and scale applications onto Amazon EC2. You retain
ownership and full control over the underlying EC2 instances.
Lambda features
The following key features help you develop Lambda applications that are scalable, secure, and easily
extensible:
Concurrency and scaling controls (p. 153) such as concurrency limits and provisioned concurrency
give you fine-grained control over the scaling and responsiveness of your production applications.
Functions defined as container images
Use your preferred container image (p. 247) tooling, workflows, and dependencies to build, test,
and deploy your Lambda functions.
Code signing
Code signing (p. 130) for Lambda provides trust and integrity controls that let you verify that only
unaltered code that approved developers have published is deployed in your Lambda functions.
Lambda extensions
You can use Lambda extensions (p. 209) to augment your Lambda functions. For example, use
extensions to more easily integrate Lambda with your favorite tools for monitoring, observability,
security, and governance.
Function blueprints
A function blueprint provides sample code that shows how to use Lambda with other AWS services
or third-party applications. Blueprints include sample code and function configuration presets for
Node.js and Python runtimes.
Database access
A database proxy (p. 121) manages a pool of database connections and relays queries from a
function. This enables a function to reach high concurrency levels without exhausting database
connections.
File systems access
You can configure a function to mount an Amazon Elastic File System (Amazon EFS) file
system (p. 126) to a local directory. With Amazon EFS, your function code can access and modify
shared resources safely and at high concurrency.
• Linux OS and commands, as well as concepts such as processes, threads, and file permissions.
• Cloud concepts and IP networking concepts (for public and private networks).
• Distributed computing concepts such as HTML as an IPC, queues, messaging, notifications, and
concurrency.
2
AWS Lambda Developer Guide
Related services
• Familiarity with security services and concepts: AWS Identity and Access Management (IAM) and access
control principles, and AWS Key Management Service (AWS KMS) and public key infrastructure.
• Familiarity with key services that interact with Lambda: Amazon API Gateway, Amazon S3, Amazon
Simple Queue Service (Amazon SQS), and DynamoDB.
• Configuring EC2 instances with Linux.
If you are a first-time user of Lambda, we recommend that you start with the following topics to help
you learn the basics:
1. Read the Lambda product overview and explore the Lambda getting started page.
2. To create and test a Lambda function using the Lambda console, try the console-based getting
started exercise (p. 8). This exercise teaches you about the Lambda programming model and
other concepts.
3. If you are familiar with container image workflows, try the getting started exercise to create a
Lambda function defined as a container image (p. 11).
AWS also provides the following resources for learning about serverless applications and Lambda:
Related services
Lambda integrates with other AWS services (p. 257) to invoke functions based on events that you
specify. For example:
• Use API Gateway (p. 261) to provide a secure and scalable gateway for web APIs that route HTTP
requests to Lambda functions.
• For services that generate a queue or data stream (such as DynamoDB (p. 313) and Kinesis (p. 365)),
Lambda polls the queue or data stream from the service and invokes your function to process the
received data.
• Define Amazon S3 (p. 407) events that invoke a Lambda function to process Amazon S3 objects, for
example, when an object is created or deleted.
• Use a Lambda function to process Amazon SQS (p. 439) messages or Amazon Simple Notification
Service (Amazon SNS) (p. 432) notifications.
• Use AWS Step Functions (p. 455) to connect Lambda functions together into serverless workflows
called state machines.
Accessing Lambda
You can create, invoke, and manage your Lambda functions using any of the following interfaces:
• AWS Management Console – Provides a web interface for you to access your functions. For more
information, see Configuring functions in the console (p. 70).
• AWS Command Line Interface (AWS CLI) – Provides commands for a broad set of AWS services,
including Lambda, and is supported on Windows, macOS, and Linux. For more information, see Using
AWS Lambda with the AWS Command Line Interface (p. 23).
3
AWS Lambda Developer Guide
Pricing for Lambda
• AWS SDKs – Provide language-specific APIs and manage many of the connection details, such as
signature calculation, request retry handling, and error handling. For more information, see AWS SDKs.
• AWS CloudFormation – Enables you to create templates that define your Lambda applications. For
more information, see AWS Lambda applications (p. 174). AWS CloudFormation also supports the
AWS Cloud Development Kit (CDK).
• AWS Serverless Application Model (AWS SAM) – Provides templates and a CLI to configure and
manage AWS serverless applications. For more information, see AWS SAM (p. 5).
4
AWS Lambda Developer Guide
AWS Account
Sections
• AWS Account (p. 5)
• AWS CLI (p. 5)
• AWS SAM (p. 5)
• AWS SAM CLI (p. 6)
• Tools for container images (p. 6)
• Code authoring tools (p. 6)
AWS Account
To use Lambda and other AWS services, you need an AWS account. If you do not have an account, visit
aws.amazon.com and choose Create an AWS Account. For instructions, see How do I create and activate
a new AWS account?
As a best practice, create an AWS Identity and Access Management (IAM) user with administrator
permissions, and then use that IAM user for all work that does not require root credentials. Create a
password for console access, and create access keys to use command line tools. For instructions, see
Creating your first IAM admin user and group in the IAM User Guide.
AWS CLI
If you plan to configure and use Lambda functions from the command line, install the AWS Command
Line Interface (AWS CLI). Tutorials in this guide use the AWS CLI, which has commands for all Lambda API
operations. Some functionality is not available in the Lambda console and can be accessed only with the
AWS CLI or the AWS SDKs.
To set up the AWS CLI, see the following topics in the AWS Command Line Interface User Guide.
To verify that the AWS CLI is configured correctly, run the list-functions command to see a list of
your Lambda functions in the current AWS Region.
AWS SAM
The AWS Serverless Application Model (AWS SAM) is an extension for the AWS CloudFormation template
language that lets you define serverless applications at a higher level. AWS SAM abstracts away common
5
AWS Lambda Developer Guide
AWS SAM CLI
tasks such as function role creation, making it easier to write templates. AWS SAM is supported directly
by AWS CloudFormation, and includes additional functionality through the AWS CLI and AWS SAM CLI.
For more information about AWS SAM templates, see the AWS SAM specification in the AWS Serverless
Application Model Developer Guide.
To set up the AWS SAM CLI, see Installing the AWS SAM CLI in the AWS Serverless Application Model
Developer Guide.
To set up the Docker CLI, see Get Docker on the Docker Docs website. For an introduction to using Docker
with AWS, see Getting started with Amazon ECR using the AWS CLI in the Amazon Elastic Container
Registry User Guide.
• The language that you use to write your Lambda function code.
• The libraries that you use in your code. The Lambda runtimes provide some of the libraries, and you
must upload any additional libraries that you use.
The following table lists the languages that Lambda supports, and the tools and options that you can
use with them.
6
AWS Lambda Developer Guide
Code authoring tools
C# • Visual Studio, with IDE plugin (see AWS Toolkit for Visual Studio)
• .NET Core (see Download .NET on the Microsoft website)
• Your own authoring environment
7
AWS Lambda Developer Guide
You can author functions in the Lambda console, or with an IDE toolkit, command line tools, or the AWS
SDKs. The Lambda console provides a code editor (p. 17) for non-compiled languages that lets you
modify and test code quickly. The AWS Command Line Interface (AWS CLI) (p. 23) gives you direct
access to the Lambda API for advanced configuration and automation use cases.
You deploy your function code to Lambda using a deployment package. Lambda supports two types of
deployment packages:
• A .zip file archive that contains your function code and its dependencies. For an example tutorial, see
Using AWS Lambda with the AWS Command Line Interface (p. 23).
• A container image that is compatible with the Open Container Initiative (OCI) specification. For an
example tutorial, see Create a function defined as a container image (p. 11).
Topics
• Create a Lambda function with the console (p. 9)
• Creating functions using the AWS Lambda console editor (p. 17)
• Using AWS Lambda with the AWS Command Line Interface (p. 23)
• Lambda concepts (p. 29)
• Lambda features (p. 32)
• Lambda deployment packages (p. 41)
• Lambda quotas (p. 44)
8
AWS Lambda Developer Guide
Create a function
In the first exercise, you create a function and use the default code that Lambda creates. The Lambda
console provides a code editor (p. 17) for non-compiled languages that lets you modify and test code
quickly.
In the next exercise, you create a function defined as a container image. First, create a container image
for your function code, and then use the Lambda console to create a function from the container image.
Topics
• Create a Lambda function with default function code (p. 9)
• Create a function defined as a container image (p. 11)
Lambda creates a Node.js function and an execution role (p. 47) that grants the function permission
to upload logs. The Lambda function assumes the execution role when you invoke your function, and
uses the execution role to create credentials for the AWS SDK and to read data from event sources.
9
AWS Lambda Developer Guide
Create a Lambda function with default function code
To invoke a function
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
3. Choose Save changes, and then choose Test. Each user can create up to 10 test events per function.
Those test events are not available to other users.
Lambda runs your function on your behalf. The function handler receives and then processes the
sample event.
4. Upon successful completion, view the results in the console.
• The Execution result shows the execution status as succeeded. To view the function execution
results, expand Details. Note that the logs link opens the Log groups page in the CloudWatch
console.
• The Summary section shows the key information reported in the Log output section (the REPORT
line in the execution log).
• The Log output section shows the log that Lambda generates for each invocation. The function
writes these logs to CloudWatch. The Lambda console shows these logs for your convenience.
Choose Click here to add logs to the CloudWatch log group and open the Log groups page in the
CloudWatch console.
5. Run the function (choose Test) a few more times to gather some metrics that you can view in the
next step.
6. Choose the Monitor tab. This page shows graphs for the metrics that Lambda sends to CloudWatch.
For more information on these graphs, see Monitoring functions in the AWS Lambda
console (p. 684).
10
AWS Lambda Developer Guide
Create a function defined as a container image
Clean up
If you are done working with the example function, delete it. You can also delete the log group that
stores the function's logs, and the execution role that the console created.
1. Open the Roles page of the AWS Identity and Access Management (IAM) console.
2. Select the function's role (my-function-role-31exxmpl).
3. Choose Delete role.
4. In the Delete role dialog box, choose Yes, delete.
You can automate the creation and cleanup of functions, log groups, and roles with AWS
CloudFormation and the AWS Command Line Interface (AWS CLI). For fully functional sample
applications, see Lambda sample applications (p. 464).
Topics
• Prerequisites (p. 11)
• Create the container image (p. 12)
• Upload the image to the Amazon ECR repository (p. 13)
• Update the user permissions (p. 13)
• Create a Lambda function defined as a container image (p. 14)
• Invoke the Lambda function (p. 14)
• Clean up (p. 15)
Prerequisites
To complete the following steps, you need a command line terminal or shell to run commands.
Commands and the expected output are listed in separate blocks:
11
AWS Lambda Developer Guide
Create a function defined as a container image
aws --version
For long commands, an escape character (\) is used to split a command over multiple lines.
On Linux and macOS, use your preferred shell and package manager. On Windows 10, you can install the
Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.
This exercise uses Docker CLI commands to create the container image. To install the Docker CLI, see Get
Docker on the Docker Docs website.
In the following commands, replace 123456789012 with your AWS account ID.
1. On your local machine, create a project directory for your new function.
2. Create a file named app.js in your project directory. Add the following code to app.js:
3. Use a text editor to create a new file named Dockerfile in your project directory. Add the
following content to Dockerfile:
FROM public.ecr.aws/lambda/nodejs:12
4. Create the package.json file. From your project directory, run the npm init command. Accept all
of the default values:
npm init
5. Build your Docker image. From your project directory, run the following command:
12
AWS Lambda Developer Guide
Create a function defined as a container image
6. (Optional) AWS base images include the Lambda runtime interface emulator, so you can test your
function locally.
a. Run your Docker image. From your project directory, run the docker run command:
b. Test your Lambda function. In a new terminal window, run a curl command to invoke your
function:
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-
stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
3. Tag your image to match your repository name using the docker tag command.
4. Deploy the image to Amazon ECR using the docker push command.
For example, use the IAM console to create a role with the following policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": ["ecr:SetRepositoryPolicy","ecr:GetRepositoryPolicy",
"ecr:InitiateLayerUpload],
"Resource": "arn:aws:ecr:<region>:<account>:repository/<repo name>/"
}
13
AWS Lambda Developer Guide
Create a function defined as a container image
]
}
Lambda creates your function and an execution role (p. 47) that grants the function permission to
upload logs. Lambda assumes the execution role when you invoke your function, and uses the execution
role to create credentials for the AWS SDK and to read data from event sources.
To invoke a function
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
3. Choose Save changes, and then choose Test. Each user can create up to 10 test events per function.
Those test events are not available to other users.
Lambda runs your function on your behalf. The function handler receives and then processes the
sample event.
4. Upon successful completion, view the results in the console.
• The Execution result shows the execution status as succeeded. To view the function execution
results, expand Details. Note that the logs link opens the Log groups page in the CloudWatch
console.
• The Summary section shows the key information reported in the Log output section (the REPORT
line in the execution log).
• The Log output section shows the log that Lambda generates for each invocation. The function
writes these logs to CloudWatch. The Lambda console shows these logs for your convenience.
Choose Click here to add logs to the CloudWatch log group and open the Log groups page in the
CloudWatch console.
14
AWS Lambda Developer Guide
Create a function defined as a container image
5. Run the function (choose Test) a few more times to gather some metrics that you can view in the
next step.
6. Choose the Monitor tab. This page shows graphs for the metrics that Lambda sends to CloudWatch.
For more information on these graphs, see Monitoring functions in the AWS Lambda
console (p. 684).
Clean up
If you are finished with the container image, see Deleting an image in the Amazon Elastic Container
Registry User Guide
If you are done working with your function, delete it. You can also delete the log group that stores the
function's logs and the execution role that the console created.
15
AWS Lambda Developer Guide
Create a function defined as a container image
You can automate the creation and cleanup of functions, log groups, and roles with AWS
CloudFormation and the AWS CLI. For fully functional sample applications, see Lambda sample
applications (p. 464).
16
AWS Lambda Developer Guide
Code editor
The code editor includes the menu bar, windows, and the editor pane.
For a list of what the commands do, see the Menu commands reference in the AWS Cloud9 User Guide.
Note that some of the commands listed in that reference are not available in the code editor.
Topics
• Working with files and folders (p. 17)
• Working with code (p. 19)
• Working in fullscreen mode (p. 22)
• Working with preferences (p. 22)
To show or hide the Environment window, choose the Environment button. If the Environment button
is not visible, choose Window, Environment on the menu bar.
17
AWS Lambda Developer Guide
Working with files and folders
To open a single file and show its contents in the editor pane, double-click the file in the Environment
window.
To open multiple files and show their contents in the editor pane, choose the files in the Environment
window. Right-click the selection, and then choose Open.
• In the Environment window, right-click the folder where you want the new file to go, and then choose
New File. Type the file's name and extension, and then press Enter.
• Choose File, New File on the menu bar. When you're ready to save the file, choose File, Save or File,
Save As on the menu bar. Then use the Save As dialog box that displays to name the file and choose
where to save it.
• In the tab buttons bar in the editor pane, choose the + button, and then choose New File. When you're
ready to save the file, choose File, Save or File, Save As on the menu bar. Then use the Save As dialog
box that displays to name the file and choose where to save it.
To create a new folder, right-click the folder in the Environment window where you want the new
folder to go, and then choose New Folder. Type the folder's name, and then press Enter.
To save a file, with the file open and its contents visible in the editor pane, choose File, Save on the
menu bar.
To rename a file or folder, right-click the file or folder in the Environment window. Type the
replacement name, and then press Enter.
To delete files or folders, choose the files or folders in the Environment window. Right-click the
selection, and then choose Delete. Then confirm the deletion by choosing Yes (for a single selection) or
Yes to All.
To cut, copy, paste, or duplicate files or folders, choose the files or folders in the Environment window.
Right-click the selection, and then choose Cut, Copy, Paste, or Duplicate, respectively.
18
AWS Lambda Developer Guide
Working with code
To collapse folders, choose the gear icon in the Environment window, and then choose Collapse All
Folders.
To show or hide hidden files, choose the gear icon in the Environment window, and then choose Show
Hidden Files.
19
AWS Lambda Developer Guide
Working with code
To close multiple open files, choose the drop-down menu in the tab buttons bar, and then choose Close
All Tabs in All Panes or Close All But Current Tab as needed.
To create a new file, choose the + button in the tab buttons bar, and then choose New File. When you're
ready to save the file, choose File, Save or File, Save As on the menu bar. Then use the Save As dialog
box that displays to name the file and choose where to save it.
To move quickly to a line in the active file, choose the line selector, type the line number to go to, and
then press Enter.
20
AWS Lambda Developer Guide
Working with code
To change the code color scheme in the active file, choose the code color scheme selector, and then
choose the new code color scheme.
To change in the active file whether soft tabs or spaces are used, the tab size, or whether to convert
to spaces or tabs, choose the spaces and tabs selector, and then choose the new settings.
To change for all files whether to show or hide invisible characters or the gutter, auto-pair brackets
or quotes, wrap lines, or the font size, choose the gear icon, and then choose the new settings.
21
AWS Lambda Developer Guide
Working in fullscreen mode
To expand the code editor to the edges of the web browser window, choose the Toggle fullscreen
button in the menu bar.
To shrink the code editor to its original size, choose the Toggle fullscreen button again.
In fullscreen mode, additional options are displayed on the menu bar: Save and Test. Choosing Save
saves the function code. Choosing Test or Configure Events enables you to create or edit the function's
test events.
To change code editor settings, choose the Preferences gear icon in the menu bar.
For a list of what the settings do, see the following references in the AWS Cloud9 User Guide.
Note that some of the settings listed in those references are not available in the code editor.
22
AWS Lambda Developer Guide
AWS CLI
In this tutorial, you manage and invoke Lambda functions with the AWS CLI. For more information, see
What is the AWS CLI? in the AWS Command Line Interface User Guide.
Prerequisites
This tutorial assumes that you have some knowledge of basic Lambda operations and the Lambda
console. If you haven't already, follow the instructions in the section called “Create a function” (p. 9).
To complete the following steps, you need a command line terminal or shell to run commands.
Commands and the expected output are listed in separate blocks:
aws --version
For long commands, an escape character (\) is used to split a command over multiple lines.
On Linux and macOS, use your preferred shell and package manager. On Windows 10, you can install the
Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.
This tutorial uses the AWS Command Line Interface (AWS CLI) to call service API operations. To install the
AWS CLI, see Installing the AWS CLI in the AWS Command Line Interface User Guide.
{
"Role": {
"Path": "/",
"RoleName": "lambda-ex",
"RoleId": "AROAQFOXMPL6TZ6ITKWND",
"Arn": "arn:aws:iam::123456789012:role/lambda-ex",
"CreateDate": "2020-01-17T23:19:12Z",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
23
AWS Lambda Developer Guide
Create the function
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
}
}
The trust-policy.json file is a JSON file in the current directory that defines the trust policy for
the role. This trust policy allows Lambda to use the role's permissions by giving the service principal
lambda.amazonaws.com permission to call the AWS Security Token Service AssumeRole action.
Example trust-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
You can also specify the trust policy inline. Requirements for escaping quotes in the JSON string vary
depending on your shell.
To add permissions to the role, use the attach-policy-to-role command. Start by adding the
AWSLambdaBasicExecutionRole managed policy.
The AWSLambdaBasicExecutionRole policy has the permissions that the function needs to write logs to
CloudWatch Logs.
Example index.js
24
AWS Lambda Developer Guide
Create the function
3. Create a Lambda function with the create-function command. Replace the highlighted text in
the role ARN with your account ID.
{
"FunctionName": "my-function",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"Runtime": "nodejs12.x",
"Role": "arn:aws:iam::123456789012:role/lambda-ex",
"Handler": "index.handler",
"CodeSha256": "FpFMvUhayLkOoVBpNuNiIVML/tuGv2iJQ7t0yWVTU8c=",
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "88ebe1e1-bfdf-4dc3-84de-3017268fa1ff",
...
}
To get logs for an invocation from the command line, use the --log-type option. The response
includes a LogResult field that contains up to 4 KB of base64-encoded logs from the invocation.
{
"StatusCode": 200,
"LogResult":
"U1RBUlQgUmVxdWVzdElkOiA4N2QwNDRiOC1mMTU0LTExZTgtOGNkYS0yOTc0YzVlNGZiMjEgVmVyc2lvb...",
"ExecutedVersion": "$LATEST"
}
25
AWS Lambda Developer Guide
Create the function
The base64 utility is available on Linux, macOS, and Ubuntu on Windows. For macOS, the command is
base64 -D.
To get full log events from the command line, you can include the log stream name in the output of your
function, as shown in the preceding example. The following example script invokes a function named
my-function and downloads the last five log events.
#!/bin/bash
aws lambda invoke --function-name my-function --cli-binary-format raw-in-base64-out --
payload '{"key": "value"}' out
sed -i'' -e 's/"//g' out
sleep 15
aws logs get-log-events --log-group-name /aws/lambda/my-function --log-stream-name $(cat
out) --limit 5
The script uses sed to remove quotes from the output file, and sleeps for 15 seconds to allow time for
the logs to be available. The output includes the response from Lambda and the output from the get-
log-events command.
./get-logs.sh
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
{
"events": [
{
"timestamp": 1559763003171,
"message": "START RequestId: 4ce9340a-b765-490f-ad8a-02ab3415e2bf Version:
$LATEST\n",
"ingestionTime": 1559763003309
},
{
"timestamp": 1559763003173,
"message": "2019-06-05T19:30:03.173Z\t4ce9340a-b765-490f-ad8a-02ab3415e2bf
\tINFO\tENVIRONMENT VARIABLES\r{\r \"AWS_LAMBDA_FUNCTION_VERSION\": \"$LATEST\",\r ...",
"ingestionTime": 1559763018353
},
{
"timestamp": 1559763003173,
"message": "2019-06-05T19:30:03.173Z\t4ce9340a-b765-490f-ad8a-02ab3415e2bf
\tINFO\tEVENT\r{\r \"key\": \"value\"\r}\n",
"ingestionTime": 1559763018353
},
{
"timestamp": 1559763003218,
"message": "END RequestId: 4ce9340a-b765-490f-ad8a-02ab3415e2bf\n",
"ingestionTime": 1559763018353
},
{
"timestamp": 1559763003218,
26
AWS Lambda Developer Guide
List the Lambda functions in your account
{
"Functions": [
{
"FunctionName": "cli",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"Runtime": "nodejs12.x",
"Role": "arn:aws:iam::123456789012:role/lambda-ex",
"Handler": "index.handler",
...
},
{
"FunctionName": "random-error",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:random-error",
"Runtime": "nodejs12.x",
"Role": "arn:aws:iam::123456789012:role/lambda-role",
"Handler": "index.handler",
...
},
...
],
"NextToken": "eyJNYXJrZXIiOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAxMH0="
}
In response, Lambda returns a list of up to 10 functions. If there are more functions you can retrieve,
NextToken provides a marker you can use in the next list-functions request. The following list-
functions AWS CLI command is an example that shows the --starting-token parameter.
27
AWS Lambda Developer Guide
Clean up
{
"Configuration": {
"FunctionName": "my-function",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"Runtime": "nodejs12.x",
"Role": "arn:aws:iam::123456789012:role/lambda-ex",
"CodeSha256": "FpFMvUhayLkOoVBpNuNiIVML/tuGv2iJQ7t0yWVTU8c=",
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "88ebe1e1-bfdf-4dc3-84de-3017268fa1ff",
...
},
"Code": {
"RepositoryType": "S3",
"Location": "https://awslambda-us-east-2-tasks.s3.us-east-2.amazonaws.com/
snapshots/123456789012/my-function-4203078a-b7c9-4f35-..."
}
}
Clean up
Run the following delete-function command to delete the my-function function.
Delete the IAM role you created in the IAM console. For information about deleting a role, see Deleting
roles or instance profiles in the IAM User Guide.
28
AWS Lambda Developer Guide
Concepts
Lambda concepts
With Lambda, you run functions to process events. To send events to your function, you can invoke it
using the Lambda API, or you can configure an AWS service or resource to invoke it.
Concepts
• Function (p. 29)
• Trigger (p. 29)
• Event (p. 29)
• Execution environment (p. 30)
• Deployment package (p. 30)
• Runtime (p. 30)
• Layer (p. 30)
• Extension (p. 31)
• Concurrency (p. 31)
• Qualifier (p. 31)
Function
A function is a resource that you can invoke to run your code in Lambda. A function has code to process
the events (p. 29) that you pass into the function or that other AWS services send to the function.
For more information, see Managing AWS Lambda functions (p. 69).
Trigger
A trigger is a resource or configuration that invokes a Lambda function. This includes AWS services
that you can configure to invoke a function, applications that you develop, and event source
mappings (p. 148). An event source mapping is a resource in Lambda that reads items from a stream or
queue and invokes a function. For more information, see Invoking AWS Lambda functions (p. 137) and
Using AWS Lambda with other services (p. 257).
Event
An event is a JSON-formatted document that contains data for a Lambda function to process. The
runtime converts the event to an object and passes it to your function code. When you invoke a function,
you determine the structure and contents of the event.
{
"TemperatureK": 281,
"WindKmh": -3,
"HumidityPct": 0.55,
"PressureHPa": 1020
}
When an AWS service invokes your function, the service defines the shape of the event.
29
AWS Lambda Developer Guide
Execution environment
"Records": [
{
"Sns": {
"Timestamp": "2019-01-02T12:45:07.000Z",
"Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",
...
For more information about events from AWS services, see Using AWS Lambda with other
services (p. 257).
Execution environment
An execution environment provides a secure and isolated runtime environment for your Lambda function.
An execution environment manages the processes and resources that are required to run the function.
The execution environment provides lifecycle support for the function and for any extensions (p. 31)
associated with your function.
For more information, see AWS Lambda execution environment (p. 200).
Deployment package
You deploy your Lambda function code using a deployment package. Lambda supports two types of
deployment packages:
• A .zip file archive that contains your function code and its dependencies. Lambda provides the
operating system and runtime for your function.
• A container image that is compatible with the Open Container Initiative (OCI) specification. You add
your function code and dependencies to the image. You must also include the operating system and a
Lambda runtime.
Runtime
The runtime provides a language-specific environment that runs in an execution environment. The
runtime relays invocation events, context information, and responses between Lambda and the function.
You can use runtimes that Lambda provides, or build your own. If you package your code as a .zip file
archive, you must configure your function to use a runtime that matches your programming language.
For a container image, you include the runtime when you build the image.
Layer
A Lambda layer is a .zip file archive that can contain additional code or other content. A layer can contain
libraries, a custom runtime (p. 236), data, or configuration files.
Layers provide a convenient way to package libraries and other dependencies that you can use with your
Lambda functions. Using layers reduces the size of uploaded deployment archives and makes it faster to
deploy your code. Layers also promote code sharing and separation of responsibilities so that you can
iterate faster on writing business logic.
You can include up to five layers per function. Layers count towards the standard Lambda deployment
size quotas (p. 44). When you include a layer in a function, the contents are extracted to the /opt
directory in the execution environment.
30
AWS Lambda Developer Guide
Extension
By default, the layers that you create are private to your AWS account. You can choose to share a layer
with other accounts or to make the layer public. If your functions consume a layer that a different
account published, your functions can continue to use the layer version after it has been deleted, or after
your permission to access the layer is revoked. However, you cannot create a new function or update
functions using a deleted layer version.
Functions deployed as a container image do not use layers. Instead, you package your preferred runtime,
libraries, and other dependencies into the container image when you build the image.
For more information, see Creating and sharing Lambda layers (p. 101).
Extension
Lambda extensions enable you to augment your functions. For example, you can use extensions to
integrate your functions with your preferred monitoring, observability, security, and governance tools.
You can choose from a broad set of tools that AWS Lambda Partners provides, or you can create your
own Lambda extensions (p. 209).
An internal extension runs in the runtime process and shares the same lifecycle as the runtime. An
external extension runs as a separate process in the execution environment. The external extension is
initialized before the function is invoked, runs in parallel with the function's runtime, and continues to
run after the function invocation is complete.
Concurrency
Concurrency is the number of requests that your function is serving at any given time. When your
function is invoked, Lambda provisions an instance of it to process the event. When the function code
finishes running, it can handle another request. If the function is invoked again while a request is still
being processed, another instance is provisioned, increasing the function's concurrency.
Concurrency is subject to quotas (p. 44) at the AWS Region level. You can configure individual
functions to limit their concurrency, or to enable them to reach a specific level of concurrency. For more
information, see Managing concurrency for a Lambda function (p. 84).
Qualifier
When you invoke or view a function, you can include a qualifier to specify a version or alias. A version
is an immutable snapshot of a function's code and configuration that has a numerical qualifier. For
example, my-function:1. An alias is a pointer to a version that you can update to map to a different
version, or split traffic between two versions. For example, my-function:BLUE. You can use versions
and aliases together to provide a stable interface for clients to invoke your function.
31
AWS Lambda Developer Guide
Features
Lambda features
Lambda provides a management console and API for managing and invoking functions. It provides
runtimes that support a standard set of features so that you can easily switch between languages and
frameworks, depending on your needs. In addition to functions, you can also create versions, aliases,
layers, and custom runtimes.
Features
• Programming model (p. 32)
• Scaling (p. 34)
• Concurrency controls (p. 35)
• Asynchronous invocation (p. 36)
• Event source mappings (p. 37)
• Destinations (p. 38)
• Function blueprints (p. 39)
• Testing and deployment tools (p. 40)
• Application templates (p. 40)
Programming model
Authoring specifics vary between runtimes, but all runtimes share a common programming model that
defines the interface between your code and the runtime code. You tell the runtime which method to
run by defining a handler in the function configuration, and the runtime runs that method. The runtime
passes in objects to the handler that contain the invocation event and the context, such as the function
name and request ID.
When the handler finishes processing the first event, the runtime sends it another. The function's class
stays in memory, so clients and variables that are declared outside of the handler method in initialization
code can be reused. To save processing time on subsequent events, create reusable resources like AWS
SDK clients during initialization. Once initialized, each instance of your function can process thousands of
requests.
When AWS X-Ray tracing (p. 451) is enabled, the runtime records separate subsegments for
initialization and execution.
32
AWS Lambda Developer Guide
Programming model
Your function also has access to local storage in the /tmp directory. Instances of your function that are
serving requests remain active for a few hours before being recycled.
The runtime captures logging output from your function and sends it to Amazon CloudWatch Logs. In
addition to logging your function's output, the runtime also logs entries when function invocation starts
and ends. This includes a report log with the request ID, billed duration, initialization duration, and other
details. If your function throws an error, the runtime returns that error to the invoker.
Note
Logging is subject to CloudWatch Logs quotas. Log data can be lost due to throttling or, in some
cases, when an instance of your function is stopped.
For a hands-on introduction to the programming model in your preferred programming language, see
the following chapters.
Lambda scales your function by running additional instances of it as demand increases, and by stopping
instances as demand decreases. Unless noted otherwise, incoming requests might be processed out of
order or concurrently. Store your application's state in other services, and don't rely on instances of your
function being long lived. Use local storage and class-level objects to increase performance, but keep
33
AWS Lambda Developer Guide
Scaling
to a minimum the size of your deployment package and the amount of data that you transfer onto the
execution environment.
Scaling
Lambda manages the infrastructure that runs your code, and scales automatically in response to
incoming requests. When your function is invoked more quickly than a single instance of your function
can process events, Lambda scales up by running additional instances. When traffic subsides, inactive
instances are frozen or stopped. You only pay for the time that your function is initializing or processing
events.
For more information, see AWS Lambda function scaling (p. 153).
34
AWS Lambda Developer Guide
Concurrency controls
Concurrency controls
Use concurrency settings to ensure that your production applications are highly available and highly
responsive. To prevent a function from using too much concurrency, and to reserve a portion of your
account's available concurrency for a function, use reserved concurrency. Reserved concurrency splits the
pool of available concurrency into subsets. A function with reserved concurrency only uses concurrency
from its dedicated pool.
To enable functions to scale without fluctuations in latency, use provisioned concurrency. For functions
that take a long time to initialize, or that require extremely low latency for all invocations, provisioned
concurrency enables you to pre-initialize instances of your function and keep them running at all times.
Lambda integrates with Application Auto Scaling to support autoscaling for provisioned concurrency
based on utilization.
35
AWS Lambda Developer Guide
Asynchronous invocation
For more information, see Managing concurrency for a Lambda function (p. 84).
Asynchronous invocation
When you invoke a function, you can choose to invoke it synchronously or asynchronously. With
synchronous invocation (p. 138), you wait for the function to process the event and return a response.
With asynchronous invocation, Lambda queues the event for processing and returns a response
immediately.
36
AWS Lambda Developer Guide
Event source mappings
For asynchronous invocations, Lambda handles retries if the function returns an error or is throttled. To
customize this behavior, you can configure error handling settings on a function, version, or alias. You
can also configure Lambda to send events that failed processing to a dead-letter queue, or to send a
record of any invocation to a destination (p. 38).
37
AWS Lambda Developer Guide
Destinations
Event source mappings maintain a local queue of unprocessed items and handle retries if the function
returns an error or is throttled. You can configure an event source mapping to customize batching
behavior and error handling, or to send a record of items that fail processing to a destination.
For more information, see AWS Lambda event source mappings (p. 148).
Destinations
A destination is an AWS resource that receives invocation records for a function. For asynchronous
invocation (p. 36), you can configure Lambda to send invocation records to a queue, topic, function,
or event bus. You can configure separate destinations for successful invocations and events that failed
processing. The invocation record contains details about the event, the function's response, and the
reason that the record was sent.
38
AWS Lambda Developer Guide
Function blueprints
For event source mappings (p. 37) that read from streams, you can configure Lambda to send a record
of batches that failed processing to a queue or topic. A failure record for an event source mapping
contains metadata about the batch, and it points to the items in the stream.
For more information, see Configuring destinations for asynchronous invocation (p. 142) and the error
handling sections of Using AWS Lambda with Amazon DynamoDB (p. 313) and Using AWS Lambda
with Amazon Kinesis (p. 365).
Function blueprints
When you create a function in the Lambda console, you can choose to start from scratch, use a
blueprint, use a container image (p. 41), or deploy an application from the AWS Serverless Application
Repository. A blueprint provides sample code that shows how to use Lambda with an AWS service or a
popular third-party application. Blueprints include sample code and function configuration presets for
Node.js and Python runtimes.
Blueprints are provided for use under the Amazon Software License. They are available only in the
Lambda console.
39
AWS Lambda Developer Guide
Testing and deployment tools
To set up the Docker CLI, see Get Docker on the Docker Docs website. For an introduction to using Docker
with AWS, see Getting started with Amazon ECR using the AWS CLI in the Amazon Elastic Container
Registry User Guide.
Application templates
You can use the Lambda console to create an application with a continuous delivery pipeline. Application
templates in the Lambda console include code for one or more functions, an application template that
defines functions and supporting AWS resources, and an infrastructure template that defines an AWS
CodePipeline pipeline. The pipeline has build and deploy stages that run every time you push changes to
the included Git repository.
Application templates are provided for use under the MIT No Attribution license. They are available only
in the Lambda console.
For more information, see Creating an application with continuous delivery in the Lambda
console (p. 178).
40
AWS Lambda Developer Guide
Deployment packages
Topics
• Container images (p. 41)
• .zip file archives (p. 41)
• Layers (p. 42)
• Using other AWS services to build a deployment package (p. 42)
Container images
A container image includes the base operating system, the runtime, Lambda extensions, your application
code and its dependencies. You can also add static data, such as machine learning models, into the
image.
Lambda provides a set of open-source base images that you can use to build your container image.
To create and test container images, you can use the AWS Serverless Application Model (AWS SAM)
command line interface (CLI) or native container tools such as the Docker CLI.
You upload your container images to Amazon Elastic Container Registry (Amazon ECR), a managed AWS
container image registry service. To deploy the image to your function, you specify the Amazon ECR
image URL using the Lambda console, the Lambda API, command line tools, or the AWS SDKs.
For more information about Lambda container images, see Using container images with
Lambda (p. 247).
When you create functions with the Lambda API, command line tools, or the AWS SDKs, you must create
a deployment package. You also must create a deployment package if your function uses a compiled
language, or to add dependencies to your function. To deploy your function's code, you upload the
deployment package from Amazon Simple Storage Service (Amazon S3) or your local machine.
You can upload a .zip file as your deployment package using the Lambda console, AWS Command Line
Interface (AWS CLI), or to an Amazon Simple Storage Service (Amazon S3) bucket.
41
AWS Lambda Developer Guide
Layers
5. Choose Save.
• Deploy Node.js Lambda functions with .zip file archives (p. 489)
• Deploy Python Lambda functions with .zip file archives (p. 515)
• Deploy Ruby Lambda functions with .zip file archives (p. 551)
• Deploy Java Lambda functions with .zip or JAR file archives (p. 578)
• Deploy Go Lambda functions with .zip file archives (p. 617)
• Deploy C# Lambda functions with .zip file archives (p. 645)
• Deploy PowerShell Lambda functions with .zip file archives (p. 669)
Using Amazon S3
You can upload a .zip file as your deployment package using Amazon Simple Storage Service (Amazon
S3). For more information, see the section called “Using other AWS services”.
Layers
If you deploy your function code using a .zip file archive, you can use Lambda layers as a distribution
mechanism for libraries, custom runtimes, and other function dependencies. Layers enable you to
manage your in-development function code independently from the unchanging code and resources that
it uses. You can configure your function to use layers that you create, layers that AWS provides, or layers
from other AWS customers.
You do not use layers with container images. Instead, you package your preferred runtime, libraries, and
other dependencies into the container image when you build the image.
For more information about layers, see Creating and sharing Lambda layers (p. 101).
For more information, see sam build in the AWS Serverless Application Model Developer Guide.
42
AWS Lambda Developer Guide
Using other AWS services
You can create a deployment package and upload the .zip file to your Amazon S3 bucket in the AWS
Region where you want to create a Lambda function. When you create your Lambda function, specify the
S3 bucket name and object key name on the Lambda console, or using the AWS CLI.
To create a bucket using the Amazon S3 console, see How do I create an S3 Bucket? in the Amazon
Simple Storage Service Console User Guide.
43
AWS Lambda Developer Guide
Quotas
Lambda quotas
Lambda sets quotas for the amount of compute and storage resources that you can use to run and store
functions. The following quotas apply per AWS Region and can be increased. For more information, see
Requesting a quota increase in the Service Quotas User Guide.
Storage for functions defined as container images. See Amazon ECR service quotas.
These images are stored in Amazon ECR.
For details on concurrency and how Lambda scales your function concurrency in response to traffic, see
AWS Lambda function scaling (p. 153).
The following quotas apply to function configuration, deployments, and execution. They cannot be
changed.
Resource Quota
Function burst concurrency (p. 153) 500 - 3000 (varies per Region)
256 KB (asynchronous)
Deployment package (.zip file archive) (p. 41) size 50 MB (zipped, for direct upload)
3 MB (console editor)
44
AWS Lambda Developer Guide
Quotas
Resource Quota
Resource Quota
Invocation requests per Region (requests per second) 10 x concurrent executions quota
(synchronous (p. 138), all sources)
Invocation requests per Region (requests per second) for Unlimited requests accepted. Execution
asynchronous AWS service sources (p. 257) rate is based on concurrency available
to the function. See Asynchronous
invocation (p. 140).
Remainder of the control plane API requests (excludes 15 requests per second
invocation, GetFunction, and GetPolicy requests)
Quotas for other services, such as AWS Identity and Access Management (IAM), Amazon CloudFront
(Lambda@Edge), and Amazon Virtual Private Cloud (Amazon VPC), can impact your Lambda functions.
For more information, see AWS service quotas in the Amazon Web Services General Reference, and Using
AWS Lambda with other services (p. 257).
45
AWS Lambda Developer Guide
A Lambda function also has a policy, called an execution role (p. 47), that grants it permission to
access AWS services and resources. At a minimum, your function needs access to Amazon CloudWatch
Logs for log streaming. If you use AWS X-Ray to trace your function (p. 451), or your function accesses
services with the AWS SDK, you grant it permission to call them in the execution role. Lambda also
uses the execution role to get permission to read from event sources when you use an event source
mapping (p. 148) to trigger your function.
Note
If your function needs network access to a resource like a relational database that isn't
accessible through AWS APIs or the internet, configure it to connect to your VPC (p. 111).
Use resource-based policies (p. 51) to give other accounts and AWS services permission to use your
Lambda resources. Lambda resources include functions, versions, aliases, and layer versions. Each of
these resources has a permissions policy that applies when the resource is accessed, in addition to any
policies that apply to the user. When an AWS service like Amazon S3 calls your Lambda function, the
resource-based policy gives it access.
To manage permissions for users and applications in your accounts, use the managed policies that
Lambda provides (p. 56), or write your own. The Lambda console uses multiple services to get
information about your function's configuration and triggers. You can use the managed policies as-is, or
as a starting point for more restrictive policies.
You can restrict user permissions by the resource an action affects and, in some cases, by additional
conditions. For example, you can specify a pattern for the Amazon Resource Name (ARN) of a function
that requires a user to include their user name in the name of functions that they create. Additionally,
you can add a condition that requires that the user configure functions to use a specific layer to, for
example, pull in logging software. For the resources and conditions that are supported by each action,
see Resources and Conditions (p. 61).
For more information about IAM, see What is IAM? in the IAM User Guide.
For more information about applying security principles to Lambda applications, see Security in the
Lambda operator guide.
Topics
• AWS Lambda execution role (p. 47)
• Using resource-based policies for AWS Lambda (p. 51)
• Identity-based IAM policies for Lambda (p. 56)
• Resources and conditions for Lambda actions (p. 61)
• Using permissions boundaries for AWS Lambda applications (p. 67)
46
AWS Lambda Developer Guide
Execution role
5. Choose a service from the dropdown list to see permissions related to that service.
47
AWS Lambda Developer Guide
Creating an execution role in the IAM console
You can add or remove permissions from a function's execution role at any time, or configure your
function to use a different role. Add permissions for any services that your function calls with the AWS
SDK, and for services that Lambda uses to enable optional features.
When you add permissions to your function, make an update to its code or configuration as well. This
forces running instances of your function, which have out-of-date credentials, to stop and be replaced.
Topics
• Creating an execution role in the IAM console (p. 48)
• Grant least privilege access to your Lambda execution role (p. 48)
• Managing roles with the IAM API (p. 49)
• AWS managed policies for Lambda features (p. 50)
For detailed instructions, see Creating a role for an AWS service (console) in the IAM User Guide.
Use IAM Access Analyzer to help identify the required permissions for the IAM execution role policy. IAM
Access Analyzer reviews your AWS CloudTrail logs over the date range that you specify and generates
a policy template with only the permissions that the function used during that time. You can use the
template to create a managed policy with fine-grained permissions, and then attach it to the IAM role.
That way, you grant only the permissions that the role needs to interact with AWS resources for your
specific use case.
To learn more, see Generate policies based on access activity in the IAM User Guide.
48
AWS Lambda Developer Guide
Managing roles with the IAM API
{
"Role": {
"Path": "/",
"RoleName": "lambda-ex",
"RoleId": "AROAQFOXMPL6TZ6ITKWND",
"Arn": "arn:aws:iam::123456789012:role/lambda-ex",
"CreateDate": "2020-01-17T23:19:12Z",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
}
}
The trust-policy.json file is a JSON file in the current directory that defines the trust policy for
the role. This trust policy allows Lambda to use the role's permissions by giving the service principal
lambda.amazonaws.com permission to call the AWS Security Token Service AssumeRole action.
Example trust-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
You can also specify the trust policy inline. Requirements for escaping quotes in the JSON string vary
depending on your shell.
To add permissions to the role, use the attach-policy-to-role command. Start by adding the
AWSLambdaBasicExecutionRole managed policy.
49
AWS Lambda Developer Guide
AWS managed policies for Lambda features
For some features, the Lambda console attempts to add missing permissions to your execution role in a
customer managed policy. These policies can become numerous. To avoid creating extra policies, add the
relevant AWS managed policies to your execution role before enabling features.
When you use an event source mapping (p. 148) to invoke your function, Lambda uses the execution
role to read event data. For example, an event source mapping for Kinesis reads events from a data
stream and sends them to your function in batches. You can use event source mappings with the
following services:
In addition to the AWS managed policies, the Lambda console provides templates for creating a custom
policy with permissions for additional use cases. When you create a function in the Lambda console,
you can choose to create a new execution role with permissions from one or more templates. These
templates are also applied automatically when you create a function from a blueprint, or when you
configure options that require access to other services. Example templates are available in this guide's
GitHub repository.
50
AWS Lambda Developer Guide
Resource-based policies
For Lambda functions, you can grant an account permission (p. 53) to invoke or manage a function.
You can add multiple statements to grant access to several accounts, or let any account invoke your
function. You can also use the policy to grant invoke permission to an AWS service (p. 52) that invokes
a function in response to activity in your account.
{
"Version": "2012-10-17",
"Id": "default",
"Statement": [
{
"Sid": "lambda-allow-s3-my-function",
"Effect": "Allow",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-2:123456789012:function:my-function”,
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "123456789012"
},
"ArnLike": {
"AWS:SourceArn": "arn:aws:s3:::my-bucket"
}
}
}
]
}
For Lambda layers, you can only use a resource-based policy on a specific layer version, instead of the
entire layer. In addition to policies that grant permission to a single account or multiple accounts, for
layers, you can also grant permission to all accounts in an organization.
Note
You can only update resource-based policies for Lambda resources within the scope of the
AddPermission (p. 743) and AddLayerVersionPermission (p. 739) API actions. Currently, you
can't author policies for your Lambda resources in JSON, or use conditions that don't map to
parameters for those actions.
51
AWS Lambda Developer Guide
Granting function access to AWS services
Resource-based policies apply to a single function, version, alias, or layer version. They grant permission
to one or more services and accounts. For trusted accounts that you want to have access to multiple
resources, or to use API actions that resource-based policies don't support, you can use cross-account
roles (p. 56).
Topics
• Granting function access to AWS services (p. 52)
• Granting function access to other accounts (p. 53)
• Granting layer access to other accounts (p. 54)
• Cleaning up resource-based policies (p. 55)
Add a statement with the add-permission command. The simplest resource-based policy statement
allows a service to invoke a function. The following command grants Amazon SNS permission to invoke a
function named my-function.
{"Sid":"sns","Effect":"Allow","Principal":
{"Service":"sns.amazonaws.com"},"Action":"lambda:InvokeFunction","Resource":"arn:aws:lambda:us-
east-2:123456789012:function:my-function"}
This lets Amazon SNS call the lambda:Invoke API for the function, but it doesn't restrict the Amazon
SNS topic that triggers the invocation. To ensure that your function is only invoked by a specific resource,
specify the Amazon Resource Name (ARN) of the resource with the source-arn option. The following
command only allows Amazon SNS to invoke the function for subscriptions to a topic named my-topic.
Some services can invoke functions in other accounts. If you specify a source ARN that has your account
ID in it, that isn't an issue. For Amazon S3, however, the source is a bucket whose ARN doesn't have
an account ID in it. It's possible that you could delete the bucket and another account could create a
bucket with the same name. Use the source-account option with your account ID to ensure that only
resources in your account can invoke the function.
52
AWS Lambda Developer Guide
Granting function access to other accounts
{"Sid":"xaccount","Effect":"Allow","Principal":
{"AWS":"arn:aws:iam::210987654321:root"},"Action":"lambda:InvokeFunction","Resource":"arn:aws:lambda:us
east-2:123456789012:function:my-function"}
The resource-based policy grants permission for the other account to access the function, but doesn't
allow users in that account to exceed their permissions. Users in the other account must have the
corresponding user permissions (p. 56) to use the Lambda API.
To limit access to a user or role in another account, specify the full ARN of the identity as the principal.
For example, arn:aws:iam::123456789012:user/developer.
The alias (p. 97) limits which version the other account can invoke. It requires the other account to
include the alias in the function ARN.
{
"StatusCode": 200,
"ExecutedVersion": "1"
}
The function owner can then update the alias to point to a new version without the caller needing to
change the way they invoke your function. This ensures that the other account doesn't need to change its
code to use the new version, and it only has permission to invoke the version of the function associated
with the alias.
You can grant cross-account access for most API actions that operate on an existing function (p. 64).
For example, you could grant access to lambda:ListAliases to let an account get a list of aliases, or
lambda:GetFunction to let them download your function code. Add each permission separately, or
use lambda:* to grant access to all actions for the specified function.
Cross-account APIs
Currently, Lambda doesn’t currently support cross-account actions for all of its APIs via resource-based
policies. The following APIs are supported:
53
AWS Lambda Developer Guide
Granting layer access to other accounts
To grant other accounts permission for multiple functions, or for actions that don't operate on a
function, we recommend that you use IAM roles (p. 56).
e210ffdc-e901-43b0-824b-5fcd0dd26d16 {"Sid":"xaccount","Effect":"Allow","Principal":
{"AWS":"arn:aws:iam::210987654321:root"},"Action":"lambda:GetLayerVersion","Resource":"arn:aws:lambda:u
east-2:123456789012:layer:xray-sdk-nodejs:1"}
Permissions apply only to a single layer version. Repeat the process each time that you create a new layer
version.
To grant permission to all accounts in an organization, use the organization-id option. The following
example grants all accounts in an organization permission to use version 3 of a layer.
b0cd9796-d4eb-4564-939f-de7fe0b42236 {"Sid":"engineering-
org","Effect":"Allow","Principal":"*","Action":"lambda:GetLayerVersion","Resource":"arn:aws:lambda:us-
east-2:123456789012:layer:my-layer:3","Condition":{"StringEquals":{"aws:PrincipalOrgID":"o-
t194hfs8cz"}}}"
To grant permission to all AWS accounts, use * for the principal, and omit the organization ID. For
multiple accounts or organizations, you need to add multiple statements.
54
AWS Lambda Developer Guide
Cleaning up resource-based policies
{"Version":"2012-10-17","Id":"default","Statement":
[{"Sid":"sns","Effect":"Allow","Principal":
{"Service":"s3.amazonaws.com"},"Action":"lambda:InvokeFunction","Resource":"arn:aws:lambda:us-
east-2:123456789012:function:my-function","Condition":{"ArnLike":
{"AWS:SourceArn":"arn:aws:sns:us-east-2:123456789012:lambda*"}}}]} 7c681fc9-b791-4e91-
acdf-eb847fdaa0f0
For versions and aliases, append the version number or alias to the function name.
b0cd9796-d4eb-4564-939f-de7fe0b42236 {"Sid":"engineering-
org","Effect":"Allow","Principal":"*","Action":"lambda:GetLayerVersion","Resource":"arn:aws:lambda:us-
west-2:123456789012:layer:my-layer:3","Condition":{"StringEquals":{"aws:PrincipalOrgID":"o-
t194hfs8cz"}}}"
55
AWS Lambda Developer Guide
User policies
Lambda provides AWS managed policies that grant access to Lambda API actions and, in some cases,
access to other AWS services used to develop and manage Lambda resources. Lambda updates these
managed policies as needed to ensure that your users have access to new features when they're released.
Note
The AWS managed policies AWSLambdaFullAccess and AWSLambdaReadOnlyAccess will be
deprecated on March 1, 2021. After this date, you cannot attach these policies to new IAM users.
For more information, see the related troubleshooting topic (p. 709).
• AWSLambda_FullAccess – Grants full access to Lambda actions and other AWS services used to
develop and maintain Lambda resources. This policy was created by scoping down the previous policy
AWSLambdaFullAccess.
• AWSLambda_ReadOnlyAccess – Grants read-only access to Lambda resources. This policy was created
by scoping down the previous policy AWSLambdaReadOnlyAccess.
• AWSLambdaRole – Grants permissions to invoke Lambda functions.
AWS managed policies grant permission to API actions without restricting the Lambda functions or layers
that a user can modify. For finer-grained control, you can create your own policies that limit the scope of
a user's permissions.
Sections
• Function development (p. 56)
• Layer development and use (p. 59)
• Cross-account roles (p. 60)
• Condition keys for VPC settings (p. 60)
Function development
The following shows an example of a permissions policy with limited scope. It allows a user to create and
manage Lambda functions named with a designated prefix (intern-), and configured with a designated
execution role.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadOnlyPermissions",
"Effect": "Allow",
"Action": [
"lambda:GetAccountSettings",
"lambda:ListFunctions",
"lambda:ListTags",
"lambda:GetEventSourceMapping",
"lambda:ListEventSourceMappings",
"iam:ListRoles"
],
56
AWS Lambda Developer Guide
Function development
"Resource": "*"
},
{
"Sid": "DevelopFunctions",
"Effect": "Allow",
"NotAction": [
"lambda:AddPermission",
"lambda:PutFunctionConcurrency"
],
"Resource": "arn:aws:lambda:*:*:function:intern-*"
},
{
"Sid": "DevelopEventSourceMappings",
"Effect": "Allow",
"Action": [
"lambda:DeleteEventSourceMapping",
"lambda:UpdateEventSourceMapping",
"lambda:CreateEventSourceMapping"
],
"Resource": "*",
"Condition": {
"StringLike": {
"lambda:FunctionArn": "arn:aws:lambda:*:*:function:intern-*"
}
}
},
{
"Sid": "PassExecutionRole",
"Effect": "Allow",
"Action": [
"iam:ListRolePolicies",
"iam:ListAttachedRolePolicies",
"iam:GetRole",
"iam:GetRolePolicy",
"iam:PassRole",
"iam:SimulatePrincipalPolicy"
],
"Resource": "arn:aws:iam::*:role/intern-lambda-execution-role"
},
{
"Sid": "ViewLogs",
"Effect": "Allow",
"Action": [
"logs:*"
],
"Resource": "arn:aws:logs:*:*:log-group:/aws/lambda/intern-*"
}
]
}
The permissions in the policy are organized into statements based on the resources and
conditions (p. 61) that they support.
• ReadOnlyPermissions – The Lambda console uses these permissions when you browse and view
functions. They don't support resource patterns or conditions.
"Action": [
"lambda:GetAccountSettings",
"lambda:ListFunctions",
"lambda:ListTags",
"lambda:GetEventSourceMapping",
"lambda:ListEventSourceMappings",
"iam:ListRoles"
57
AWS Lambda Developer Guide
Function development
],
"Resource": "*"
• DevelopFunctions – Use any Lambda action that operates on functions prefixed with intern-,
except AddPermission and PutFunctionConcurrency. AddPermission modifies the resource-
based policy (p. 51) on the function and can have security implications. PutFunctionConcurrency
reserves scaling capacity for a function and can take capacity away from other functions.
"NotAction": [
"lambda:AddPermission",
"lambda:PutFunctionConcurrency"
],
"Resource": "arn:aws:lambda:*:*:function:intern-*"
"Action": [
"lambda:DeleteEventSourceMapping",
"lambda:UpdateEventSourceMapping",
"lambda:CreateEventSourceMapping"
],
"Resource": "*",
"Condition": {
"StringLike": {
"lambda:FunctionArn": "arn:aws:lambda:*:*:function:intern-*"
}
}
"Action": [
"iam:ListRolePolicies",
"iam:ListAttachedRolePolicies",
"iam:GetRole",
"iam:GetRolePolicy",
"iam:PassRole",
"iam:SimulatePrincipalPolicy"
],
"Resource": "arn:aws:iam::*:role/intern-lambda-execution-role"
• ViewLogs – Use CloudWatch Logs to view logs for functions that are prefixed with intern-.
"Action": [
"logs:*"
],
"Resource": "arn:aws:logs:*:*:log-group:/aws/lambda/intern-*"
This policy allows a user to get started with Lambda, without putting other users' resources at risk. It
doesn't allow a user to configure a function to be triggered by or call other AWS services, which requires
broader IAM permissions. It also doesn't include permission to services that don't support limited-scope
policies, like CloudWatch and X-Ray. Use the read-only policies for these services to give the user access
to metrics and trace data.
58
AWS Lambda Developer Guide
Layer development and use
When you configure triggers for your function, you need access to use the AWS service that invokes
your function. For example, to configure an Amazon S3 trigger, you need permission to use the
Amazon S3 actions that manage bucket notifications. Many of these permissions are included in the
AWSLambdaFullAccess managed policy. Example policies are available in this guide's GitHub repository.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublishLayers",
"Effect": "Allow",
"Action": [
"lambda:PublishLayerVersion"
],
"Resource": "arn:aws:lambda:*:*:layer:test-*"
},
{
"Sid": "ManageLayerVersions",
"Effect": "Allow",
"Action": [
"lambda:GetLayerVersion",
"lambda:DeleteLayerVersion"
],
"Resource": "arn:aws:lambda:*:*:layer:test-*:*"
}
]
}
You can also enforce layer use during function creation and configuration with the lambda:Layer
condition. For example, you can prevent users from using layers published by other accounts. The
following policy adds a condition to the CreateFunction and UpdateFunctionConfiguration
actions to require that any layers specified come from account 123456789012.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ConfigureFunctions",
"Effect": "Allow",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Resource": "*",
"Condition": {
"ForAllValues:StringLike": {
"lambda:Layer": [
"arn:aws:lambda:*:123456789012:layer:*:*"
]
}
}
}
]
59
AWS Lambda Developer Guide
Cross-account roles
To ensure that the condition applies, verify that no other statements grant the user permission to these
actions.
Cross-account roles
You can apply any of the preceding policies and statements to a role, which you can then share with
another account to give it access to your Lambda resources. Unlike an IAM user, a role doesn't have
credentials for authentication. Instead, it has a trust policy that specifies who can assume the role and
use its permissions.
You can use cross-account roles to give accounts that you trust access to Lambda actions and
resources. If you just want to grant permission to invoke a function or use a layer, use resource-based
policies (p. 51) instead.
For more information, see IAM roles in the IAM User Guide.
For more information, see the section called “Using IAM condition keys for VPC settings” (p. 113).
60
AWS Lambda Developer Guide
Resources and conditions
Every IAM policy statement grants permission to an action that's performed on a resource. When the
action doesn't act on a named resource, or when you grant permission to perform the action on all
resources, the value of the resource in the policy is a wildcard (*). For many API actions, you can restrict
the resources that a user can modify by specifying the Amazon Resource Name (ARN) of a resource, or an
ARN pattern that matches multiple resources.
• Function – arn:aws:lambda:us-west-2:123456789012:function:my-function
• Function version – arn:aws:lambda:us-west-2:123456789012:function:my-function:1
• Function alias – arn:aws:lambda:us-west-2:123456789012:function:my-function:TEST
• Event source mapping – arn:aws:lambda:us-west-2:123456789012:event-source-
mapping:fa123456-14a1-4fd2-9fec-83de64ad683de6d47
• Layer – arn:aws:lambda:us-west-2:123456789012:layer:my-layer
• Layer version – arn:aws:lambda:us-west-2:123456789012:layer:my-layer:1
For example, the following policy allows a user in account 123456789012 to invoke a function named
my-function in the US West (Oregon) Region.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Invoke",
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "arn:aws:lambda:us-west-2:123456789012:function:my-function"
}
]
}
This is a special case where the action identifier (lambda:InvokeFunction) differs from the API
operation (Invoke (p. 840)). For other actions, the action identifier is the operation name prefixed by
lambda:.
Conditions are an optional policy element that applies additional logic to determine if an action is
allowed. In addition to common conditions supported by all actions, Lambda defines condition types
that you can use to restrict the values of additional parameters on some actions.
For example, the lambda:Principal condition lets you restrict the service or account that a user can
grant invocation access to on a function's resource-based policy. The following policy lets a user grant
permission to SNS topics to invoke a function named test.
61
AWS Lambda Developer Guide
Function resource names
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ManageFunctionPolicy",
"Effect": "Allow",
"Action": [
"lambda:AddPermission",
"lambda:RemovePermission"
],
"Resource": "arn:aws:lambda:us-west-2:123456789012:function:test:*",
"Condition": {
"StringEquals": {
"lambda:Principal": "sns.amazonaws.com"
}
}
}
]
}
The condition requires that the principal is Amazon SNS and not another service or account. The resource
pattern requires that the function name is test and includes a version number or alias. For example,
test:v1.
For more information on resources and conditions for Lambda and other AWS services, see Actions,
resources, and condition keys in the IAM User Guide.
Sections
• Function resource names (p. 62)
• Function actions (p. 64)
• Event source mapping actions (p. 66)
• Layer actions (p. 66)
When making Lambda API calls, users can specify a version or alias by passing a version ARN or alias ARN
in the FunctionName parameter, or by setting a value in the Qualifier parameter. Lambda makes
authorization decisions by comparing the resource element in the IAM policy with the FunctionName
passed in the API calls.
You must use the correct function ARN types in your policies to achieve the results that you expect,
especially in policies that deny access. We recommend that you follow the best practices for using Deny
statements with functions.
62
AWS Lambda Developer Guide
Function resource names
The following sections provide example policy statements for each of the policy objectives.
Note
You can use only identity-based policies to deny specific function resources. Currently, Lambda
does not support the Deny effect in resource-based policies.
For the action list in a policy statement, you can add any of the actions defined by Lambda that act on a
function resource.
{
"Version": "2020-07-20",
"Statement": [
{
"Effect": "Deny",
"Action": [
"lambda:GetFunctionConfiguration"
],
"Resource": "arn:aws:lambda:us-west-2:123456789012:function:my-function*"
}
]
}
The following identity-based policy statement denies access to the lambda:InvokeFunction action in
the my-alias alias of the my-function function.
63
AWS Lambda Developer Guide
Function actions
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenySpecificAlias",
"Effect": "Deny",
"Action": "lambda:InvokeFunction",
"Resource": [
"arn:aws:lambda:us-west-2:123456789012:function:my-function:my-alias",
"arn:aws:lambda:us-west-2:123456789012:function:my-function"
]
}
]
}
The following identity-based policy statement denies access to the invoke action in version 1 of the my-
function function.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenySpecificFunctionVersion",
"Effect": "Deny",
"Action": [
"lambda:InvokeFunction"
],
"Resource": [
"arn:aws:lambda:us-west-2:123456789012:function:my-function:1",
"arn:aws:lambda:us-west-2:123456789012:function:my-function"
]
}
]
}
Function actions
Actions that operate on a function can be restricted to a specific function by function, version, or alias
ARN, as described in the following table. Actions that don't support resource restrictions can only be
granted for all resources (*).
64
AWS Lambda Developer Guide
Function actions
Functions
Function alias
lambda:VpcIds
lambda:SubnetIds
lambda:SecurityGroupIds
65
AWS Lambda Developer Guide
Event source mapping actions
For these actions, the resource is the event source mapping, so Lambda provides a condition that lets you
restrict permission based on the function that the event source mapping invokes.
Layer actions
Layer actions let you restrict the layers that a user can manage or use with a function. Actions related
to layer use and permissions act on a version of a layer, while PublishLayerVersion acts on a layer
name. You can use either with wildcards to restrict the layers that a user can work with by name.
Note
Note: the GetLayerVersion (p. 827) action also covers GetLayerVersionByArn (p. 830).
Lambda does not support GetLayerVersionByArn as an IAM action.
Layers
66
AWS Lambda Developer Guide
Permissions boundaries
The application templates in the Lambda console include a global property that applies a permissions
boundary to all functions that they create.
Globals:
Function:
PermissionsBoundary: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/
${AppId}-${AWS::Region}-PermissionsBoundary'
The boundary limits the permissions of the functions' roles. You can add permissions to a function's
execution role in the template, but that permission is only effective if it's also allowed by the permissions
boundary. The role that AWS CloudFormation assumes to deploy the application enforces the use of the
permissions boundary. That role only has permission to create and pass roles that have the application's
permissions boundary attached.
By default, an application's permissions boundary enables functions to perform actions on the resources
in the application. For example, if the application includes an Amazon DynamoDB table, the boundary
allows access to any API action that can be restricted to operate on specific tables with resource-level
permissions. You can only use actions that don't support resource-level permissions if they're specifically
permitted in the boundary. These include Amazon CloudWatch Logs and AWS X-Ray API actions for
logging and tracing.
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"*"
],
"Resource": [
"arn:aws:lambda:us-east-2:123456789012:function:my-app-getAllItemsFunction-
*",
"arn:aws:lambda:us-east-2:123456789012:function:my-app-getByIdFunction-*",
"arn:aws:lambda:us-east-2:123456789012:function:my-app-putItemFunction-*",
"arn:aws:dynamodb:us-east-1:123456789012:table/my-app-SampleTable-*"
],
"Effect": "Allow",
"Sid": "StackResources"
},
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:PutLogEvents",
"xray:Put*"
],
"Resource": "*",
67
AWS Lambda Developer Guide
Permissions boundaries
"Effect": "Allow",
"Sid": "StaticPermissions"
},
...
]
}
To access other resources or API actions, you or an administrator must expand the permissions boundary
to include those resources. You might also need to update the execution role or deployment role of an
application to allow the use of additional actions.
• Permissions boundary – Extend the application's permissions boundary when you add resources to
your application, or the execution role needs access to more actions. In IAM, add resources to the
boundary to allow the use of API actions that support resource-level permissions on that resource's
type. For actions that don't support resource-level permissions, add them in a statement that isn't
scoped to any resource.
• Execution role – Extend a function's execution role when it needs to use additional actions. In the
application template, add policies to the execution role. The intersection of permissions in the
boundary and execution role is granted to the function.
• Deployment role – Extend the application's deployment role when it needs additional permissions
to create or configure resources. In IAM, add policies to the application's deployment role. The
deployment role needs the same user permissions that you need to deploy or update an application in
AWS CloudFormation.
For a tutorial that walks through adding resources to an application and extending its permissions, see
??? (p. 178).
For more information, see Permissions boundaries for IAM entities in the IAM User Guide.
68
AWS Lambda Developer Guide
To keep secrets out of your function code, store them in the function's configuration and read them from
the execution environment during initialization. Environment variables (p. 77) are always encrypted
at rest, and can be encrypted client-side as well. Use environment variables to make your function code
portable by removing connection strings, passwords, and endpoints for external resources.
Versions and aliases (p. 95) are secondary resources that you can create to manage function
deployment and invocation. Publish versions (p. 95) of your function to store its code and
configuration as a separate resource that cannot be changed, and create an alias (p. 97) that points
to a specific version. Then you can configure your clients to invoke a function alias, and update the alias
when you want to point the client to a new version, instead of updating the client.
As you add libraries and other dependencies to your function, creating and uploading a deployment
package can slow down development. Use layers (p. 101) to manage your function's dependencies
independently and keep your deployment package small. You can also use layers to share your own
libraries with other customers and use publicly available layers with your functions.
To use your Lambda function with AWS resources in an Amazon VPC, configure it with security groups
and subnets to create a VPC connection (p. 111). Connecting your function to a VPC lets you access
resources in a private subnet such as relational databases and caches. You can also create a database
proxy (p. 121) for MySQL and Aurora DB instances. A database proxy enables a function to reach high
concurrency levels without exhausting database connections.
To use code signing (p. 130) with your Lambda function, configure it with a code-signing configuration.
When a user attempts to deploy a code package, Lambda checks that the code package has a valid
signature from a trusted publisher. The code-signing configuration includes a set of signing profiles,
which define the trusted publishers for this function.
69
AWS Lambda Developer Guide
Configure console
To manage a function, open the Lambda console Functions page and choose a function.
The function overview shows a visualization of your function and its upstream and downstream
resources.
• Triggers – Triggers are services and resources that you have configured to invoke your function.
Choose Add trigger to create a Lambda event source mapping (p. 148) or to configure a trigger in
another service that the Lambda console integrates with. For details about these services and others,
see Using AWS Lambda with other services (p. 257).
• Layers – Choose the Layers node to add layers (p. 101) to your application. A layer is a .zip file
archive that contains libraries, a custom runtime, or other dependencies.
• Destinations – Add a destination to your function to send details about invocation results to another
service. You can send invocation records when your function is invoked asynchronously (p. 140), or by
an event source mapping (p. 148) that reads from a stream.
By default you are working with the Latest function version. You can modify the following settings in
this version:
Function settings
• Code – The code and dependencies of your function. For scripting languages, you can edit your
function code in the embedded editor (p. 17). To add libraries, or for languages that the editor doesn't
support, or to create a function deployed as a container image, upload a deployment package (p. 41).
If your deployment package is larger than 50 MB, choose Upload a file from Amazon S3.
• Runtime – The Lambda runtime (p. 195) that runs your function.
• Handler – The method that the runtime runs when your function is invoked, such as index.handler.
The first value is the name of the file or module. The second value is the name of the method.
• Environment variables – Key-value pairs that Lambda sets in the execution environment. To extend
your function's configuration outside of code, use environment variables (p. 77).
• Tags – Key-value pairs that Lambda attaches to your function resource. Use tags (p. 134) to organize
Lambda functions into groups for cost reporting and filtering in the Lambda console.
Tags apply to the entire function, including all versions and aliases.
• Execution role – The AWS Identity and Access Management (IAM) role (p. 47) that Lambda assumes
when it runs your function.
70
AWS Lambda Developer Guide
Configure console
Reserved concurrency applies to the entire function, including all versions and aliases.
• Asynchronous invocation – Configure error handling behavior (p. 140) to reduce the number of
retries that Lambda attempts, or the amount of time that unprocessed events stay queued before
Lambda discards them. Configure a dead-letter queue (p. 145) to retain discarded events.
Except as noted in the preceding list, you can only change function settings on the unpublished version
of a function. When you publish a version, code and most settings are locked to ensure a consistent
experience for users of that version. Use aliases (p. 97) to propagate configuration changes in a
controlled manner.
To configure functions with the Lambda API, use the following actions:
For example, to update a function's memory setting with the AWS CLI, use the update-function-
configuration command.
For function configuration best practices, see Function configuration (p. 193).
71
AWS Lambda Developer Guide
Functions defined as container images
When you select an image using an image tag, Lambda translates the tag to the underlying image digest.
To retrieve the digest for your image, use the GetFunctionConfiguration (p. 817) API operation. To
update the function to a newer image version, you must use the Lambda console to update the function
code (p. 74), or use the UpdateFunctionCode (p. 927) API operation. Configuration operations such
as UpdateFunctionConfiguration (p. 936) do not update the function's container image.
Note
In Amazon ECR, if you reassign the image tag to another image, Lambda does not update the
image version.
Topics
• Function version $LATEST (p. 72)
• Container image deployment (p. 72)
• Amazon ECR permissions (p. 72)
• Override the container settings (p. 73)
• Creating a function (console) (p. 73)
• Updating the function code (console) (p. 74)
• Overriding the image parameters (console) (p. 74)
• Using the Lambda API (p. 75)
• AWS CloudFormation (p. 75)
Note that Amazon Elastic Container Registry (Amazon ECR) also uses a latest tag to denote the latest
version of the container image. Be careful not to confuse this tag with the $LATEST function version.
For more information about managing versions, see Lambda function versions (p. 95).
72
AWS Lambda Developer Guide
Override the container settings
{
"Sid": "LambdaECRImageRetrievalPolicy",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
}
For more information about Amazon ECR repository permissions, see Repository policies in the Amazon
Elastic Container Registry User Guide.
If the Amazon ECR repository does not include these permissions, Lambda adds ecr:BatchGetImage
and ecr:GetDownloadUrlForLayer to the container image repository permissions. Lambda can
add these permissions only if the Principal calling Lambda has ecr:getRepositoryPolicy and
ecr:setRepositoryPolicy permissions.
To view or edit your Amazon ECR repository permissions, follow the directions in Setting a repository
policy statement in the Amazon Elastic Container Registry User Guide.
• ENTRYPOINT – Specifies the absolute path of the entry point to the application.
• CMD – Specifies parameters that you want to pass in with ENTRYPOINT.
• WORKDIR – Specifies the absolute path of the working directory.
• ENV – Specifies an environment variable for the Lambda function.
Any values that you provide in the Lambda console or the Lambda API override the values in the
Dockerfile (p. 249).
73
AWS Lambda Developer Guide
Updating the function code (console)
• For Entrypoint, enter the full path of the runtime executable. The following example shows
an entrypoint for a Node.js function:
"/usr/bin/npx", "aws-lambda-ric"
• For Command, enter additional parameters to pass in to the image with Entrypoint. The
following example shows a command for a Node.js function:
"app.handler"
• For Working directory, enter the full path of the working directory for the function. The
following example shows the working directory for an AWS base image for Lambda:
"/var/task"
Note
For the override settings, make sure that you enclose each string in quotation marks ("
").
5. (Optional) Under Permissions, expand Change default execution role. Then, choose to create a new
Execution role, or to use an existing role.
6. Choose Create function.
74
AWS Lambda Developer Guide
Using the Lambda API
For more information, see the section called “Environment variables” (p. 77).
To create a function defined as container image, use the create-function command. Set the
package-type to Image and specify your container image URI using the code parameter. Note that
you must create the function from the same account as the container registry in Amazon EFS.
To update the function code, use the update-function-code command. Specify the container image
location using the image-uri parameter.
Note
You cannot change the package-type of a function.
AWS CloudFormation
You can use AWS CloudFormation to create Lambda functions defined as container images. In your AWS
CloudFormation template, the AWS::Lambda::Function resource specifies the Lambda function. For
descriptions of the properties in the AWS::Lambda::Function resource, see AWS::Lambda::Function in
the AWS CloudFormation User Guide.
In the AWS::Lambda::Function resource, set the following properties to create a function defined as a
container image:
• AWS::Lambda::Function
• PackageType – Set to Image.
• Code – Enter your container image URI in the ImageUri field.
• ImageConfig – (Optional) Override the container image configuration properties.
75
AWS Lambda Developer Guide
AWS CloudFormation
Note
If you declare an ImageConfig property in your AWS CloudFormation template, you must
provide values for all three of the ImageConfig properties.
For more information, see ImageConfig in the AWS CloudFormation User Guide.
76
AWS Lambda Developer Guide
Environment variables
Environment variables are not evaluated prior to the function invocation. Any value you define is
considered a literal string and not expanded. Perform the variable evaluation in your function code.
Sections
• Configuring environment variables (p. 77)
• Configuring environment variables with the API (p. 78)
• Example scenario for environment variables (p. 78)
• Retrieve environment variables (p. 79)
• Defined runtime environment variables (p. 80)
• Securing environment variables (p. 81)
• Sample code and templates (p. 83)
You create an environment variable on your function by defining a key and a value. Your function uses
the name of the key to retrieve the value of environment variable.
Requirements
77
AWS Lambda Developer Guide
Configuring environment variables with the API
The following example sets two environment variables on a function named my-function.
When you apply environment variables with the update-function-configuration command, the
entire contents of the Variables structure is replaced. To retain existing environment variables when
you add a new one, include all existing values in your request.
{
"FunctionName": "my-function",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"Runtime": "nodejs12.x",
"Role": "arn:aws:iam::123456789012:role/lambda-role",
"Environment": {
"Variables": {
"BUCKET": "my-bucket",
"KEY": "file.txt"
}
},
"RevisionId": "0894d3c1-2a3d-4d48-bf7f-abade99f3c15",
...
}
To ensure that the values don't change between when you read the configuration and when you update
it, you can pass the revision ID from the output of get-function-configuration as a parameter to
update-function-configuration.
78
AWS Lambda Developer Guide
Retrieve environment variables
The following example shows how to define the database host and database name as environment
variables.
If you want your test environment to generate more debug information than the production
environment, you could set an environment variable to configure your test environment to use more
verbose logging or more detailed tracing.
Node.js
Python
import os
region = os.environ['AWS_REGION']
Note
In some cases, you may need to use the following format:
region = os.environ.get('AWS_REGION')
Ruby
region = ENV["AWS_REGION"]
Java
Go
C#
79
AWS Lambda Developer Guide
Defined runtime environment variables
PowerShell
$region = $env:AWS_REGION
Lambda stores environment variables securely by encrypting them at rest. You can configure Lambda to
use a different encryption key (p. 81), encrypt environment variable values on the client side, or set
environment variables in an AWS CloudFormation template with AWS Secrets Manager.
The following additional environment variables aren't reserved and can be extended in your function
configuration.
80
AWS Lambda Developer Guide
Securing environment variables
The sample values shown reflect the latest runtimes. The presence of specific variables or their values
can vary on earlier runtimes.
When you provide the key, only users in your account with access to the key can view or manage
environment variables on the function. Your organization might also have internal or external
requirements to manage keys that are used for encryption and to control when they're rotated.
No AWS KMS permissions are required for your user or the function's execution role to use the default
encryption key. To use a customer managed CMK, you need permission to use the key. Lambda uses your
permissions to create a grant on the key. This allows Lambda to use it for encryption.
You can get these permissions from your user account or from a key's resource-based permissions policy.
ListAliases is provided by the managed policies for Lambda (p. 56). Key policies grant the remaining
permissions to users in the Key users group.
Users without Decrypt permissions can still manage functions, but they can't view environment
variables or manage them in the Lambda console. To prevent a user from viewing environment variables,
add a statement to the user's permissions that denies access to the default key, a customer managed key,
or all keys.
81
AWS Lambda Developer Guide
Securing environment variables
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Deny",
"Action": [
"kms:Decrypt"
],
"Resource": "arn:aws:kms:us-east-2:123456789012:key/3be10e2d-xmpl-4be4-
bc9d-0405a71945cc"
}
]
}
For details on managing key permissions, see Using key policies in AWS KMS.
You can also encrypt environment variable values on the client side before sending them to Lambda, and
decrypt them in your function code. This obscures secret values in the Lambda console and API output,
even for users who have permission to use the key. In your code, you retrieve the encrypted value from
the environment and decrypt it by using the AWS KMS API.
Note
When you use the console encryption helpers, your function needs permission to call the
kms:Decrypt API operation in its execution role (p. 47).
To view sample code for your function's language, choose Code next to an environment variable. The
sample code shows how to retrieve an environment variable in a function and decrypt its value.
Another option is to store passwords in AWS Secrets Manager secrets. You can reference the secret in
your AWS CloudFormation templates to set passwords on databases. You can also set the value of an
environment variable on the Lambda function. For an example, see the next section.
82
AWS Lambda Developer Guide
Sample code and templates
Sample applications
• Blank function (p. 466) – Create a function and an Amazon SNS topic in the same template. Pass
the name of the topic to the function in an environment variable. Read environment variables in code
(multiple languages).
• RDS MySQL – Create a VPC and an Amazon RDS DB instance in one template, with a password stored
in Secrets Manager. In the application template, import database details from the VPC stack, read the
password from Secrets Manager, and pass all connection configuration to the function in environment
variables.
83
AWS Lambda Developer Guide
Concurrency
This topic details how to manage and configure reserved and provisioned concurrency. To learn about
how concurrency interacts with scaling, see Lambda function scaling.
To ensure that a function can always reach a certain level of concurrency, configure the function with
reserved concurrency (p. 85). When a function has reserved concurrency, no other function can use
that concurrency. Reserved concurrency also limits the maximum concurrency for the function, and
applies to the function as a whole, including versions and aliases.
When Lambda allocates an instance of your function, the runtime (p. 195) loads your function's code
and runs initialization code that you define outside of the handler. If your code and dependencies are
large, or you create SDK clients during initialization, this process can take some time. As your function
scales up (p. 153), this causes the portion of requests that are served by new instances to have higher
latency than the rest.
To enable your function to scale without fluctuations in latency, use provisioned concurrency (p. 87).
By allocating provisioned concurrency before an increase in invocations, you can ensure that all requests
are served by initialized instances with very low latency. You can configure provisioned concurrency on a
version of a function, or on an alias.
Lambda also integrates with Application Auto Scaling. You can configure Application Auto Scaling
to manage provisioned concurrency on a schedule or based on utilization. Use scheduled scaling to
increase provisioned concurrency in anticipation of peak traffic. To increase provisioned concurrency
automatically as needed, use the Application Auto Scaling API (p. 90) to register a target and create a
scaling policy.
Provisioned concurrency counts towards a function's reserved concurrency and Regional quotas. If
the amount of provisioned concurrency on a function's versions and aliases adds up to the function's
reserved concurrency, all invocations run on provisioned concurrency. This configuration also has the
effect of throttling the unpublished version of the function ($LATEST), which prevents it from executing.
Note
You can't allocate more provisioned concurrency than reserved concurrency for a function.
Sections
• Configuring reserved concurrency (p. 85)
• Configuring provisioned concurrency (p. 87)
• Configuring concurrency with the Lambda API (p. 90)
84
AWS Lambda Developer Guide
Configuring reserved concurrency
You can reserve up to the Unreserved account concurrency value that is shown, minus 100 for functions
that don't have reserved concurrency. To throttle a function, set the reserved concurrency to zero. This
stops any events from being processed until you remove the limit.
The following example shows two functions with pools of reserved concurrency, and the unreserved
concurrency pool used by other functions. Throttling errors occur when all of the concurrency in a pool is
in use.
85
AWS Lambda Developer Guide
Configuring reserved concurrency
Legend
• Function concurrency
• Reserved concurrency
• Unreserved concurrency
• Throttling
• Other functions can't prevent your function from scaling – All of your account's functions in the
same Region without reserved concurrency share the pool of unreserved concurrency. Without
reserved concurrency, other functions can use up all of the available concurrency. This prevents your
function from scaling up when needed.
• Your function can't scale out of control – Reserved concurrency also limits your function from
using concurrency from the unreserved pool, which caps its maximum concurrency. You can reserve
86
AWS Lambda Developer Guide
Configuring provisioned concurrency
concurrency to prevent your function from using all the available concurrency in the Region, or from
overloading downstream resources.
Setting per-function concurrency can impact the concurrency pool that is available to other functions.
To avoid issues, limit the number of users who can use the PutFunctionConcurrency and
DeleteFunctionConcurrency API operations.
You can manage provisioned concurrency for all aliases and versions from the function configuration
page. The list of provisioned concurrency configurations shows the allocation progress of each
configuration. Provisioned concurrency settings are also available on the configuration page for each
version and alias.
In the following example, the my-function-DEV and my-function-PROD functions are configured
with both reserved and provisioned concurrency. For my-function-DEV, the full pool of reserved
concurrency is also provisioned concurrency. In this case, all invocations either run on provisioned
concurrency or are throttled. For my-function-PROD, a portion of the reserved concurrency pool
is standard concurrency. When all provisioned concurrency is in use, the function scales on standard
concurrency to serve any additional requests.
87
AWS Lambda Developer Guide
Configuring provisioned concurrency
Legend
• Function concurrency
• Reserved concurrency
• Provisioned concurrency
• Unreserved concurrency
• Throttling
Provisioned concurrency does not come online immediately after you configure it. Lambda starts
allocating provisioned concurrency after a minute or two of preparation. Similar to how functions scale
under load (p. 153), up to 3000 instances of the function can be initialized at once, depending on the
Region. After the initial burst, instances are allocated at a steady rate of 500 per minute until the request
is fulfilled. When you request provisioned concurrency for multiple functions or versions of a function in
the same Region, scaling quotas apply across all requests.
88
AWS Lambda Developer Guide
Configuring provisioned concurrency
Legend
• Function instances
• Open requests
• Provisioned concurrency
• Standard concurrency
To optimize latency, you can customize the initialization behavior for functions that use provisioned
concurrency . You can run initialization code for provisioned concurrency instances without impacting
latency, because the initialization code runs at allocation time. However, the initialization code for an
on-demand instance directly impacts the latency of the first invocation. For an on-demand instance, you
may choose to defer initialization for a specific capability until the function needs that capability.
89
AWS Lambda Developer Guide
Configuring concurrency with the Lambda API
of AWS_LAMBDA_INITIALIZATION_TYPE is immutable and does not change over the lifetime of the
execution environment.
If you use the .NET 3.1 runtime, you can configure the AWS_LAMBDA_DOTNET_PREJIT environment
variable to improve the latency for functions that use provisioned concurrency. The .NET runtime
lazily compiles and initializes each library that your code calls for the first time. As a result, the
first invocation of a Lambda function can take longer than subsequent invocations. When you set
AWS_LAMBDA_DOTNET_PREJIT to ProvisionedConcurrency, Lambda performs ahead-of-time JIT
compilation for common system dependencies. Lambda performs this initialization optimization for
provisioned concurrency instances only, which results in faster performance for the first invocation. If
you set the environment variable to Always, Lambda performs ahead-of-time JIT compilation for every
initialization. If you set the environment variable to Never, ahead-of-time JIT compilation is disabled.
The default value for AWS_LAMBDA_DOTNET_PREJIT is ProvisionedConcurrency.
For provisioned concurrency instances, your function's initialization code (p. 32) runs during allocation
and every few hours, as running instances of your function are recycled. You can see the initialization
time in logs and traces (p. 451) after an instance processes a request. However, initialization is billed
even if the instance never processes a request. Provisioned concurrency runs continually and is billed
separately from initialization and invocation costs. For details, see AWS Lambda pricing.
Each version of a function can only have one provisioned concurrency configuration. This can be directly
on the version itself, or on an alias that points to the version. Two aliases can't allocate provisioned
concurrency for the same version. Also, you can't allocate provisioned concurrency on an alias that points
to the unpublished version ($LATEST).
When you change the version that an alias points to, provisioned concurrency is deallocated from the old
version and then allocated to the new version. You can add a routing configuration to an alias that has
provisioned concurrency. However, you can't manage provisioned concurrency settings on the alias while
the routing configuration is in place.
• ProvisionedConcurrentExecutions
• ProvisionedConcurrencyInvocations
• ProvisionedConcurrencySpilloverInvocations
• ProvisionedConcurrencyUtilization
For details, see Working with AWS Lambda function metrics (p. 692).
90
AWS Lambda Developer Guide
Configuring concurrency with the Lambda API
To configure reserved concurrency with the AWS CLI, use the put-function-concurrency command.
The following command reserves a concurrency of 100 for a function named my-function:
{
"ReservedConcurrentExecutions": 100
}
{
"Requested ProvisionedConcurrentExecutions": 100,
"Allocated ProvisionedConcurrentExecutions": 0,
"Status": "IN_PROGRESS",
"LastModified": "2019-11-21T19:32:12+0000"
}
To configure Application Auto Scaling to manage provisioned concurrency, use the Application Auto
Scaling to configure target tracking scaling. First, register a function's alias as a scaling target. The
following example registers the BLUE alias of a function named my-function:
Next, apply a scaling policy to the target. The following example configures Application Auto Scaling to
adjust the provisioned concurrency configuration for an alias to keep utilization near 70 percent:
91
AWS Lambda Developer Guide
Configuring concurrency with the Lambda API
"PolicyARN": "arn:aws:autoscaling:us-east-2:123456789012:scalingPolicy:12266dbb-1524-
xmpl-a64e-9a0a34b996fa:resource/lambda/function:my-function:BLUE:policyName/my-policy",
"Alarms": [
{
"AlarmName": "TargetTracking-function:my-function:BLUE-AlarmHigh-aed0e274-
xmpl-40fe-8cba-2e78f000c0a7",
"AlarmARN": "arn:aws:cloudwatch:us-east-2:123456789012:alarm:TargetTracking-
function:my-function:BLUE-AlarmHigh-aed0e274-xmpl-40fe-8cba-2e78f000c0a7"
},
{
"AlarmName": "TargetTracking-function:my-function:BLUE-AlarmLow-7e1a928e-
xmpl-4d2b-8c01-782321bc6f66",
"AlarmARN": "arn:aws:cloudwatch:us-east-2:123456789012:alarm:TargetTracking-
function:my-function:BLUE-AlarmLow-7e1a928e-xmpl-4d2b-8c01-782321bc6f66"
}
]
}
Application Auto Scaling creates two alarms in CloudWatch. The first alarm triggers when the utilization
of provisioned concurrency consistently exceeds 70 percent. When this happens, Application Auto
Scaling allocates more provisioned concurrency to reduce utilization. The second alarm triggers when
utilization is consistently less than 63 percent (90 percent of the 70 percent target). When this happens,
Application Auto Scaling reduces the alias's provisioned concurrency.
In the following example, a function scales between a minimum and maximum amount of provisioned
concurrency based on utilization. When the number of open requests increases, Application Auto
Scaling increases provisioned concurrency in large steps until it reaches the configured maximum. The
function continues to scale on standard concurrency until utilization starts to drop. When utilization is
consistently low, Application Auto Scaling decreases provisioned concurrency in smaller periodic steps.
92
AWS Lambda Developer Guide
Configuring concurrency with the Lambda API
Legend
• Function instances
• Open requests
• Provisioned concurrency
• Standard concurrency
93
AWS Lambda Developer Guide
Configuring concurrency with the Lambda API
{
"AccountLimit": {
"TotalCodeSize": 80530636800,
"CodeSizeUnzipped": 262144000,
"CodeSizeZipped": 52428800,
"ConcurrentExecutions": 1000,
"UnreservedConcurrentExecutions": 900
},
"AccountUsage": {
"TotalCodeSize": 174913095,
"FunctionCount": 52
}
}
94
AWS Lambda Developer Guide
Versions
Sections
• Creating function versions (p. 95)
• Managing versions with the Lambda API (p. 95)
• Using versions (p. 96)
• Granting permissions (p. 96)
The following example publishes a new version of a function. The response returns configuration
information about the new version, including the version number and the function ARN with the version
suffix.
95
AWS Lambda Developer Guide
Using versions
"FunctionName": "my-function",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function:1",
"Version": "1",
"Role": "arn:aws:iam::123456789012:role/lambda-role",
"Handler": "function.handler",
"Runtime": "nodejs12.x",
...
}
Using versions
You can reference your Lambda function using either a qualified ARN or an unqualified ARN.
• Qualified ARN – The function ARN with a version suffix. The following example refers to version 42 of
the helloworld function.
arn:aws:lambda:aws-region:acct-id:function:helloworld:42
arn:aws:lambda:aws-region:acct-id:function:helloworld
You can use a qualified or an unqualified ARN in all relevant API operations. However, you can't use an
unqualified ARN to create an alias.
If you decide not to publish function versions, you can invoke the function using either the qualified
or unqualified ARN in your event source mapping (p. 148). When you invoke a function using an
unqualified ARN, Lambda implicitly invokes $LATEST.
Lambda publishes a new function version only if the code has never been published or if the code has
changed from the last published version. If there is no change, the function version remains at the last
published version.
The qualified ARN for each Lambda function version is unique. After you publish a version, you can't
change the ARN or the function code.
Granting permissions
You can use a resource-based policy (p. 51) or an identity-based policy (p. 56) to grant access to your
function. The scope of the permission depends on whether you apply the policy to a function or to one
version of a function. For more information about function resource names in policies, see Resources and
conditions for Lambda actions (p. 61).
You can simplify the management of event sources and AWS Identity and Access Management (IAM)
policies by using function aliases. For more information, see Lambda function aliases (p. 97).
96
AWS Lambda Developer Guide
Aliases
Sections
• Creating a function alias (Console) (p. 97)
• Managing aliases with the Lambda API (p. 97)
• Using aliases (p. 98)
• Resource policies (p. 98)
• Alias routing configuration (p. 98)
To create an alias
To change an alias to point a new version of the function, use the update-alias command.
The AWS CLI commands in the preceding steps correspond to the following Lambda API operations:
97
AWS Lambda Developer Guide
Using aliases
Using aliases
Each alias has a unique ARN. An alias can point only to a function version, not to another alias. You can
update an alias to point to a new version of the function.
Event sources such as Amazon Simple Storage Service (Amazon S3) invoke your Lambda function. These
event sources maintain a mapping that identifies the function to invoke when events occur. If you specify
a Lambda function alias in the mapping configuration, you don't need to update the mapping when the
function version changes. For more information, see AWS Lambda event source mappings (p. 148).
In a resource policy, you can grant permissions for event sources to use your Lambda function. If you
specify an alias ARN in the policy, you don't need to update the policy when the function version
changes.
Resource policies
You can use a resource-based policy (p. 51) to give a service, resource, or account access to your function.
The scope of that permission depends on whether you apply it to an alias, a version, or the entire
function. For example, if you use an alias name (such as helloworld:PROD), the permission allows you
to invoke the helloworld function using the alias ARN (helloworld:PROD).
If you attempt to invoke the function without an alias or a specific version, then you get a permission
error. This permission error still occurs even if you attempt to directly invoke the function version
associated with the alias.
For example, the following AWS CLI command grants Amazon S3 permissions to invoke the PROD alias
of the helloworld function when Amazon S3 is acting on behalf of examplebucket.
For more information about using resource names in policies, see Resources and conditions for Lambda
actions (p. 61).
Note that Lambda uses a simple probabilistic model to distribute the traffic between the two function
versions. At low traffic levels, you might see a high variance between the configured and actual
percentage of traffic on each version. If your function uses provisioned concurrency, you can avoid
spillover invocations (p. 692) by configuring a higher number of provisioned concurrency instances
during the time that alias routing is active.
You can point an alias to a maximum of two Lambda function versions. The versions must meet the
following criteria:
• Both versions must have the same execution role (p. 47).
98
AWS Lambda Developer Guide
Alias routing configuration
• Both versions must have the same dead-letter queue configuration, or no dead-letter queue
configuration.
• Both versions must be published. The alias cannot point to $LATEST.
The following example creates a Lambda function alias named routing-alias that points to version 1
of the function. Version 2 of the function receives 3 percent of the traffic. The remaining 97 percent of
traffic is routed to version 1.
Use the update-alias command to increase the percentage of incoming traffic to version 2. In the
following example, you increase the traffic to 5 percent.
To route all traffic to version 2, use the update-alias command to change the function-version
property to point the alias to version 2. The command also resets the routing configuration.
The AWS CLI commands in the preceding steps correspond to the following Lambda API operations:
99
AWS Lambda Developer Guide
Alias routing configuration
• CloudWatch Logs – Lambda automatically emits a START log entry that contains the invoked version
ID to Amazon CloudWatch Logs for every function invocation. The following is an example:
For alias invocations, Lambda uses the Executed Version dimension to filter the metric data by the
invoked version. For more information, see Working with AWS Lambda function metrics (p. 692).
• Response payload (synchronous invocations) – Responses to synchronous function invocations
include an x-amz-executed-version header to indicate which function version has been invoked.
100
AWS Lambda Developer Guide
Creating layers
You can use layers only with Lambda functions deployed as a .zip file archive (p. 41). For functions
defined as a container image (p. 247), you package your preferred runtime and all code dependencies
when you create the container image. For more information, see Working with Lambda layers and
extensions in container images on the AWS Compute Blog.
You can create layers using the Lambda console, the Lambda API, AWS CloudFormation, or the AWS
Serverless Application Model (AWS SAM). For more information about creating layers with AWS SAM, see
Working with layers in the AWS Serverless Application Model Developer Guide.
Sections
• Creating layer content (p. 101)
• Compiling the .zip file archive for your layer (p. 101)
• Including library dependencies in a layer (p. 102)
• Creating a layer (p. 103)
• Deleting a layer version (p. 104)
• Configuring layer permissions (p. 104)
• Using AWS CloudFormation with layers (p. 105)
• Deploy Node.js Lambda functions with .zip file archives (p. 489)
• Deploy Python Lambda functions with .zip file archives (p. 515)
• Deploy Ruby Lambda functions with .zip file archives (p. 551)
• Deploy Java Lambda functions with .zip or JAR file archives (p. 578)
• Deploy Go Lambda functions with .zip file archives (p. 617)
• Deploy C# Lambda functions with .zip file archives (p. 645)
• Deploy PowerShell Lambda functions with .zip file archives (p. 669)
101
AWS Lambda Developer Guide
Including library dependencies in a layer
One way to ensure that you package libraries correctly for Lambda is to use AWS Cloud9. For more
information, see Using Lambda layers to simplify your development process on the AWS Compute Blog.
The following table lists the folder paths that each runtime supports.
Runtime Path
Node.js nodejs/node_modules
nodejs/node14/node_modules (NODE_PATH)
Python python
python/lib/python3.8/site-packages(site
directories)
ruby/lib (RUBYLIB)
lib (LD_LIBRARY_PATH)
The following examples show how you can structure the folders in your layer .zip archive.
Node.js
Example file structure for the AWS X-Ray SDK for Node.js
xray-sdk.zip
# nodejs/node_modules/aws-xray-sdk
Python
pillow.zip
# python/PIL
# python/Pillow-5.3.0.dist-info
Ruby
json.zip
102
AWS Lambda Developer Guide
Creating a layer
# ruby/gems/2.5.0/
| build_info
| cache
| doc
| extensions
| gems
| # json-2.1.0
# specifications
# json-2.1.0.gemspec
Java
jackson.zip
# java/lib/jackson-core-2.2.3.jar
All
jq.zip
# bin/jq
For more information about path settings in the Lambda execution environment, see Defined runtime
environment variables (p. 80).
Creating a layer
You can create new layers using the Lambda console or the Lambda API.
Layers can have one or more version. When you create a layer, Lambda sets the layer version to version
1. You can configure permissions on an existing layer version, but to update the code or make other
configuration changes, you must create a new version of the layer.
103
AWS Lambda Developer Guide
Deleting a layer version
To create a layer, use the publish-layer-version command with a name, description, .zip file archive, and
list of runtimes (p. 195) that are compatible with the layer. The list of runtimes is optional.
{
"Content": {
"Location": "https://awslambda-us-east-2-layers.s3.us-east-2.amazonaws.com/
snapshots/123456789012/my-layer-4aaa2fbb-ff77-4b0a-ad92-5b78a716a96a?
versionId=27iWyA73cCAYqyH...",
"CodeSha256": "tv9jJO+rPbXUUXuRKi7CwHzKtLDkDRJLB3cC3Z/ouXo=",
"CodeSize": 169
},
"LayerArn": "arn:aws:lambda:us-east-2:123456789012:layer:my-layer",
"LayerVersionArn": "arn:aws:lambda:us-east-2:123456789012:layer:my-layer:1",
"Description": "My layer",
"CreatedDate": "2018-11-14T23:03:52.894+0000",
"Version": 1,
"LicenseInfo": "MIT",
"CompatibleRuntimes": [
"python3.6",
"python3.7",
"python3.8"
]
}
Note
Each time that you call publish-layer-version, you create a new version of the layer.
When you delete a layer version, you can no longer configure a Lambda function to use it. However, any
function that already uses the version continues to have access to it. Version numbers are never reused
for a layer name.
To grant layer-usage permission to another account, add a statement to the layer version's permissions
policy using the add-layer-version-permission command. In each statement, you can grant permission
to a single account, all accounts, or an organization.
104
AWS Lambda Developer Guide
Using AWS CloudFormation with layers
e210ffdc-e901-43b0-824b-5fcd0dd26d16 {"Sid":"xaccount","Effect":"Allow","Principal":
{"AWS":"arn:aws:iam::210987654321:root"},"Action":"lambda:GetLayerVersion","Resource":"arn:aws:lambda:u
east-2:123456789012:layer:xray-sdk-nodejs:1"}
Permissions apply only to a single layer version. Repeat the process each time that you create a new layer
version.
For more examples, see Granting layer access to other accounts (p. 54).
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A Lambda application that calls the Lambda API.
Resources:
function:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
CodeUri: function/.
Description: Call the Lambda API
Timeout: 10
# Function's execution role
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambda_ReadOnlyAccess
- AWSXrayWriteOnlyAccess
Tracing: Active
Layers:
- !Ref libs
libs:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: blank-nodejs-lib
Description: Dependencies for the blank sample app.
ContentUri: lib/.
CompatibleRuntimes:
- nodejs12.x
105
AWS Lambda Developer Guide
Using layers
You can use layers only with Lambda functions deployed as a .zip file archive (p. 41). For a function
defined as a container image (p. 247), you can package your preferred runtime and all code
dependencies when you create the container image. For more information, see Working with Lambda
layers and extensions in container images on the AWS Compute Blog.
Sections
• Configuring a function to use layers (p. 106)
• Accessing the contents of a layer (p. 107)
• Finding layer information (p. 107)
• Updating a layer version that your function uses (p. 108)
• Adding layer permissions (p. 108)
• Using AWS SAM to add a layer to a function (p. 105)
• Sample applications (p. 109)
If your functions consume a layer that a different AWS account publishes, your functions can continue
to use the layer version after it has been deleted, or after your permission to access the layer is revoked.
However, you cannot create a new function that uses a deleted layer version.
To add layers to your function, use the update-function-configuration command. The following
example adds two layers: one from the same AWS account as the function, and one from a different
account.
{
"FunctionName": "test-layers",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"Runtime": "nodejs12.x",
"Role": "arn:aws:iam::123456789012:role/service-role/lambda-role",
"Layers": [
{
"Arn": "arn:aws:lambda:us-east-2:123456789012:layer:my-layer:3",
"CodeSize": 169
},
{
"Arn": "arn:aws:lambda:us-east-2:210987654321:layer:their-layer:2",
106
AWS Lambda Developer Guide
Accessing the contents of a layer
"CodeSize": 169
}
],
"RevisionId": "81cc64f5-5772-449a-b63e-12330476bcc4",
...
}
To specify the layer versions to use, you must provide the full Amazon Resource Name (ARN) of each
layer version. When you add layers to a function that already has layers, you overwrite the previous list of
layers. Be sure to include all layers every time that you update the layer configuration. Or, to remove all
layers, specify an empty list.
The creator of a layer can delete a version of the layer. If you're using that layer version in a function,
your function continues to run as though the layer version still exists. However, when you update the
layer configuration, you must remove the reference to the deleted version.
Each Lambda runtime (p. 195) adds specific /opt directory folders to the PATH variable. Your function
code can access the layer content without the need to specify the path. For more information about path
settings in the Lambda execution environment, see Defined runtime environment variables (p. 80).
{
"Layers": [
{
"LayerName": "my-layer",
"LayerArn": "arn:aws:lambda:us-east-2:123456789012:layer:my-layer",
"LatestMatchingVersion": {
"LayerVersionArn": "arn:aws:lambda:us-east-2:123456789012:layer:my-
layer:2",
"Version": 2,
"Description": "My layer",
"CreatedDate": "2018-11-15T00:37:46.592+0000",
"CompatibleRuntimes": [
"python3.6",
"python3.7",
"python3.8",
]
}
}
]
107
AWS Lambda Developer Guide
Updating a layer version that your function uses
To list all layers in your account, you can omit the --compatible-runtime option. The details in the
response reflect the latest version of the layer.
You can also get the latest version of a layer using the list-layer-versions command.
To add an updated layer version to your function, use the update-function-configuration command.
Use the --layers option with this command to list all of the layer versions that you want to add. If the
function already has layers, the new list overwrites the previous list.
To update only one of the layer versions, you must include the ARNs of the existing layer versions with
the --layers option.
The following procedure assumes that you have packaged the updated layer code into a local file named
layer.zip.
1. (Optional) If the new layer version is not published yet, publish the new version.
2. (Optional) If the function has more than one layer, get the current layer versions associated with the
function.
3. Add the new layer version to the function. In the following example command, the function also has
a layer version named other-layer:5:
108
AWS Lambda Developer Guide
Using AWS SAM to add a layer to a function
To use a layer in another account, the owner of that account must grant your account permission in a
resource-based policy (p. 51).
For examples, see Granting layer access to other accounts (p. 54).
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Lambda application that calls the Lambda API.
Resources:
function:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
CodeUri: function/.
Description: Call the AWS Lambda API
Timeout: 10
# Function's execution role
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambda_ReadOnlyAccess
- AWSXrayWriteOnlyAccess
Tracing: Active
Layers:
- !Ref libs
libs:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: blank-nodejs-lib
Description: Dependencies for the blank sample app.
ContentUri: lib/.
CompatibleRuntimes:
- nodejs12.x
When you update your dependencies and deploy, AWS SAM creates a new version of the layer and
updates the mapping.
Sample applications
The GitHub repository for this guide provides blank sample applications that demonstrate the use of
layers for dependency management.
• Node.js – blank-nodejs
• Python – blank-python
• Ruby – blank-ruby
• Java – blank-java
For more information about the blank sample app, see Blank function sample application for AWS
Lambda (p. 466). For other samples, see Samples (p. 464).
109
AWS Lambda Developer Guide
Memory
This page describes how to update the memory allotted to your function in the Lambda console.
Sections
• Configuring function memory (console) (p. 110)
• Accepting function memory recommendations (console) (p. 110)
When you've opted in and your Lambda function meets Compute Optimizer requirements, you can view
and accept function memory recommendations from Compute Optimizer in the Lambda console.
110
AWS Lambda Developer Guide
Network
When you connect a function to a VPC, Lambda creates an elastic network interface for each subnet in
your function's VPC configuration. This process can take several minutes.
While Lambda creates a network interface, you can't perform additional operations that target the
function, such as creating versions (p. 95) or updating the function's code. For new functions, you can't
invoke the function until its state changes from Pending to Active. For existing functions, you can still
invoke an earlier version while the update is in progress. For more information about function states, see
Monitoring the state of a function with the Lambda API (p. 151).
Multiple functions can share a network interface, if the functions share the same subnet and security
group. Connecting additional functions to a subnet that has an existing Lambda-managed network
interface is much quicker than having Lambda create additional network interfaces. However, if you have
many functions or functions with high network usage, Lambda might still create additional network
interfaces.
If your functions aren't active for a long period of time, Lambda reclaims its network interfaces, and the
functions become Idle. To reactivate an idle function, invoke it. This invocation fails, and the function
enters a Pending state again until a network interface is available.
Lambda functions can't connect directly to a VPC with dedicated instance tenancy. To connect to
resources in a dedicated VPC, peer it to a second VPC with default tenancy.
Sections
• Execution role and user permissions (p. 111)
• Configuring VPC access (console) (p. 112)
• Configuring VPC access (API) (p. 112)
• Using IAM condition keys for VPC settings (p. 113)
• Internet and service access for VPC-connected functions (p. 116)
• VPC tutorials (p. 116)
• Sample VPC configurations (p. 116)
• ec2:CreateNetworkInterface
• ec2:DescribeNetworkInterfaces
• ec2:DeleteNetworkInterface
111
AWS Lambda Developer Guide
Configuring VPC access (console)
When you configure VPC connectivity, Lambda uses your permissions to verify network resources. To
configure a function to connect to a VPC, your AWS Identity and Access Management (IAM) user needs
the following permissions:
User permissions
• ec2:DescribeSecurityGroups
• ec2:DescribeSubnets
• ec2:DescribeVpcs
112
AWS Lambda Developer Guide
Using IAM condition keys for VPC settings
To create a function and connect it to a VPC using the AWS Command Line Interface (AWS CLI), you can
use the create-function command with the vpc-config option. The following example creates a
function with a connection to a VPC with two subnets and one security group.
To disconnect your function from a VPC, update the function configuration with an empty list of subnets
and security groups.
The Lambda API operations CreateFunction (p. 763) and UpdateFunctionConfiguration (p. 936)
support these condition keys. For more information about using condition keys in IAM policies, see IAM
JSON Policy Elements: Condition in the IAM User Guide.
Tip
If your function already includes a VPC configuration from a previous API request, you can send
an UpdateFunctionConfiguration request without the VPC configuration.
113
AWS Lambda Developer Guide
Using IAM condition keys for VPC settings
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnforceVPCFunction",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Effect": "Deny",
"Resource": "*",
"Condition": {
"Null": {
"lambda:VpcIds": "true"
}
}
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnforceOutOfVPC",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Effect": "Deny",
"Resource": "*",
"Condition": {
"StringEquals": {
"lambda:VpcIds": ["vpc-1", "vpc-2"]
}
}
}
To deny users access to specific subnets, use StringEquals to check the value of the
lambda:SubnetIds condition. The following example denies users access to subnet-1 and subnet-2.
{
"Sid": "EnforceOutOfSubnet",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Effect": "Deny",
"Resource": "*",
"Condition": {
"ForAnyValue:StringEquals": {
114
AWS Lambda Developer Guide
Using IAM condition keys for VPC settings
To deny users access to specific security groups, use StringEquals to check the value of the
lambda:SecurityGroupIds condition. The following example denies users access to sg-1 and sg-2.
{
"Sid": "EnforceOutOfSecurityGroups",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Effect": "Deny",
"Resource": "*",
"Condition": {
"ForAnyValue:StringEquals": {
"lambda:SecurityGroupIds": ["sg-1", "sg-2"]
}
}
}
]
}
Allow users to create and update functions with specific VPC settings
To allow users to access specific VPCs, use StringEquals to check the value of the lambda:VpcIds
condition. The following example allows users to access vpc-1 and vpc-2.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnforceStayInSpecificVpc",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Effect": "Allow",
"Resource": "*",
"Condition": {
"StringEquals": {
"lambda:VpcIds": ["vpc-1", "vpc-2"]
}
}
}
To allow users to access specific subnets, use StringEquals to check the value of the
lambda:SubnetIds condition. The following example allows users to access subnet-1 and subnet-2.
{
"Sid": "EnforceStayInSpecificSubnets",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Effect": "Allow",
115
AWS Lambda Developer Guide
Internet and service access for VPC-connected functions
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"lambda:SubnetIds": ["subnet-1", "subnet-2"]
}
}
}
To allow users to access specific security groups, use StringEquals to check the value of the
lambda:SecurityGroupIds condition. The following example allows users to access sg-1 and sg-2.
{
"Sid": "EnforceStayInSpecificSecurityGroup",
"Action": [
"lambda:CreateFunction",
"lambda:UpdateFunctionConfiguration"
],
"Effect": "Allow",
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"lambda:SecurityGroupIds": ["sg-1", "sg-2"]
}
}
}
]
}
Internet access from a private subnet requires network address translation (NAT). To give your function
access to the internet, route outbound traffic to a NAT gateway in a public subnet. The NAT gateway
has a public IP address and can connect to the internet through the VPC's internet gateway. For more
information, see How do I give internet access to my Lambda function in a VPC?
VPC tutorials
In the following tutorials, you connect a Lambda function to resources in your VPC.
• Tutorial: Configuring a Lambda function to access Amazon RDS in an Amazon VPC (p. 401)
• Tutorial: Configuring a Lambda function to access Amazon ElastiCache in an Amazon VPC (p. 345)
116
AWS Lambda Developer Guide
Sample VPC configurations
• vpc-private.yaml – A VPC with two private subnets and VPC endpoints for Amazon Simple Storage
Service (Amazon S3) and Amazon DynamoDB. Use this template to create a VPC for functions that
don't need internet access. This configuration supports use of Amazon S3 and DynamoDB with the
AWS SDKs, and access to database resources in the same VPC over a local network connection.
• vpc-privatepublic.yaml – A VPC with two private subnets, VPC endpoints, a public subnet with a NAT
gateway, and an internet gateway. Internet-bound traffic from functions in the private subnets is
routed to the NAT gateway using a route table.
To create a VPC using a template, on the AWS CloudFormation console Stacks page, choose Create stack,
and then follow the instructions in the Create stack wizard.
117
AWS Lambda Developer Guide
Interface VPC endpoints
To establish a private connection between your VPC and Lambda, create an interface VPC endpoint.
Interface endpoints are powered by AWS PrivateLink, which enables you to privately access Lambda APIs
without an internet gateway, NAT device, VPN connection, or AWS Direct Connect connection. Instances
in your VPC don't need public IP addresses to communicate with Lambda APIs. Traffic between your VPC
and Lambda does not leave the AWS network.
Each interface endpoint is represented by one or more elastic network interfaces in your subnets. A
network interface provides a private IP address that serves as an entry point for traffic to Lambda.
Sections
• Considerations for Lambda interface endpoints (p. 118)
• Creating an interface endpoint for Lambda (p. 119)
• Creating an interface endpoint policy for Lambda (p. 119)
You can call any of the Lambda API operations from your VPC. For example, you can invoke the Lambda
function by calling the Invoke API from within your VPC. For the full list of Lambda APIs, see Actions in
the Lambda API reference.
Billing Considerations
There is no additional cost to access a Lambda function through an interface endpoint. For more Lambda
pricing information, see AWS Lambda Pricing.
Standard pricing for AWS PrivateLink applies to interface endpoints for Lambda. Your AWS account
is billed for every hour an interface endpoint is provisioned in each Availability Zone and for data
processed through the interface endpoint. For more interface endpoint pricing information, see AWS
PrivateLink pricing.
118
AWS Lambda Developer Guide
Creating an interface endpoint for Lambda
Traffic between peered VPCs stays on the AWS network and does not traverse the public internet.
Once VPCs are peered, resources like Amazon Elastic Compute Cloud (Amazon EC2) instances, Amazon
Relational Database Service (Amazon RDS) instances, or VPC-enabled Lambda functions in both VPCs can
access the Lambda API through interface endpoints created in the one of the VPCs.
To use the private DNS option, you must set the enableDnsHostnames and
enableDnsSupportattributes of your VPC. For more information, see Viewing and updating
DNS support for your VPC in the Amazon VPC User Guide. If you enable private DNS for the interface
endpoint, you can make API requests to Lambda using its default DNS name for the Region, for example,
lambda.us-east-1.amazonaws.com. For more service endpoints, see Service endpoints and quotas in
the AWS General Reference.
For more information, see Accessing a service through an interface endpoint in the Amazon VPC User
Guide.
For information about creating and configuring an endpoint using AWS CloudFormation, see the
AWS::EC2::VPCEndpoint resource in the AWS CloudFormation User Guide.
Use the create-vpc-endpoint command and specify the VPC ID, VPC endpoint type (interface),
service name, subnets that will use the endpoint, and security groups to associate with the endpoint's
network interfaces. For example:
119
AWS Lambda Developer Guide
Creating an interface endpoint policy for Lambda
For more information, see Controlling access to services with VPC endpoints in the Amazon VPC User
Guide.
The following is an example of an endpoint policy for Lambda. When attached to an endpoint, this policy
allows user MyUser to invoke the function my-function.
{
"Statement":[
{
"Principal":
{
"AWS": "arn:aws:iam::123412341234:user/MyUser"
},
"Effect":"Allow",
"Action":[
"lambda:InvokeFunction"
],
"Resource": [
"arn:aws:lambda:us-east-2:123456789012:function:my-function”
]
}
]
}
120
AWS Lambda Developer Guide
Database
Sections
• Creating a database proxy (console) (p. 121)
• Using the function's permissions for authentication (p. 122)
• Sample application (p. 122)
Example secret
{
"username": "admin",
"password": "e2abcecxmpldc897"
}
• IAM role – An IAM role with permission to use the secret, and a trust policy that allows Amazon
RDS to assume the role.
• Authentication – The authentication and authorization method for connecting to the proxy from
your function code.
6. Choose Add.
Pricing
Amazon RDS charges a hourly price for proxies that is determined by the instance size of your
database. For details, see RDS Proxy pricing.
Proxy creation takes a few minutes. When the proxy is available, configure your function to connect to
the proxy endpoint instead of the database endpoint.
Standard Amazon RDS Proxy pricing applies. For more information, see Managing connections with the
Amazon RDS Proxy in the Amazon Aurora User Guide.
121
AWS Lambda Developer Guide
Using the function's permissions for authentication
You can create a database proxy that uses the function's IAM credentials for authentication and
authorization instead of a password. To use the function's permissions to connect to the proxy, set
Authentication to Execution role.
The Lambda console adds the required permission (rds-db:connect) to the execution role. You can
then use the AWS SDK to generate a token that allows it to connect to the proxy. The following example
shows how to configure a database connection with the mysql2 library in Node.js.
For more information, see IAM database authentication in the Amazon RDS User Guide.
Sample application
Sample applications that demonstrate the use of Lambda with an Amazon RDS database are available in
this guide's GitHub repository. There are two applications:
• RDS MySQL – The AWS CloudFormation template template-vpcrds.yml creates a MySQL 5.7
database in a private VPC. In the sample application, a Lambda function proxies queries to the
database. The function and database templates both use Secrets Manager to access database
credentials.
122
AWS Lambda Developer Guide
Sample application
• List Manager – A processor function reads events from a Kinesis stream. It uses the data from the
events to update DynamoDB tables, and stores a copy of the event in a MySQL database.
123
AWS Lambda Developer Guide
Sample application
124
AWS Lambda Developer Guide
Sample application
To use the sample applications, follow the instructions in the GitHub repository: RDS MySQL, List
Manager.
125
AWS Lambda Developer Guide
File system
Sections
• Connecting to a file system (console) (p. 126)
• Configuring a file system and access point (p. 126)
• Execution role and user permissions (p. 127)
• Configuring file system access with the Lambda API (p. 128)
• AWS CloudFormation and AWS SAM (p. 128)
• Sample applications (p. 129)
• EFS file system – The access point for a file system in the same VPC.
• Local mount path – The location where the file system is mounted on the Lambda function,
starting with /mnt/.
Pricing
Amazon EFS charges for storage and throughput, with rates that vary by storage class. For
details, see Amazon EFS pricing.
Lambda charges for data transfer between VPCs. This only applies if your function's VPC is
peered to another VPC with a file system. The rates are the same as for Amazon EC2 data
transfer between VPCs in the same Region. For details, see Lambda pricing.
For more information about Lambda's integration with Amazon EFS, see Using Amazon EFS with
Lambda (p. 351).
126
AWS Lambda Developer Guide
Execution role and user permissions
configuration you could have a VPC with two private subnets in separate Availability Zones. The function
connects to both subnets and a mount target is available in each. Ensure that NFS traffic (port 2049) is
allowed by the security groups used by the function and mount targets.
Note
When you create a file system, you choose a performance mode that can't be changed later.
General purpose mode has lower latency, and Max I/O mode supports a higher maximum
throughput and IOPS. For help choosing, see Amazon EFS performance in the Amazon Elastic
File System User Guide.
An access point connects each instance of the function to the right mount target for the Availability Zone
it connects to. For best performance, create an access point with a non-root path, and limit the number
of files that you create in each directory. User and owner IDs are required, but they don't need to have a
specific value. The following example creates a directory named my-function on the file system and
sets the owner ID to 1001 with standard directory permissions (755).
• Name – files
• User ID – 1001
• Group ID – 1001
• Path – /my-function
• Permissions – 755
• Owner user ID – 1001
• Group user ID – 1001
When a function uses the access point, it is given user ID 1001 and has full access to the directory.
For more information, see the following topics in the Amazon Elastic File System User Guide:
• elasticfilesystem:ClientMount
• elasticfilesystem:ClientWrite (not required for read-only connections)
When you configure a file system, Lambda uses your permissions to verify mount targets. To configure a
function to connect to a file system, your IAM user needs the following permissions:
User permissions
• elasticfilesystem:DescribeMountTargets
127
AWS Lambda Developer Guide
Configuring file system access with the Lambda API
ARN=arn:aws:elasticfilesystem:us-east-2:123456789012:access-point/fsap-015cxmplb72b405fd
aws lambda update-function-configuration --function-name my-function \
--fs-config FileSystemArn=$ARN,LocalMountPath=/mnt/efs0
You can get the ARN of a file system's access point with the describe-access-points command.
{
"AccessPoints": [
{
"ClientToken": "console-aa50c1fd-xmpl-48b5-91ce-57b27a3b1017",
"Name": "lambda-ap",
"Tags": [
{
"Key": "Name",
"Value": "lambda-ap"
}
],
"AccessPointId": "fsap-015cxmplb72b405fd",
"AccessPointArn": "arn:aws:elasticfilesystem:us-east-2:123456789012:access-
point/fsap-015cxmplb72b405fd",
"FileSystemId": "fs-aea3xmpl",
"RootDirectory": {
"Path": "/"
},
"OwnerId": "123456789012",
"LifeCycleState": "available"
}
]
}
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
128
AWS Lambda Developer Guide
Sample applications
CidrBlock: 10.0.0.0/16
Subnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: VPC
CidrBlock: 10.0.1.0/24
AvailabilityZone: "eu-central-1a"
EfsSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId:
Ref: VPC
GroupDescription: "mnt target sg"
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: "0.0.0.0/0"
FileSystem:
Type: AWS::EFS::FileSystem
Properties:
PerformanceMode: generalPurpose
MountTarget1:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId:
Ref: FileSystem
SubnetId:
Ref: Subnet1
SecurityGroups:
- Ref: EfsSecurityGroup
MyFunctionWithEfs:
Type: AWS::Serverless::Function
Properties:
CodeUri: function/.
Description: Use a file system.
FileSystemConfigs:
-
Arn: !Sub
- "arn:aws:elasticfilesystem:eu-central-1:123456789101:access-point/
fsap-015cxmplb72b405fd"
LocalMountPath: "/mnt/efs0"
DependsOn: "MountTarget1"
You must add the DependsOn to ensure that the mount targets are fully created before the Lambda runs
for the first time.
For the AWS CloudFormation AWS::Lambda::Function type, the property name and fields are the
same. For more information, see Using AWS Lambda with AWS CloudFormation (p. 303).
Sample applications
The GitHub repository for this guide includes a sample application that demonstrates the use of Amazon
EFS with a Lambda function.
• efs-nodejs – A function that uses an Amazon EFS file system in a Amazon VPC. This sample includes a
VPC, file system, mount targets, and access point configured for use with Lambda.
129
AWS Lambda Developer Guide
Code signing
To verify code integrity, use AWS Signer to create digitally signed code packages for functions and
layers. When a user attempts to deploy a code package, Lambda performs validation checks on the code
package before accepting the deployment. Because code signing validation checks run at deployment
time, there is no performance impact on function execution.
You also use AWS Signer to create signing profiles. You use a signing profile to create the signed code
package. Use AWS Identity and Access Management (IAM) to control who can sign code packages and
create signing profiles. For more information, see Authentication and Access Control in the AWS Signer
Developer Guide.
To enable code signing for a function, you create a code signing configuration and attach it to the
function. A code signing configuration defines a list of allowed signing profiles and the policy action to
take if any of the validation checks fail.
Lambda layers follow the same signed code package format as function code packages. When you add a
layer to a function that has code signing enabled, Lambda checks that the layer is signed by an allowed
signing profile. When you enable code signing for a function, all layers that are added to the function
must also be signed by one of the allowed signing profiles.
Use IAM to control who can create code signing configurations. Typically, you allow only specific
administrative users to have this ability. Additionally, you can set up IAM policies to enforce that
developers only create functions that have code signing enabled.
You can configure code signing to log changes to AWS CloudTrail. Successful and blocked deployments
to functions are logged to CloudTrail with information about the signature and validation checks.
You can configure code signing for your functions using the Lambda console, the AWS Command Line
Interface (AWS CLI), AWS CloudFormation, and the AWS Serverless Application Model (AWS SAM).
There is no additional charge for using AWS Signer or code signing for AWS Lambda.
Sections
• Signature validation (p. 130)
• Configuration prerequisites (p. 131)
• Creating code signing configurations (p. 131)
• Updating a code signing configuration (p. 131)
• Deleting a code signing configuration (p. 132)
• Enabling code signing for a function (p. 132)
• Configuring IAM policies (p. 132)
• Configuring code signing with the Lambda API (p. 133)
Signature validation
Lambda performs the following validation checks when you deploy a signed code package to your
function:
130
AWS Lambda Developer Guide
Configuration prerequisites
1. Integrity – Validates that the code package has not been modified since it was signed. Lambda
compares the hash of the package with the hash from the signature.
2. Expiry – Validates that the signature of the code package has not expired.
3. Mismatch – Validates that the code package is signed with one of the allowed signing profiles for the
Lambda function. A mismatch also occurs if a signature is not present.
4. Revocation – Validates that the signature of the code package has not been revoked.
The signature validation policy defined in the code signing configuration determines which of the
following actions Lambda takes if any of the validation checks fail:
• Warn – Lambda allows the deployment of the code package, but issues a warning. Lambda issues a
new Amazon CloudWatch metric and also stores the warning in the CloudTrail log.
• Enforce – Lambda issues a warning (the same as for the Warn action) and blocks the deployment of
the code package.
You can configure the policy for the expiry, mismatch, and revocation validation checks. Note that you
cannot configure a policy for the integrity check. If the integrity check fails, Lambda blocks deployment.
Configuration prerequisites
Before you can configure code signing for a Lambda function, use AWS Signer to do the following:
For more information, see Creating Signing Profiles (Console) in the AWS Signer Developer Guide.
a. For Signing profile version ARN, choose a profile version's Amazon Resource Name (ARN), or
enter the ARN.
b. To add an additional signing profile, choose Add signing profiles.
5. Under Signature validation policy, choose Warn or Enforce.
6. Choose Create configuration.
131
AWS Lambda Developer Guide
Deleting a code signing configuration
a. For Signing profile version ARN, choose a profile version's Amazon Resource Name (ARN), or
enter the ARN.
b. To add an additional signing profile, choose Add signing profiles.
5. Under Signature validation policy, choose Warn or Enforce.
6. Choose Save changes.
The following example policy statement grants permission to create, update, and retrieve code signing
configurations.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:CreateCodeSigningConfig",
132
AWS Lambda Developer Guide
Configuring code signing with the Lambda API
"lambda:UpdateCodeSigningConfig",
"lambda:GetCodeSigningConfig"
],
"Resource": "*"
}
]
}
Administrators can use the CodeSigningConfigArn condition key to specify the code signing
configurations that developers must use to create or update your functions.
The following example policy statement grants permission to create a function. The policy statement
includes a lambda:CodeSigningConfigArn condition to specify the allowed code signing
configuration. Lambda blocks any CreateFunction API request if its CodeSigningConfigArn
parameter is missing or does not match the value in the condition.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReferencingCodeSigningConfig",
"Effect": "Allow",
"Action": [
"lambda:CreateFunction",
],
"Resource": "*",
"Condition": {
"StringEquals": {
"lambda:CodeSigningConfigArn":
“arn:aws:lambda:us-west-2:123456789012:code-signing-
config:csc-0d4518bd353a0a7c6”
}
}
}
]
}
• ListCodeSigningConfigs
• CreateCodeSigningConfig
• GetCodeSigningConfig
• UpdateCodeSigningConfig
• DeleteCodeSigningConfig
To manage the code signing configuration for a function, use the following API operations:
133
AWS Lambda Developer Guide
Tags
Sections
• Adding tags to a function (console) (p. 134)
• Using tags to filter functions (console) (p. 134)
• Using Tags with the AWS CLI (p. 135)
• Tag Key and Value Requirements (p. 136)
6. Choose Save.
134
AWS Lambda Developer Guide
Using Tags with the AWS CLI
3. Choose a tag key to see a list of values that are in-use in the current region.
4. Choose a value to see functions with that value, or choose (all values) to see all functions that have
a tag with that key.
The search bar also supports searching for tag keys. Type tag to see just a list of tag keys, or start typing
the name of a key to find it in the list.
With AWS Billing and Cost Management, you can use tags to customize billing reports and create
cost-allocation reports. For more information, see see Monthly Cost Allocation Report and Using Cost
Allocation Tags in the AWS Billing and Cost Management User Guide.
135
AWS Lambda Developer Guide
Tag Key and Value Requirements
If you want to view the tags that are applied to a specific Lambda function, you can use either of the
following Lambda API commands:
• ListTags (p. 873) – You supply your Lambda function ARN (Amazon Resource Name) to view a list of
the tags associated with this function:
• GetFunction (p. 808) – You supply your Lambda function name to a view a list of the tags associated
with this function:
You can also use the AWS Tagging Service’s GetResources API to filter your resources by tags. The
GetResources API receives up to 10 filters, with each filter containing a tag key and up to 10 tag values.
You provide GetResources with a ‘ResourceType’ to filter by specific resource types. For more information
about the AWS Tagging Service, see Working with Resource Groups.
136
AWS Lambda Developer Guide
When you invoke a function, you can choose to invoke it synchronously or asynchronously. With
synchronous invocation (p. 138), you wait for the function to process the event and return a response.
With asynchronous (p. 140) invocation, Lambda queues the event for processing and returns a response
immediately. For asynchronous invocation, Lambda handles retries and can send invocation records to a
destination (p. 142).
To use your function to process data automatically, add one or more triggers. A trigger is a Lambda
resource or a resource in another service that you configure to invoke your function in response to
lifecycle events, external requests, or on a schedule. Your function can have multiple triggers. Each
trigger acts as a client invoking your function independently. Each event that Lambda passes to your
function only has data from one client or trigger.
To process items from a stream or queue, you can create an event source mapping (p. 148). An event
source mapping is a resource in Lambda that reads items from an Amazon SQS queue, an Amazon
Kinesis stream, or an Amazon DynamoDB stream, and sends them to your function in batches. Each
event that your function processes can contain hundreds or thousands of items.
Other AWS services and resources invoke your function directly. For example, you can configure
CloudWatch Events to invoke your function on a timer, or you can configure Amazon S3 to invoke your
function when an object is created. Each service varies in the method it uses to invoke your function,
the structure of the event, and how you configure it. For more information, see Using AWS Lambda with
other services (p. 257).
Depending on who invokes your function and how it's invoked, scaling behavior and the types of errors
that occur can vary. When you invoke a function synchronously, you receive errors in the response and
can retry. When you invoke asynchronously, use an event source mapping, or configure another service
to invoke your function, the retry requirements and the way that your function scales to handle large
numbers of events can vary. For details, see AWS Lambda function scaling (p. 153) and Error handling
and automatic retries in AWS Lambda (p. 158).
Topics
• Synchronous invocation (p. 138)
• Asynchronous invocation (p. 140)
• AWS Lambda event source mappings (p. 148)
• Monitoring the state of a function with the Lambda API (p. 151)
• AWS Lambda function scaling (p. 153)
• Error handling and automatic retries in AWS Lambda (p. 158)
• Using Lambda extensions (p. 160)
• Invoking functions defined as container images (p. 164)
• Invoking Lambda functions with the AWS Mobile SDK for Android (p. 165)
137
AWS Lambda Developer Guide
Synchronous invocation
Synchronous invocation
When you invoke a function synchronously, Lambda runs the function and waits for a response. When
the function completes, Lambda returns the response from the function's code with additional data, such
as the version of the function that was invoked. To invoke a function synchronously with the AWS CLI,
use the invoke command.
aws lambda invoke --function-name my-function --payload '{ "key": "value" }' response.json
{
"ExecutedVersion": "$LATEST",
"StatusCode": 200
}
The following diagram shows clients invoking a Lambda function synchronously. Lambda sends the
events directly to the function and sends the function's response back to the invoker.
The payload is a string that contains an event in JSON format. The name of the file where the AWS CLI
writes the response from the function is response.json. If the function returns an object or error, the
response is the object or error in JSON format. If the function exits without error, the response is null.
The output from the command, which is displayed in the terminal, includes information from headers
in the response from Lambda. This includes the version that processed the event (useful when you use
aliases (p. 97)), and the status code returned by Lambda. If Lambda was able to run the function, the
status code is 200, even if the function returned an error.
Note
For functions with a long timeout, your client might be disconnected during synchronous
invocation while it waits for a response. Configure your HTTP client, SDK, firewall, proxy, or
operating system to allow for long connections with timeout or keep-alive settings.
If Lambda isn't able to run the function, the error is displayed in the output.
138
AWS Lambda Developer Guide
Synchronous invocation
The AWS CLI is an open-source tool that enables you to interact with AWS services using commands in
your command line shell. To complete the steps in this section, you must have the following:
You can use the AWS CLI to retrieve logs for an invocation using the --log-type command option.
The response contains a LogResult field that contains up to 4 KB of base64-encoded logs from the
invocation.
{
"StatusCode": 200,
"LogResult":
"U1RBUlQgUmVxdWVzdElkOiA4N2QwNDRiOC1mMTU0LTExZTgtOGNkYS0yOTc0YzVlNGZiMjEgVmVyc2lvb...",
"ExecutedVersion": "$LATEST"
}
The base64 utility is available on Linux, macOS, and Ubuntu on Windows. macOS users may need to use
base64 -D.
For more information about the Invoke API, including a full list of parameters, headers, and errors, see
Invoke (p. 840).
When you invoke a function directly, you can check the response for errors and retry. The AWS CLI
and AWS SDK also automatically retry on client timeouts, throttling, and service errors. For more
information, see Error handling and automatic retries in AWS Lambda (p. 158).
139
AWS Lambda Developer Guide
Asynchronous invocation
Asynchronous invocation
Several AWS services, such as Amazon Simple Storage Service (Amazon S3) and Amazon Simple
Notification Service (Amazon SNS), invoke functions asynchronously to process events. When you invoke
a function asynchronously, you don't wait for a response from the function code. You hand off the event
to Lambda and Lambda handles the rest. You can configure how Lambda handles errors, and can send
invocation records to a downstream resource to chain together components of your application.
The following diagram shows clients invoking a Lambda function asynchronously. Lambda queues the
events before sending them to the function.
For asynchronous invocation, Lambda places the event in a queue and returns a success response
without additional information. A separate process reads events from the queue and sends them to your
function. To invoke a function asynchronously, set the invocation type parameter to Event.
The cli-binary-format option is required if you are using AWS CLI version 2. You can also configure this
option in your AWS CLI config file
.
{
"StatusCode": 202
}
The output file (response.json) doesn't contain any information, but is still created when you run
this command. If Lambda isn't able to add the event to the queue, the error message appears in the
command output.
140
AWS Lambda Developer Guide
Asynchronous invocation
Lambda manages the function's asynchronous event queue and attempts to retry on errors. If the
function returns an error, Lambda attempts to run it two more times, with a one-minute wait between
the first two attempts, and two minutes between the second and third attempts. Function errors include
errors returned by the function's code and errors returned by the function's runtime, such as timeouts.
If the function doesn't have enough concurrency available to process all events, additional requests
are throttled. For throttling errors (429) and system errors (500-series), Lambda returns the event
to the queue and attempts to run the function again for up to 6 hours. The retry interval increases
exponentially from 1 second after the first attempt to a maximum of 5 minutes. However, it might be
longer if the queue is backed up. Lambda also reduces the rate at which it reads events from the queue.
The following example shows an event that was successfully added to the queue, but is still pending one
hour later due to throttling.
Even if your function doesn't return an error, it's possible for it to receive the same event from Lambda
multiple times because the queue itself is eventually consistent. If the function can't keep up with
incoming events, events might also be deleted from the queue without being sent to the function.
Ensure that your function code gracefully handles duplicate events, and that you have enough
concurrency available to handle all invocations.
When the queue is backed up, new events might age out before Lambda has a chance to send them
to your function. When an event expires or fails all processing attempts, Lambda discards it. You can
configure error handling (p. 142) for a function to reduce the number of retries that Lambda performs,
or to discard unprocessed events more quickly.
You can also configure Lambda to send an invocation record to another service. Lambda supports the
following destinations (p. 142) for asynchronous invocation.
141
AWS Lambda Developer Guide
Configuring error handling for asynchronous invocation
The invocation record contains details about the request and response in JSON format. You can configure
separate destinations for events that are processed successfully, and events that fail all processing
attempts. Alternatively, you can configure an SQS queue or SNS topic as a dead-letter queue (p. 145)
for discarded events. For dead-letter queues, Lambda only sends the content of the event, without
details about the response.
Sections
• Configuring error handling for asynchronous invocation (p. 142)
• Configuring destinations for asynchronous invocation (p. 142)
• Asynchronous invocation configuration API (p. 144)
• AWS Lambda function dead-letter queues (p. 145)
• Maximum age of event – The maximum amount of time Lambda retains an event in the
asynchronous event queue, up to 6 hours.
• Retry attempts – The number of times Lambda retries when the function returns an error,
between 0 and 2.
6. Choose Save.
When an invocation event exceeds the maximum age or fails all retry attempts, Lambda discards it. To
retain a copy of discarded events, configure a failed-event destination.
The following example shows a function that is processing asynchronous invocations. When the function
returns a success response or exits without throwing an error, Lambda sends a record of the invocation
to an EventBridge event bus. When an event fails all processing attempts, Lambda sends an invocation
record to an Amazon SQS queue.
142
AWS Lambda Developer Guide
Configuring destinations for asynchronous invocation
To send events to a destination, your function needs additional permissions. Add a policy with the
required permissions to your function's execution role (p. 47). Each destination service requires a
different permission, as follows:
143
AWS Lambda Developer Guide
Asynchronous invocation configuration API
• On failure – Send a record when the event fails all processing attempts or exceeds the maximum
age.
• On success – Send a record when the function successfully processes an asynchronous invocation.
6. For Destination type, choose the type of resource that receives the invocation record.
7. For Destination, choose a resource.
8. Choose Save.
When an invocation matches the condition, Lambda sends a JSON document with details about the
invocation to the destination. The following example shows an invocation record for an event that failed
three processing attempts due to a function error.
{
"version": "1.0",
"timestamp": "2019-11-14T18:16:05.568Z",
"requestContext": {
"requestId": "e4b46cbf-b738-xmpl-8880-a18cdf61200e",
"functionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function:
$LATEST",
"condition": "RetriesExhausted",
"approximateInvokeCount": 3
},
"requestPayload": {
"ORDER_IDS": [
"9e07af03-ce31-4ff3-xmpl-36dce652cb4f",
"637de236-e7b2-464e-xmpl-baf57f86bb53",
"a81ddca6-2c35-45c7-xmpl-c3a03a31ed15"
]
},
"responseContext": {
"statusCode": 200,
"executedVersion": "$LATEST",
"functionError": "Unhandled"
},
"responsePayload": {
"errorMessage": "RequestId: e4b46cbf-b738-xmpl-8880-a18cdf61200e Process exited
before completing request"
}
}
The invocation record contains details about the event, the response, and the reason that the record was
sent.
• PutFunctionEventInvokeConfig
• GetFunctionEventInvokeConfig
• UpdateFunctionEventInvokeConfig
• ListFunctionEventInvokeConfigs
144
AWS Lambda Developer Guide
Dead-letter queues
• DeleteFunctionEventInvokeConfig
To configure asynchronous invocation with the AWS CLI, use the put-function-event-invoke-
config command. The following example configures a function with a maximum event age of 1 hour
and no retries.
{
"LastModified": 1573686021.479,
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:error:$LATEST",
"MaximumRetryAttempts": 0,
"MaximumEventAgeInSeconds": 3600,
"DestinationConfig": {
"OnSuccess": {},
"OnFailure": {}
}
}
{
"LastModified": 1573687896.493,
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:error:$LATEST",
"MaximumRetryAttempts": 0,
"MaximumEventAgeInSeconds": 3600,
"DestinationConfig": {
"OnSuccess": {},
"OnFailure": {
"Destination": "arn:aws:sqs:us-east-2:123456789012:destination"
}
}
}
If you don't have a queue or topic, create one. Choose the target type that matches your use case.
145
AWS Lambda Developer Guide
Dead-letter queues
• Amazon SQS queue – A queue holds failed events until they're retrieved. You can retrieve events
manually, or you can configure Lambda to read from the queue (p. 439) and invoke a function.
To send events to a queue or topic, your function needs additional permissions. Add a policy with the
required permissions to your function's execution role (p. 47).
If the target queue or topic is encrypted with a customer managed key, the execution role must also be a
user in the key's resource-based policy.
After creating the target and updating your function's execution role, add the dead-letter queue to your
function. You can configure multiple functions to send events to the same target.
To configure a dead-letter queue with the AWS CLI, use the update-function-configuration
command.
Lambda sends the event to the dead-letter queue as-is, with additional information in attributes. You
can use this information to identify the error that the function returned, or to correlate the event with
logs or an AWS X-Ray trace.
• RequestID (String) – The ID of the invocation request. Request IDs appear in function logs. You can
also use the X-Ray SDK to record the request ID on an attribute in the trace. You can then search for
traces by request ID in the X-Ray console. For an example, see the error processor sample (p. 473).
• ErrorCode (Number) – The HTTP status code.
• ErrorMessage (String) – The first 1 KB of the error message.
146
AWS Lambda Developer Guide
Dead-letter queues
If Lambda can't send a message to the dead-letter queue, it deletes the event and emits the
DeadLetterErrors (p. 692) metric. This can happen because of lack of permissions, or if the total size of
the message exceeds the limit for the target queue or topic. For example, if an Amazon SNS notification
with a body close to 256 KB triggers a function that results in an error, the additional event data added
by Amazon SNS, combined with the attributes added by Lambda, can cause the message to exceed the
maximum size allowed in the dead-letter queue.
If you're using Amazon SQS as an event source, configure a dead-letter queue on the Amazon SQS queue
itself and not on the Lambda function. For more information, see Using AWS Lambda with Amazon
SQS (p. 439).
147
AWS Lambda Developer Guide
Event source mapping
An event source mapping uses permissions in the function's execution role (p. 47) to read and manage
items in the event source. Permissions, event structure, settings, and polling behavior vary by event
source. For more information, see the linked topic for the service that you use as an event source.
To manage an event source with the AWS CLI or AWS SDK, you can use the following API operations:
The following example uses the AWS CLI to map a function named my-function to a DynamoDB
stream that is specified by its Amazon Resource Name (ARN), with a batch size of 500.
{
"UUID": "14e0db71-5d35-4eb5-b481-8945cf9d10c2",
"BatchSize": 500,
"MaximumBatchingWindowInSeconds": 0,
"ParallelizationFactor": 1,
"EventSourceArn": "arn:aws:dynamodb:us-east-2:123456789012:table/my-table/
stream/2019-06-10T19:26:16.525",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"LastModified": 1560209851.963,
"LastProcessingResult": "No records processed",
"State": "Creating",
"StateTransitionReason": "User action",
"DestinationConfig": {},
"MaximumRecordAgeInSeconds": 604800,
"BisectBatchOnFunctionError": false,
148
AWS Lambda Developer Guide
Event source mapping
"MaximumRetryAttempts": 10000
}
Event source mappings read items from a stream or queue in batches. They include multiple items in the
event that your function receives. You can configure the size of the batch that the event source mapping
sends to your function, up to a maximum that varies by service. The number of items in the event can be
smaller than the batch size if there aren't enough items available, or if the batch is too large to send in
one event and has to be split up.
The following example shows an event source mapping that reads from a Kinesis stream. If a batch of
events fails all processing attempts, the event source mapping sends details about the batch to an SQS
queue.
The event batch is the event that Lambda sends to the function. It is a batch of records or messages
compiled from the items that the event source mapping reads from a stream or queue. Batch size and
other settings only apply to the event batch.
149
AWS Lambda Developer Guide
Event source mapping
For streams, an event source mapping creates an iterator for each shard in the stream and processes
items in each shard in order. You can configure the event source mapping to read only new items that
appear in the stream, or to start with older items. Processed items aren't removed from the stream and
can be processed by other functions or consumers.
By default, if your function returns an error, the entire batch is reprocessed until the function succeeds,
or the items in the batch expire. To ensure in-order processing, processing for the affected shard is
paused until the error is resolved. You can configure the event source mapping to discard old events,
restrict the number of retries, or process multiple batches in parallel. If you process multiple batches in
parallel, in-order processing is still guaranteed for each partition key, but multiple partition keys in the
same shard are processed simultaneously.
You can also configure the event source mapping to send an invocation record to another service when it
discards an event batch. Lambda supports the following destinations (p. 142) for event source mappings.
The invocation record contains details about the failed event batch in JSON format.
{
"requestContext": {
"requestId": "c9b8fa9f-5a7f-xmpl-af9c-0c604cde93a5",
"functionArn": "arn:aws:lambda:us-east-2:123456789012:function:myfunction",
"condition": "RetryAttemptsExhausted",
"approximateInvokeCount": 1
},
"responseContext": {
"statusCode": 200,
"executedVersion": "$LATEST",
"functionError": "Unhandled"
},
"version": "1.0",
"timestamp": "2019-11-14T00:38:06.021Z",
"KinesisBatchInfo": {
"shardId": "shardId-000000000001",
"startSequenceNumber": "49601189658422359378836298521827638475320189012309704722",
"endSequenceNumber": "49601189658422359378836298522902373528957594348623495186",
"approximateArrivalOfFirstRecord": "2019-11-14T00:38:04.835Z",
"approximateArrivalOfLastRecord": "2019-11-14T00:38:05.580Z",
"batchSize": 500,
"streamArn": "arn:aws:kinesis:us-east-2:123456789012:stream/mystream"
}
}
Lambda also supports in-order processing for FIFO (first-in, first-out) queues (p. 439), scaling up to
the number of active message groups. For standard queues, items aren't necessarily processed in order.
Lambda scales up to process a standard queue as quickly as possible. When an error occurs, batches
are returned to the queue as individual items and might be processed in a different grouping than the
original batch. Occasionally, the event source mapping might receive the same item from the queue
twice, even if no function error occurred. Lambda deletes items from the queue after they're processed
successfully. You can configure the source queue to send items to a dead-letter queue if they can't be
processed.
For information about services that invoke Lambda functions directly, see Using AWS Lambda with other
services (p. 257).
150
AWS Lambda Developer Guide
Function states
If you configure your function to connect to a virtual private cloud (VPC), the process can take longer.
When you first connect a function to a VPC, Lambda provisions network interfaces, which takes about
a minute. To communicate the current state of your function, Lambda includes additional fields in the
function configuration (p. 975) document that is returned by several Lambda API actions.
When you create a function, the function is initially in the Pending state. When the function is ready to
be invoked, the state changes from Pending to Active. While the state is Pending, invocations and
other API actions that operate on the function return an error. If you build automation around creating
and updating functions, wait for the function to become active before performing additional actions that
operate on the function.
You can use the Lambda API to get information about a function's state. State information is included in
the FunctionConfiguration (p. 975) document returned by several API actions. To view the function's
state with the AWS CLI, use the get-function-configuration command.
{
"FunctionName": "my-function",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"Runtime": "nodejs12.x",
"Role": "arn:aws:iam::123456789012:role/lambda-role",
"TracingConfig": {
"Mode": "Active"
},
"State": "Pending",
"StateReason": "The function is being created.",
"StateReasonCode": "Creating",
...
}
The StateReason and StateReasonCode contain additional information about the state when it is not
Active. The following operations fail while function creation is pending:
When you update a function's configuration, the update can trigger an asynchronous operation to
provision resources. While this is in progress, you can invoke the function, but other operations on
the function fail. Invocations that occur while the update is in progress run against the previous
configuration. The function's state is Active, but its LastUpdateStatus is InProgress.
151
AWS Lambda Developer Guide
Function states
"FunctionName": "my-function",
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
"Runtime": "nodejs12.x",
"VpcConfig": {
"SubnetIds": [
"subnet-071f712345678e7c8",
"subnet-07fd123456788a036",
"subnet-0804f77612345cacf"
],
"SecurityGroupIds": [
"sg-085912345678492fb"
],
"VpcId": "vpc-08e1234569e011e83"
},
"State": "Active",
"LastUpdateStatus": "InProgress",
...
}
For example, when you connect your function to a virtual private cloud (VPC), Lambda provisions an
elastic network interface for each subnet. This process can leave your function in a pending state for a
minute or so. Lambda also reclaims network interfaces that are not in use, placing your function in an
Inactive state. When the function is inactive, an invocation causes it to enter the Pending state while
network access is restored. The invocation that triggers restoration, and further invocations while the
operation is pending, fail with ResourceNotReadyException.
If Lambda encounters an error when restoring a function's network interface, the function goes back to
the Inactive state. The next invocation can trigger another attempt. For some configuration errors,
Lambda waits at least 5 minutes before attempting to create another network interface. These errors
have the following LastUpdateStatusReasonCode values:
For more information on how states work with VPC connectivity, see Configuring a Lambda function to
access resources in a VPC (p. 111).
152
AWS Lambda Developer Guide
Function scaling
Your functions' concurrency is the number of instances that serve requests at a given time. For an initial
burst of traffic, your functions' cumulative concurrency in a Region can reach an initial level of between
500 and 3000, which varies per Region. Note that the burst concurrency quota is not per-function; it
applies to all of your functions in the Region.
After the initial burst, your functions' concurrency can scale by an additional 500 instances each
minute. This continues until there are enough instances to serve all requests, or until a concurrency
limit is reached. When requests come in faster than your function can scale, or when your function is at
maximum concurrency, additional requests fail with a throttling error (429 status code).
The following example shows a function processing a spike in traffic. As invocations increase
exponentially, the function scales up. It initializes a new instance for any request that can't be routed to
an available instance. When the burst concurrency limit is reached, the function starts to scale linearly.
If this isn't enough concurrency to serve all requests, additional requests are throttled and should be
retried.
153
AWS Lambda Developer Guide
Function scaling
Legend
• Function instances
• Open requests
• Throttling possible
The function continues to scale until the account's concurrency limit for the function's Region is reached.
The function catches up to demand, requests subside, and unused instances of the function are stopped
after being idle for some time. Unused instances are frozen while they're waiting for requests and don't
incur any charges.
154
AWS Lambda Developer Guide
Function scaling
The regional concurrency limit starts at 1,000. You can increase the limit by submitting a request in the
Support Center console. To allocate capacity on a per-function basis, you can configure functions with
reserved concurrency (p. 84). Reserved concurrency creates a pool that can only be used by its function,
and also prevents its function from using unreserved concurrency.
When your function scales up, the first request served by each instance is impacted by the time it
takes to load and initialize your code. If your initialization code (p. 32) takes a long time, the impact on
average and percentile latency can be significant. To enable your function to scale without fluctuations
in latency, use provisioned concurrency (p. 84). The following example shows a function with provisioned
concurrency processing a spike in traffic.
Legend
• Function instances
• Open requests
• Provisioned concurrency
• Standard concurrency
155
AWS Lambda Developer Guide
Function scaling
When you allocate provisioned concurrency, your function is ready to serve a burst of incoming requests
with very low latency. When all provisioned concurrency is in use, the function scales up normally to
handle any additional requests.
Application Auto Scaling takes this a step further by providing autoscaling for provisioned concurrency.
With Application Auto Scaling, you can create a target tracking scaling policy that adjusts provisioned
concurrency levels automatically, based on the utilization metric that Lambda emits. Use the Application
Auto Scaling API (p. 90) to register an alias as a scalable target and create a scaling policy.
In the following example, a function scales between a minimum and maximum amount of provisioned
concurrency based on utilization. When the number of open requests increases, Application Auto
Scaling increases provisioned concurrency in large steps until it reaches the configured maximum. The
function continues to scale on standard concurrency until utilization starts to drop. When utilization is
consistently low, Application Auto Scaling decreases provisioned concurrency in smaller periodic steps.
156
AWS Lambda Developer Guide
Function scaling
Legend
• Function instances
• Open requests
• Provisioned concurrency
• Standard concurrency
When you invoke your function asynchronously, by using an event source mapping or another AWS
service, scaling behavior varies. For example, event source mappings that read from a stream are limited
by the number of shards in the stream. Scaling capacity that is unused by an event source is available for
use by other clients and event sources. For more information, see the following topics.
You can monitor concurrency levels in your account by using the following metrics:
Concurrency metrics
• ConcurrentExecutions
• UnreservedConcurrentExecutions
• ProvisionedConcurrentExecutions
• ProvisionedConcurrencyInvocations
• ProvisionedConcurrencySpilloverInvocations
• ProvisionedConcurrencyUtilization
For more information, see Working with AWS Lambda function metrics (p. 692).
157
AWS Lambda Developer Guide
Error handling
Issues with the request, caller, or account can cause invocation errors. Invocation errors include an error
type and status code in the response that indicate the cause of the error.
• Request – The request event is too large or isn't valid JSON, the function doesn't exist, or a parameter
value is the wrong type.
• Caller – The user or service doesn't have permission to invoke the function.
• Account – The maximum number of function instances are already running, or requests are being
made too quickly.
Clients such as the AWS CLI and the AWS SDK retry on client timeouts, throttling errors (429), and other
errors that aren't caused by a bad request. For a full list of invocation errors, see Invoke (p. 840).
Function errors occur when your function code or the runtime that it uses return an error.
Unlike invocation errors, function errors don't cause Lambda to return a 400-series or 500-series status
code. If the function returns an error, Lambda indicates this by including a header named X-Amz-
Function-Error, and a JSON-formatted response with the error message and other details. For
examples of function errors in each language, see the following topics.
When you invoke a function directly, you determine the strategy for handling errors. You can retry, send
the event to a queue for debugging, or ignore the error. Your function's code might have run completely,
partially, or not at all. If you retry, ensure that your function's code can handle the same event multiple
times without causing duplicate transactions or other unwanted side effects.
When you invoke a function indirectly, you need to be aware of the retry behavior of the invoker and any
service that the request encounters along the way. This includes the following scenarios.
158
AWS Lambda Developer Guide
Error handling
• Asynchronous invocation – Lambda retries function errors twice. If the function doesn't have enough
capacity to handle all incoming requests, events might wait in the queue for hours or days to be sent
to the function. You can configure a dead-letter queue on the function to capture events that weren't
successfully processed. For more information, see Asynchronous invocation (p. 140).
• Event source mappings – Event source mappings that read from streams retry the entire batch of
items. Repeated errors block processing of the affected shard until the error is resolved or the items
expire. To detect stalled shards, you can monitor the Iterator Age (p. 692) metric.
For event source mappings that read from a queue, you determine the length of time between retries
and destination for failed events by configuring the visibility timeout and redrive policy on the source
queue. For more information, see AWS Lambda event source mappings (p. 148) and the service-
specific topics under Using AWS Lambda with other services (p. 257).
• AWS services – AWS services can invoke your function synchronously (p. 138) or asynchronously.
For synchronous invocation, the service decides whether to retry. For example, Amazon S3 batch
operations retries the operation if the Lambda function returns a TemporaryFailure response code.
Services that proxy requests from an upstream user or client may have a retry strategy or may relay
the error response back to the requestor. For example, API Gateway always relays the error response
back to the requestor.
For asynchronous invocation, the behavior is the same as when you invoke the function synchronously.
For more information, see the service-specific topics under Using AWS Lambda with other
services (p. 257) and the invoking service's documentation.
• Other accounts and clients – When you grant access to other accounts, you can use resource-based
policies (p. 51) to restrict the services or resources they can configure to invoke your function. To
protect your function from being overloaded, consider putting an API layer in front of your function
with Amazon API Gateway (p. 261).
To help you deal with errors in Lambda applications, Lambda integrates with services like Amazon
CloudWatch and AWS X-Ray. You can use a combination of logs, metrics, alarms, and tracing to quickly
detect and identify issues in your function code, API, or other resources that support your application.
For more information, see Monitoring and troubleshooting Lambda applications (p. 683).
For a sample application that uses a CloudWatch Logs subscription, X-Ray tracing, and a Lambda
function to detect and process errors, see Error processor sample application for AWS Lambda (p. 473).
159
AWS Lambda Developer Guide
Using extensions
Lambda supports external and internal extensions. An external extension runs as an independent process
in the execution environment and continues to run after the function invocation is fully processed.
Because extensions run as separate processes, you can write them in a different language than the
function.
An internal extension runs as part of the runtime process. Your function accesses internal extensions by
using wrapper scripts or in-process mechanisms such as JAVA_TOOL_OPTIONS. For more information,
see Modifying the runtime environment (p. 231).
You can add extensions to a function using the Lambda console, the AWS Command Line Interface (AWS
CLI), or infrastructure as code (IaC) services and tools such as AWS CloudFormation, AWS Serverless
Application Model (AWS SAM), and Terraform.
Note that the Go 1.x runtime does not support extensions. To support extensions, you can create Go
functions on the provided.al2 runtime. For more information, see Migrating Lambda functions to
Amazon Linux 2.
You are charged for the execution time that the extension consumes (in 1 ms increments). For more
pricing information for extensions, see AWS Lambda Pricing. For pricing information for partner
extensions, see those partners' websites. There is no cost to install your own extensions.
Topics
• Execution environment (p. 161)
• Impact on performance and resources (p. 161)
• Permissions (p. 161)
• Configuring extensions (.zip file archive) (p. 162)
• Using extensions in container images (p. 162)
• Next steps (p. 162)
160
AWS Lambda Developer Guide
Execution environment
Execution environment
Lambda invokes your function in an execution environment (p. 200), which provides a secure and
isolated runtime environment. The execution environment manages the resources required to run your
function and provides lifecycle support for the function's runtime and extensions.
• Init: In this phase, Lambda creates or unfreezes an execution environment with the configured
resources, downloads the code for the function and all layers, initializes any extensions, initializes the
runtime, and then runs the function’s initialization code (the code outside the main handler). The Init
phase happens either during the first invocation, or in advance of function invocations if you have
enabled provisioned concurrency (p. 87).
The Init phase is split into three sub-phases: Extension init, Runtime init, and Function
init. These sub-phases ensure that all extensions and the runtime complete their setup tasks before
the function code runs.
• Invoke: In this phase, Lambda invokes the function handler. After the function runs to completion,
Lambda prepares to handle another function invocation.
• Shutdown: This phase is triggered if the Lambda function does not receive any invocations for a period
of time. In the Shutdown phase, Lambda shuts down the runtime, alerts the extensions to let them
stop cleanly, and then removes the environment. Lambda sends a Shutdown event to each extension,
which tells the extension that the environment is about to be shut down.
During the Init phase, Lambda extracts layers containing extensions into the /opt directory in the
execution environment. Lambda looks for extensions in the /opt/extensions/ directory, interprets
each file as an executable bootstrap for launching the extension, and starts all extensions in parallel.
Extensions can impact the performance of your function because they share function resources such as
CPU, memory, and storage. For example, if an extension performs compute-intensive operations, you
may see your function's execution duration increase.
Each extension must complete its initialization before Lambda invokes the function. Therefore,
an extension that consumes significant initialization time can increase the latency of the function
invocation.
To measure the extra time that the extension takes after the function execution, you can use the
PostRuntimeExtensionsDuration function metric (p. 692). To measure the increase in memory
used, you can use the MaxMemoryUsed metric. To understand the impact of a specific extension, you can
run different versions of your functions side by side.
Permissions
Extensions have access to the same resources as functions. Because extensions are executed within the
same environment as the function, permissions are shared between the function and the extension.
For a .zip file archive, you can create an AWS CloudFormation template to simplify the task of attaching
the same extension configuration—including AWS Identity and Access Management (IAM) permissions—
to multiple functions.
161
AWS Lambda Developer Guide
Configuring extensions (.zip file archive)
You add the extension to your function using the same method as you would for any layer. For more
information, see Using layers with your Lambda function (p. 106).
You can run multiple processes within a container. Lambda manages the lifecycle of the main process
and any additional processes. Lambda uses the Extensions API (p. 209) to manage the extension
lifecycle.
FROM public.ecr.aws/lambda/python:3.8
Next steps
To learn more about extensions, we recommend the following resources:
• For a basic working example, see Building Extensions for AWS Lambda on the AWS Compute Blog.
162
AWS Lambda Developer Guide
Next steps
• For information about extensions that AWS Lambda Partners provides, see Introducing AWS Lambda
Extensions on the AWS Compute Blog.
• To view available example extensions and wrapper scripts, see AWS Lambda Extensions on the AWS
Samples GitHub repository.
163
AWS Lambda Developer Guide
Invoking functions defined as container images
Topics
• Function lifecycle (p. 164)
• Invoking the function (p. 164)
• Image security (p. 164)
Function lifecycle
Lambda optimizes a new or updated container image in preparation for the function to receive
invocations. The optimization process can take a few seconds. The function remains in the Pending
state until the process completes. The function then transitions to the Active state. While the state is
Pending, you can invoke the function, but other operations on the function fail. Invocations that occur
while an image update is in progress run the code from the previous image.
If a function is not invoked for multiple weeks, Lambda reclaims its optimized version, and the function
transitions to the Inactive state. To reactivate the function, you must invoke it. Lambda rejects the first
invocation and the function enters the Pending state until Lambda re-optimizes the image. The function
then returns to the Active state.
Lambda periodically fetches the associated container image from the Amazon Elastic Container Registry
(Amazon ECR) repository. If the corresponding container image no longer exists on Amazon ECR, the
function enters the Failed state, and Lambda returns a failure for any function invocations.
You can use the Lambda API to get information about a function's state. For more information, see
Monitoring the state of a function with the Lambda API (p. 151).
Lambda then runs the function by calling the code entry point specified in the function configuration
(the ENTRYPOINT and CMD container image settings (p. 249)).
Image security
When Lambda first downloads the container image from its original source (Amazon ECR), the container
image is optimized, encrypted, and stored using authenticated convergent encryption methods. All
keys that are required to decrypt customer data are protected using customer managed AWS Key
Management Service (AWS KMS) customer master keys (CMKs). To track and audit Lambda's usage of
CMKs, you can view the AWS CloudTrail logs (p. 285).
164
AWS Lambda Developer Guide
Mobile SDK for Android
When you invoke a function from a mobile application, you choose the event structure, invocation
type (p. 137), and permission model. You can use aliases (p. 97) to enable seamless updates to your
function code, but otherwise the function and application are tightly coupled. As you add more
functions, you can create an API layer to decouple your function code from your front-end clients and
improve performance.
To create a fully-featured web API for your mobile and web applications, use Amazon API Gateway.
With API Gateway, you can add custom authorizers, throttle requests, and cache results for all of your
functions. For more information, see Using AWS Lambda with Amazon API Gateway (p. 261).
Topics
• Tutorial: Using AWS Lambda with the Mobile SDK for Android (p. 165)
• Sample function code (p. 171)
The mobile application retrieves AWS credentials from an Amazon Cognito identity pool and uses them
to invoke a Lambda function with an event that contains request data. The function processes the
request and returns a response to the front-end.
165
AWS Lambda Developer Guide
Tutorial
Prerequisites
This tutorial assumes that you have some knowledge of basic Lambda operations and the Lambda
console. If you haven't already, follow the instructions in Getting started with Lambda (p. 9) to create
your first Lambda function.
To complete the following steps, you need a command line terminal or shell to run commands.
Commands and the expected output are listed in separate blocks:
aws --version
For long commands, an escape character (\) is used to split a command over multiple lines.
On Linux and macOS, use your preferred shell and package manager. On Windows 10, you can install the
Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.
The AWSLambdaBasicExecutionRole policy has the permissions that the function needs to write logs to
CloudWatch Logs.
Example index.js
166
AWS Lambda Developer Guide
Tutorial
The cli-binary-format option is required if you are using AWS CLI version 2. You can also configure
this option in your AWS CLI config file
.
For more information about IAM roles, see IAM roles in the IAM User Guide. For more information about
Amazon Cognito services, see the Amazon Cognito product detail page.
• The identity pool you are creating must allow access to unauthenticated identities because our
example mobile application does not require a user log in. Therefore, make sure to select the
Enable access to unauthenticated identities option.
• Add the following statement to the permission policy associated with the unauthenticated
identities.
167
AWS Lambda Developer Guide
Tutorial
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": [
"arn:aws:lambda:us-
east-1:123456789012:function:AndroidBackendLambdaFunction"
]
}
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"mobileanalytics:PutEvents",
"cognito-sync:*"
],
"Resource":[
"*"
]
},
{
"Effect":"Allow",
"Action":[
"lambda:invokefunction"
],
"Resource":[
"arn:aws:lambda:us-east-1:account-
id:function:AndroidBackendLambdaFunction"
]
}
]
}
For instructions about how to create an identity pool, log in to the Amazon Cognito console and
follow the New Identity Pool wizard.
3. Note the identity pool ID. You specify this ID in your mobile application you create in the next
section. The app uses this ID when it sends request to Amazon Cognito to request for temporary
security credentials.
1. Create a new Android project called AndroidEventGenerator using the following configuration:
compile 'com.amazonaws:aws-android-sdk-core:2.2.+'
168
AWS Lambda Developer Guide
Tutorial
compile 'com.amazonaws:aws-android-sdk-lambda:2.2.+'
3. Build the project so that the required dependencies are downloaded, as needed.
4. In the Android application manifest (AndroidManifest.xml), add the following permissions so
that your application can connect to the Internet. You can add them just before the </manifest>
end tag.
import com.amazonaws.mobileconnectors.lambdainvoker.*;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;
6. In the package section, add the following two classes (RequestClass and ResponseClass). Note
that the POJO is same as the POJO you created in your Lambda function in the preceding section.
• RequestClass. The instances of this class act as the POJO (Plain Old Java Object) for event data
which consists of first and last name. If you are using Java example for your Lambda function
you created in the preceding section, this POJO is same as the POJO you created in your Lambda
function code.
package com.example....lambdaeventgenerator;
public class RequestClass {
String firstName;
String lastName;
public RequestClass() {
}
}
• ResponseClass
package com.example....lambdaeventgenerator;
public class ResponseClass {
String greetings;
169
AWS Lambda Developer Guide
Tutorial
public ResponseClass() {
}
}
7. In the same package, create interface called MyInterface for invoking the
AndroidBackendLambdaFunction Lambda function.
package com.example.....lambdaeventgenerator;
import com.amazonaws.mobileconnectors.lambdainvoker.LambdaFunction;
public interface MyInterface {
/**
* Invoke the Lambda function "AndroidBackendLambdaFunction".
* The function name is the method name.
*/
@LambdaFunction
ResponseClass AndroidBackendLambdaFunction(RequestClass request);
The @LambdaFunction annotation in the code maps the specific client method to the same-name
Lambda function.
8. To keep the application simple, we are going to add code to invoke the Lambda function in the
onCreate() event handler. In MainActivity, add the following code toward the end of the
onCreate() code.
// Create the Lambda proxy object with a default Json data binder.
// You can provide your own data binder by implementing
// LambdaDataBinder.
final MyInterface myInterface = factory.build(MyInterface.class);
170
AWS Lambda Developer Guide
Sample code
}
}
@Override
protected void onPostExecute(ResponseClass result) {
if (result == null) {
return;
}
// Do a toast
Toast.makeText(MainActivity.this, result.getGreetings(),
Toast.LENGTH_LONG).show();
}
}.execute(request);
Topics
• Node.js (p. 171)
• Java (p. 171)
Node.js
The following example uses data to generate a string response.
Example index.js
Zip up the sample code to create a deployment package. For instructions, see Deploy Node.js Lambda
functions with .zip file archives (p. 489).
Java
The following example uses data to generate a string response.
In the code, the handler (myHandler) uses the RequestClass and ResponseClass types for the
input and output. The code provides implementation for these types.
Example HelloPojo.java
package example;
171
AWS Lambda Developer Guide
Sample code
import com.amazonaws.services.lambda.runtime.Context;
public RequestClass() {
}
}
public ResponseClass() {
}
Dependencies
• aws-lambda-java-core
172
AWS Lambda Developer Guide
Sample code
Build the code with the Lambda library dependencies to create a deployment package. For instructions,
see Deploy Java Lambda functions with .zip or JAR file archives (p. 578).
173
AWS Lambda Developer Guide
The AWS Serverless Application Repository provides a collection of Lambda applications that you
can deploy in your account with a few clicks. The repository includes both ready-to-use applications
and samples that you can use as a starting point for your own projects. You can also submit your own
projects for inclusion.
AWS CloudFormation enables you to create a template that defines your application's resources and lets
you manage the application as a stack. You can more safely add or modify resources in your application
stack. If any part of an update fails, AWS CloudFormation automatically rolls back to the previous
configuration. With AWS CloudFormation parameters, you can create multiple environments for your
application from the same template. AWS SAM (p. 5) extends AWS CloudFormation with a simplified
syntax focused on Lambda application development.
The AWS CLI (p. 5) and SAM CLI (p. 6) are command line tools for managing Lambda application stacks.
In addition to commands for managing application stacks with the AWS CloudFormation API, the AWS
CLI supports higher-level commands that simplify tasks like uploading deployment packages and
updating templates. The AWS SAM CLI provides additional functionality, including validating templates
and testing locally.
When creating an application, you can create its Git repository using either CodeCommit or an AWS
CodeStar connection to GitHub. CodeCommit enables you to use the IAM console to manage SSH keys
and HTTP credentials for your users. AWS CodeStar connections enables you to connect to your GitHub
account. For more information about connections, see What are connections? in the Developer Tools
console User Guide.
For more information about designing Lambda applications, see Application design in the Lambda
operator guide.
Topics
• Managing applications in the AWS Lambda console (p. 175)
• Creating an application with continuous delivery in the Lambda console (p. 178)
• Rolling deployments for Lambda functions (p. 187)
• Common Lambda application types and use cases (p. 189)
• Best practices for working with AWS Lambda functions (p. 192)
174
AWS Lambda Developer Guide
Manage applications
• AWS CloudFormation template or SAM template – The template that defines your application.
• Resources – The AWS resources that are defined in your application's template. To manage your
application's Lambda functions, choose a function name from the list.
Monitoring applications
The Monitoring tab shows an Amazon CloudWatch dashboard with aggregate metrics for the resources
in your application.
By default, the Lambda console shows a basic dashboard. You can customize this page by defining
custom dashboards in your application template. When your template includes one or more dashboards,
the page shows your dashboards instead of the default dashboard. You can switch between dashboards
with the drop-down menu on the top right of the page.
175
AWS Lambda Developer Guide
Custom monitoring dashboards
Resources:
MyDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardName: my-dashboard
DashboardBody: |
{
"widgets": [
{
"type": "metric",
"width": 12,
"height": 6,
"properties": {
"metrics": [
[
"AWS/Lambda",
"Invocations",
"FunctionName",
"my-function",
{
"stat": "Sum",
"label": "MyFunction"
}
],
[
{
"expression": "SUM(METRICS())",
"label": "Total Invocations"
}
]
],
"region": "us-east-1",
"title": "Invocations",
"view": "timeSeries",
"stacked": false
}
}
]
}
You can get the definition for any of the widgets in the default monitoring dashboard from the
CloudWatch console.
176
AWS Lambda Developer Guide
Custom monitoring dashboards
5. Choose Source.
For more information about authoring CloudWatch dashboards and widgets, see Dashboard body
structure and syntax in the Amazon CloudWatch API Reference.
177
AWS Lambda Developer Guide
Tutorial – Create an application
• Application – A Node.js Lambda function, build specification, and AWS Serverless Application Model
(AWS SAM) template.
• Pipeline – An AWS CodePipeline pipeline that connects the other resources to enable continuous
delivery.
• Repository – A Git repository in AWS CodeCommit. When you push a change, the pipeline copies the
source code into an Amazon S3 bucket and passes it to the build project.
• Trigger – An Amazon CloudWatch Events rule that watches the main branch of the repository and
triggers the pipeline.
• Build project – An AWS CodeBuild build that gets the source code from the pipeline and packages the
application. The source includes a build specification with commands that install dependencies and
prepare the application template for deployment.
• Deployment configuration – The pipeline's deployment stage defines a set of actions that take
the processed AWS SAM template from the build output, and deploy the new version with AWS
CloudFormation.
• Bucket – An Amazon Simple Storage Service (Amazon S3) bucket for deployment artifact storage.
• Roles – The pipeline's source, build, and deploy stages have IAM roles that allow them to manage AWS
resources. The application's function has an execution role (p. 47) that allows it to upload logs and can
be extended to access other services.
Your application and pipeline resources are defined in AWS CloudFormation templates that you can
customize and extend. Your application repository includes a template that you can modify to add
Amazon DynamoDB tables, an Amazon API Gateway API, and other application resources. The continuous
delivery pipeline is defined in a separate template outside of source control and has its own stack.
The pipeline maps a single branch in a repository to a single application stack. You can create additional
pipelines to add environments for other branches in the same repository. You can also add stages to your
pipeline for testing, staging, and manual approvals. For more information about AWS CodePipeline, see
What is AWS CodePipeline.
Sections
• Prerequisites (p. 179)
• Create an application (p. 179)
• Invoke the function (p. 180)
• Add an AWS resource (p. 181)
• Update the permissions boundary (p. 183)
• Update the function code (p. 183)
• Next steps (p. 184)
• Troubleshooting (p. 185)
• Clean up (p. 186)
178
AWS Lambda Developer Guide
Prerequisites
Prerequisites
This tutorial assumes that you have some knowledge of basic Lambda operations and the Lambda
console. If you haven't already, follow the instructions in Getting started with Lambda (p. 9) to create
your first Lambda function.
To complete the following steps, you need a command line terminal or shell to run commands.
Commands and the expected output are listed in separate blocks:
aws --version
For long commands, an escape character (\) is used to split a command over multiple lines.
On Linux and macOS, use your preferred shell and package manager. On Windows 10, you can install the
Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.
This tutorial uses CodeCommit for source control. To set up your local machine to access and update
application code, see Setting up in the AWS CodeCommit User Guide.
Create an application
Create an application in the Lambda console. In Lambda, an application is an AWS CloudFormation
stack with a Lambda function and any number of supporting resources. In this tutorial, you create an
application that has a function and its execution role.
To create an application
Lambda creates the pipeline and related resources and commits the sample application code to the Git
repository. As resources are created, they appear on the overview page.
179
AWS Lambda Developer Guide
Invoke the function
The Infrastructure stack contains the repository, build project, and other resources that combine to form
a continuous delivery pipeline. When this stack finishes deploying, it in turn deploys the application stack
that contains the function and execution role. These are the application resources that appear under
Resources.
The Lambda console runs your function and displays the result. Expand the Details section under the
result to see the output and execution details.
180
AWS Lambda Developer Guide
Add an AWS resource
...
Resources:
ddbTable:
Type: AWS::Serverless::SimpleTable
Properties:
181
AWS Lambda Developer Guide
Add an AWS resource
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
helloFromLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: src/handlers/hello-from-lambda.helloFromLambdaHandler
Runtime: nodejs10.x
MemorySize: 128
Timeout: 60
Description: A Lambda function that returns a static string.
Environment:
Variables:
DDB_TABLE: !Ref ddbTable
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref ddbTable
- AWSLambdaBasicExecutionRole
When you push a change, it triggers the application's pipeline. Use the Deployments tab of the
application screen to track the change as it flows through the pipeline. When the deployment is
complete, proceed to the next step.
182
AWS Lambda Developer Guide
Update the permissions boundary
In order for the function to use the DynamoDB permission that you added to its execution role in the
previous step, you must extend the permissions boundary to allow the additional permissions. The
Lambda console detects resources that aren't in the permissions boundary and provides an updated
policy that you can use to update it.
For more information about permissions boundaries, see Using permissions boundaries for AWS Lambda
applications (p. 67).
1. Add a new handler named index.js to the src/handlers folder with the following content.
Example src/handlers/index.js
const response = {
body: JSON.stringify(message)
};
console.log(`body: ${response.body}`);
return response;
183
AWS Lambda Developer Guide
Next steps
2. Open the application template and change the handler value to src/handlers/index.handler.
Example template.yml
...
helloFromLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: src/handlers/index.handler
Runtime: nodejs10.x
After the code change is deployed, invoke the function a few times to update the DynamoDB table.
Lambda creates additional instances of your function to handle multiple concurrent invocations. Each log
stream in the CloudWatch Logs log group corresponds to a function instance. A new function instance
is also created when you change your function's code or configuration. For more information on scaling,
see AWS Lambda function scaling (p. 153).
Next steps
The AWS CloudFormation template that defines your application resources uses the AWS Serverless
Application Model transform to simplify the syntax for resource definitions, and automate uploading
184
AWS Lambda Developer Guide
Troubleshooting
the deployment package and other artifacts. AWS SAM also provides a command line interface (the AWS
SAM CLI), which has the same packaging and deployment functionality as the AWS CLI, with additional
features specific to Lambda applications. Use the AWS SAM CLI to test your application locally in a
Docker container that emulates the Lambda execution environment.
AWS Cloud9 provides an online development environment that includes Node.js, the AWS SAM CLI, and
Docker. With AWS Cloud9, you can start developing quickly and access your development environment
from any computer. For instructions, see Getting started in the AWS Cloud9 User Guide.
For local development, AWS toolkits for integrated development environments (IDEs) let you test and
debug functions before pushing them to your repository.
• AWS Toolkit for JetBrains – Plugin for PyCharm (Python) and IntelliJ (Java) IDEs.
• AWS Toolkit for Eclipse – Plugin for Eclipse IDE (multiple languages).
• AWS Toolkit for Visual Studio Code – Plugin for Visual Studio Code IDE (multiple languages).
• AWS Toolkit for Visual Studio – Plugin for Visual Studio IDE (multiple languages).
Troubleshooting
As you develop your application, you will likely encounter the following types of errors.
• Build errors – Issues that occur during the build phase, including compilation, test, and packaging
errors.
• Deployment errors – Issues that occur when AWS CloudFormation isn't able to update the application
stack. These include permissions errors, account quotas, service issues, or template errors.
• Invocation errors – Errors that are returned by a function's code or runtime.
For build and deployment errors, you can identify the cause of an error in the Lambda console.
For deployment errors that occur during the ExecuteChangeSet action, the pipeline links to a list of
stack events in the AWS CloudFormation console. Search for an event with the status UPDATE_FAILED.
Because AWS CloudFormation rolls back after an error, the relevant event is under several other events
in the list. If AWS CloudFormation could not create a change set, the error appears under Change sets
instead of under Events.
A common cause of deployment and invocation errors is a lack of permissions in one or more roles.
The pipeline has a role for deployments (CloudFormationRole) that's equivalent to the user
permissions (p. 56) that you would use to update an AWS CloudFormation stack directly. If you add
resources to your application or enable Lambda features that require user permissions, the deployment
role is used. You can find a link to the deployment role under Infrastructure in the application overview.
185
AWS Lambda Developer Guide
Clean up
If your function accesses other AWS services or resources, or if you enable features that require the
function to have additional permissions, the function's execution role (p. 47) is used. All execution roles
that are created in your application template are also subject to the application's permissions boundary.
This boundary requires you to explicitly grant access to additional services and resources in IAM after
adding permissions to the execution role in the template.
For example, to connect a function to a virtual private cloud (p. 111) (VPC), you need user permissions to
describe VPC resources. The execution role needs permission to manage network interfaces. This requires
the following steps.
After you address permissions errors, choose Release change in the pipeline overview to rerun the build
and deployment.
Clean up
You can continue to modify and use the sample to develop your own application. If you are done using
the sample, delete the application to avoid paying for the pipeline, repository, and storage.
Function logs are not associated with the application or infrastructure stack in AWS CloudFormation.
Delete the log group separately in the CloudWatch Logs console.
186
AWS Lambda Developer Guide
Rolling deployments
You configure a rolling deployment by using AWS CodeDeploy and AWS SAM. CodeDeploy is a service
that automates application deployments to Amazon computing platforms such as Amazon EC2 and AWS
Lambda. For more information, see What is CodeDeploy?. By using CodeDeploy to deploy your Lambda
function, you can easily monitor the status of the deployment and initiate a rollback if you detect any
issues.
AWS SAM is an open-source framework for building serverless applications. You create an AWS
SAM template (in YAML format) to specify the configuration of the components required for the
rolling deployment. AWS SAM uses the template to create and configure the components. For more
information, see What is the AWS SAM?.
The alias routing configuration is the underlying capability that implements the rolling deployment.
• It creates a CodeDeploy application and deployment group.
The deployment group manages the rolling deployment and the rollback (if needed).
• It detects when you create a new version of your Lambda function.
• It triggers CodeDeploy to start the deployment of the new version.
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A sample SAM template for deploying Lambda functions.
Resources:
# Details about the myDateTimeFunction Lambda function
myDateTimeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: myDateTimeFunction.handler
Runtime: nodejs12.x
# Creates an alias named "live" for the function, and automatically publishes when you
update the function.
AutoPublishAlias: live
DeploymentPreference:
# Specifies the deployment configuration
Type: Linear10PercentEvery2Minutes
This template defines a Lambda function named myDateTimeFunction with the following properties.
187
AWS Lambda Developer Guide
Example AWS SAM Lambda template
AutoPublishAlias
The AutoPublishAlias property creates an alias named live. In addition, the AWS SAM
framework automatically detects when you save new code for the function. The framework then
publishes a new function version and updates the live alias to point to the new version.
DeploymentPreference
The DeploymentPreference property determines the rate at which the CodeDeploy application
shifts traffic from the original version of the Lambda function to the new version. The value
Linear10PercentEvery2Minutes shifts an additional ten percent of the traffic to the new
version every two minutes.
For a detailed tutorial on how to use CodeDeploy with Lambda functions, see Deploy an updated
Lambda function with CodeDeploy.
188
AWS Lambda Developer Guide
Use cases
• File processing – Suppose you have a photo sharing application. People use your application to upload
photos, and the application stores these user photos in an Amazon S3 bucket. Then, your application
creates a thumbnail version of each user's photos and displays them on the user's profile page. In this
scenario, you may choose to create a Lambda function that creates a thumbnail automatically. Amazon
S3 is one of the supported AWS event sources that can publish object-created events and invoke your
Lambda function. Your Lambda function code can read the photo object from the S3 bucket, create a
thumbnail version, and then save it in another S3 bucket.
• Data and analytics – Suppose you are building an analytics application and storing raw data in a
DynamoDB table. When you write, update, or delete items in a table, DynamoDB streams can publish
item update events to a stream associated with the table. In this case, the event data provides the
item key, event name (such as insert, update, and delete), and other relevant details. You can write a
Lambda function to generate custom metrics by aggregating raw data.
• Websites – Suppose you are creating a website and you want to host the backend logic on Lambda.
You can invoke your Lambda function over HTTP using Amazon API Gateway as the HTTP endpoint.
Now, your web client can invoke the API, and then API Gateway can route the request to Lambda.
• Mobile applications – Suppose you have a custom mobile application that produces events. You can
create a Lambda function to process events published by your custom application. For example, in
this scenario you can configure a Lambda function to process the clicks within your custom mobile
application.
AWS Lambda supports many AWS services as event sources. For more information, see Using AWS
Lambda with other services (p. 257). When you configure these event sources to trigger a Lambda
function, the Lambda function is invoked automatically when events occur. You define event source
mapping, which is how you identify what events to track and which Lambda function to invoke.
The following are introductory examples of event sources and how the end-to-end experience works.
189
AWS Lambda Developer Guide
Example 2: AWS Lambda pulls events from a
Kinesis stream and invokes a Lambda function
You configure Amazon S3 to invoke your function as a bucket notification action. To grant Amazon S3
permission to invoke the function, update the function's resource-based policy (p. 51).
The following diagram shows how a custom application writes records to a Kinesis stream.
190
AWS Lambda Developer Guide
Example 2: AWS Lambda pulls events from a
Kinesis stream and invokes a Lambda function
When working with stream-based event sources, you create event source mappings in AWS Lambda.
Lambda reads items from the stream invokes the function synchronously. You don't need to grant
Lambda permission to invoke the function, but it does need permission to read from the stream.
191
AWS Lambda Developer Guide
Best practices
Topics
• Function code (p. 192)
• Function configuration (p. 193)
• Metrics and alarms (p. 194)
• Working with streams (p. 194)
For more information about best practices for Lambda applications, see Application design in the
Lambda operator guide.
Function code
• Separate the Lambda handler from your core logic. This allows you to make a more unit-testable
function. In Node.js this may look like:
callback(null, result);
}
• Take advantage of execution environment reuse to improve the performance of your function.
Initialize SDK clients and database connections outside of the function handler, and cache static assets
locally in the /tmp directory. Subsequent invocations processed by the same instance of your function
can reuse these resources. This saves cost by reducing function run time.
To avoid potential data leaks across invocations, don’t use the execution environment to store user
data, events, or other information with security implications. If your function relies on a mutable state
that can’t be stored in memory within the handler, consider creating a separate function or separate
versions of a function for each user.
• Use a keep-alive directive to maintain persistent connections. Lambda purges idle connections over
time. Attempting to reuse an idle connection when invoking a function will result in a connection error.
To maintain your persistent connection, use the keep-alive directive associated with your runtime. For
an example, see Reusing Connections with Keep-Alive in Node.js.
• Use environment variables (p. 77) to pass operational parameters to your function. For example, if
you are writing to an Amazon S3 bucket, instead of hard-coding the bucket name you are writing to,
configure the bucket name as an environment variable.
• Control the dependencies in your function's deployment package. The AWS Lambda execution
environment contains a number of libraries such as the AWS SDK for the Node.js and Python runtimes
(a full list can be found here: Lambda runtimes (p. 195)). To enable the latest set of features and
security updates, Lambda will periodically update these libraries. These updates may introduce subtle
changes to the behavior of your Lambda function. To have full control of the dependencies your
function uses, package all of your dependencies with your deployment package.
192
AWS Lambda Developer Guide
Function configuration
• Minimize your deployment package size to its runtime necessities. This will reduce the amount of
time that it takes for your deployment package to be downloaded and unpacked ahead of invocation.
For functions authored in Java or .NET Core, avoid uploading the entire AWS SDK library as part of
your deployment package. Instead, selectively depend on the modules which pick up components of
the SDK you need (e.g. DynamoDB, Amazon S3 SDK modules and Lambda core libraries).
• Reduce the time it takes Lambda to unpack deployment packages authored in Java by putting your
dependency .jar files in a separate /lib directory. This is faster than putting all your function’s code
in a single jar with a large number of .class files. See Deploy Java Lambda functions with .zip or JAR
file archives (p. 578) for instructions.
• Minimize the complexity of your dependencies. Prefer simpler frameworks that load quickly on
execution environment (p. 200) startup. For example, prefer simpler Java dependency injection (IoC)
frameworks like Dagger or Guice, over more complex ones like Spring Framework.
• Avoid using recursive code in your Lambda function, wherein the function automatically calls itself
until some arbitrary criteria is met. This could lead to unintended volume of function invocations and
escalated costs. If you do accidentally do so, set the function reserved concurrency to 0 immediately to
throttle all invocations to the function, while you update the code.
Function configuration
• Performance testing your Lambda function is a crucial part in ensuring you pick the optimum
memory size configuration. Any increase in memory size triggers an equivalent increase in CPU
available to your function. The memory usage for your function is determined per-invoke and can be
viewed in Amazon CloudWatch. On each invoke a REPORT: entry will be made, as shown below:
By analyzing the Max Memory Used: field, you can determine if your function needs more memory
or if you over-provisioned your function's memory size.
To find the right memory configuration for your functions, we recommend using the open source
AWS Lambda Power Tuning project. For more information, see AWS Lambda Power Tuning on GitHub.
To optimize function performance, we also recommend deploying libraries that can leverage Advanced
Vector Extensions 2 (AVX2). This allows you to process demanding workloads, including machine
learning inferencing, media processing, high performance computing (HPC), scientific simulations, and
financial modeling. For more information, see Creating faster AWS Lambda functions with AVX2.
• Load test your Lambda function to determine an optimum timeout value. It is important to analyze
how long your function runs so that you can better determine any problems with a dependency
service that may increase the concurrency of the function beyond what you expect. This is especially
important when your Lambda function makes network calls to resources that may not handle
Lambda's scaling.
• Use most-restrictive permissions when setting IAM policies. Understand the resources and
operations your Lambda function needs, and limit the execution role to these permissions. For more
information, see AWS Lambda permissions (p. 46).
• Be familiar with Lambda quotas (p. 44). Payload size, file descriptors and /tmp space are often
overlooked when determining runtime resource limits.
• Delete Lambda functions that you are no longer using. By doing so, the unused functions won't
needlessly count against your deployment package size limit.
• If you are using Amazon Simple Queue Service as an event source, make sure the value of the
function's expected invocation time does not exceed the Visibility Timeout value on the queue. This
applies both to CreateFunction (p. 763) and UpdateFunctionConfiguration (p. 936).
• In the case of CreateFunction, AWS Lambda will fail the function creation process.
193
AWS Lambda Developer Guide
Metrics and alarms
By default, Lambda invokes your function as soon as records are available in the stream. If the batch
that Lambda reads from the stream only has one record in it, Lambda sends only one record to the
function. To avoid invoking the function with a small number of records, you can tell the event source
to buffer records for up to five minutes by configuring a batch window. Before invoking the function,
Lambda continues to read records from the stream until it has gathered a full batch, or until the batch
window expires.
• Increase Kinesis stream processing throughput by adding shards. A Kinesis stream is composed
of one or more shards. Lambda will poll each shard with at most one concurrent invocation. For
example, if your stream has 100 active shards, there will be at most 100 Lambda function invocations
running concurrently. Increasing the number of shards will directly increase the number of maximum
concurrent Lambda function invocations and can increase your Kinesis stream processing throughput.
If you are increasing the number of shards in a Kinesis stream, make sure you have picked a good
partition key (see Partition Keys) for your data, so that related records end up on the same shards and
your data is well distributed.
• Use Amazon CloudWatch on IteratorAge to determine if your Kinesis stream is being processed. For
example, configure a CloudWatch alarm with a maximum setting to 30000 (30 seconds).
194
AWS Lambda Developer Guide
Lambda runtimes
Lambda supports multiple languages through the use of runtimes (p. 30). For a function defined as a
container image (p. 72), you choose a runtime and the Linux distribution when you create the container
image (p. 248). To change the runtime, you create a new container image.
When you use a .zip file archive for the deployment package, you choose a runtime when you create
the function. To change the runtime, you can update your function's configuration (p. 70). The runtime
is paired with one of the Amazon Linux distributions. The underlying execution environment provides
additional libraries and environment variables (p. 77) that you can access from your function code.
Amazon Linux
• Image – amzn-ami-hvm-2018.03.0.20181129-x86_64-gp2
• Linux kernel – 4.14.171-105.231.amzn1.x86_64
Amazon Linux 2
• Image – Custom
• Linux kernel – 4.14.165-102.205.amzn2.x86_64
When your function is invoked, Lambda attempts to re-use the execution environment from a previous
invocation if one is available. This saves time preparing the execution environment, and it allows you to
save resources such as database connections and temporary files in the execution environment (p. 200)
to avoid creating them every time your function runs.
A runtime can support a single version of a language, multiple versions of a language, or multiple
languages. Runtimes specific to a language or framework version are deprecated (p. 198) when the
version reaches end of life.
Node.js runtimes
Note
For end of support information about Node.js 10, see the section called “Runtime support
policy” (p. 198).
Python runtimes
195
AWS Lambda Developer Guide
Important
Python 2.7 reached end of life on January 1, 2020. End of support (phase 1) for the Python
2.7 runtime starts on July 15, 2021. For more information, see Announcing end of support for
Python 2.7 in AWS Lambda on the AWS Compute Blog.
Ruby runtimes
Note
For end of support information about Ruby 2.5, see the section called “Runtime support
policy” (p. 198).
Java runtimes
Go runtimes
.NET runtimes
Note
For end of support information about .NET Core 2.1, see the section called “Runtime support
policy” (p. 198).
196
AWS Lambda Developer Guide
To use other languages in Lambda, you can implement a custom runtime (p. 236). The Lambda
execution environment provides a runtime interface (p. 205) for getting invocation events and sending
responses. You can deploy a custom runtime alongside your function code, or in a layer (p. 101).
Custom runtime
Topics
• Runtime support policy (p. 198)
• AWS Lambda execution environment (p. 200)
• Runtime support for Lambda container images (p. 203)
• AWS Lambda runtime API (p. 205)
• Lambda Extensions API (p. 209)
• Lambda Logs API (p. 223)
• Modifying the runtime environment (p. 231)
• Custom AWS Lambda runtimes (p. 236)
• Tutorial – Publishing a custom runtime (p. 239)
• Using AVX2 vectorization in Lambda (p. 245)
197
AWS Lambda Developer Guide
Runtime support policy
Deprecation (end of support) for a runtime occurs in two phases. In phase 1, Lambda no longer applies
security patches or other updates to the runtime. You can no longer create functions that use the
runtime, but you can continue to update existing functions. This includes updating the runtime version,
and rolling back to the previous runtime version. Note that functions that use a deprecated runtime are
no longer eligible for technical support.
In phase 2, which starts at least 30 days after the start of phase 1, you can no longer create or update
functions that use the runtime. To update a function, you need to migrate it to a supported runtime
version. After you migrate the function to a supported runtime version, you cannot rollback the function
to the previous runtime.
Lambda does not block invocations of functions that use deprecated runtime versions. Function
invocations continue indefinitely after the runtime version reaches end of support. However, AWS
strongly recommends that you migrate functions to a supported runtime version so that you continue to
receive security patches and remain eligible for technical support.
Important
Python 2.7 reached end of life on January 1, 2020. End of support (phase 1) for the Python
2.7 runtime starts on July 15, 2021. For more information, see Announcing end of support for
Python 2.7 in AWS Lambda on the AWS Compute Blog.
The following runtimes have reached or are scheduled for end of support:
.NET Core 2.1 dotnetcore2.1 Amazon Linux Aug 23, 2021 Sept 23, 2021
Python 2.7 python2.7 Amazon Linux July 15, 2021 Sept 30, 2021
Ruby 2.5 ruby2.5 Amazon Linux July 30, 2021 Aug 30, 2021
Node.js 10.x nodejs10.x Amazon Linux 2 July 30, 2021 Aug 30, 2021
In almost all cases, the end-of-life date of a language version or operating system is known well in
advance. Lambda notifies you by email if you have functions using a runtime that is scheduled for end of
198
AWS Lambda Developer Guide
Runtime support policy
support in the next 60 days. In rare cases, advance notice of support ending might not be possible. For
example, security issues that require a backwards-incompatible update, or a runtime component that
doesn't provide a long-term support (LTS) schedule.
• Node.js – github.com
• Python – devguide.python.org
• Ruby – www.ruby-lang.org
• Java – www.oracle.com and Corretto FAQs
• Go – golang.org
• .NET Core – dotnet.microsoft.com
199
AWS Lambda Developer Guide
Execution environment
The function's runtime communicates with Lambda using the Runtime API (p. 205). Extensions
communicate with Lambda using the Extensions API (p. 209). Extensions can also receive log messages
from the function by subscribing to logs using the Logs API (p. 223).
When you create your Lambda function, you specify configuration information, such as the amount
of memory available and the maximum execution time allowed for your function. Lambda uses this
information to set up the execution environment.
The function's runtime and each external extension are processes that run within the execution
environment. Permissions, resources, credentials, and environment variables are shared between the
function and the extensions.
• Init: In this phase, Lambda creates or unfreezes an execution environment with the configured
resources, downloads the code for the function and all layers, initializes any extensions, initializes the
runtime, and then runs the function’s initialization code (the code outside the main handler). The Init
phase happens either during the first invocation, or in advance of function invocations if you have
enabled provisioned concurrency (p. 87).
The Init phase is split into three sub-phases: Extension init, Runtime init, and Function
init. These sub-phases ensure that all extensions and the runtime complete their setup tasks before
the function code runs.
• Invoke: In this phase, Lambda invokes the function handler. After the function runs to completion,
Lambda prepares to handle another function invocation.
• Shutdown: This phase is triggered if the Lambda function does not receive any invocations for a period
of time. In the Shutdown phase, Lambda shuts down the runtime, alerts the extensions to let them
stop cleanly, and then removes the environment. Lambda sends a Shutdown event to each extension,
which tells the extension that the environment is about to be shut down.
200
AWS Lambda Developer Guide
Runtime lifecycle
Each phase starts with an event that Lambda sends to the runtime and to all registered extensions. The
runtime and each extension indicate completion by sending a Next API request. Lambda freezes the
execution environment when the runtime and each extension have completed and there are no pending
events.
Topics
• Init phase (p. 201)
• Invoke phase (p. 201)
• Shutdown phase (p. 202)
Init phase
In the Init phase, Lambda performs three tasks:
The Init phase ends when the runtime and all extensions signal that they are ready by sending a
Next API request. The Init phase is limited to 10 seconds. If all three tasks do not complete within 10
seconds, Lambda retries the Init phase at the time of the first function invocation.
Invoke phase
When a Lambda function is invoked in response to a Next API request, Lambda sends an Invoke event
to the runtime and to each extension.
The function's timeout setting limits the duration of the entire Invoke phase. For example, if you set the
function timeout as 360 seconds, the function and all extensions need to complete within 360 seconds.
Note that there is no independent post-invoke phase. The duration is the sum of all invocation time
(runtime + extensions) and is not calculated until the function and all extensions have finished executing.
The invoke phase ends after the runtime and all extensions signal that they are done by sending a Next
API request.
If the Lambda function crashes or times out during the Invoke phase, Lambda resets the execution
environment. The reset behaves like a Shutdown event. First, Lambda shuts down the runtime. Then
Lambda sends a Shutdown event to each registered external extension. The event includes the reason
for the shutdown. If another Invoke event results in this execution environment being reused, Lambda
initializes the runtime and extensions as part of the next invocation.
201
AWS Lambda Developer Guide
Runtime lifecycle
Shutdown phase
When Lambda is about to shut down the runtime, it sends a Shutdown event to the runtime and to
each external extension. Extensions can use this time for final cleanup tasks. The Shutdown event is a
response to a Next API request.
Duration: The entire Shutdown phase is capped at 2 seconds. If the runtime or any extension does not
respond, Lambda terminates it via a signal (SIGKILL).
After the function and all extensions have completed, Lambda maintains the execution environment
for some time in anticipation of another function invocation. In effect, Lambda freezes the execution
environment. When the function is invoked again, Lambda thaws the environment for reuse. Reusing the
execution environment has the following implications:
• Objects declared outside of the function's handler method remain initialized, providing additional
optimization when the function is invoked again. For example, if your Lambda function establishes
a database connection, instead of reestablishing the connection, the original connection is used in
subsequent invocations. We recommend adding logic in your code to check if a connection exists
before creating a new one.
• Each execution environment provides 512 MB of disk space in the /tmp directory. The directory
content remains when the execution environment is frozen, providing a transient cache that can be
used for multiple invocations. You can add extra code to check if the cache has the data that you
stored. For more information on deployment size limits, see Lambda quotas (p. 44).
• Background processes or callbacks that were initiated by your Lambda function and did not complete
when the function ended resume if Lambda reuses the execution environment. Make sure that any
background processes or callbacks in your code are complete before the code exits.
When you write your function code, do not assume that Lambda automatically reuses the execution
environment for subsequent function invocations. Other factors may dictate a need for Lambda to
create a new execution environment, which can lead to unexpected results, such as database connection
failures.
202
AWS Lambda Developer Guide
Container images
Topics
• AWS base images for Lambda (p. 203)
• Base images for custom runtimes (p. 203)
• Runtime interface clients (p. 203)
• Runtime interface emulator (p. 204)
AWS will maintain and regularly update these images. In addition, AWS will release an AWS base images
when any new managed runtime becomes available.
DockerHub: amazon/aws-lambda-provided
203
AWS Lambda Developer Guide
Runtime interface emulator
HTTP interface for runtimes to receive invocation events from Lambda and respond with success or
failure indications.
Each of the AWS base images for Lambda include a runtime interface client. If you choose one of the
base images for custom runtimes or an alternative base image, you need to add the appropriate runtime
interface client.
For your convenience, Lambda provides an open source runtime interface client for each of the
supported Lambda runtimes:
204
AWS Lambda Developer Guide
Runtime API
The OpenAPI specification for the runtime API version 2018-06-01 is available in runtime-api.zip
To create an API request URL, runtimes get the API endpoint from the AWS_LAMBDA_RUNTIME_API
environment variable, add the API version, and add the desired resource path.
Example Request
curl "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next"
API methods
• Next invocation (p. 205)
• Invocation response (p. 206)
• Initialization error (p. 206)
• Invocation error (p. 207)
Next invocation
Path – /runtime/invocation/next
Method – GET
The runtime sends this message to Lambda to request an invocation event. The response body contains
the payload from the invocation, which is a JSON document that contains event data from the function
trigger. The response headers contain additional data about the invocation.
Response headers
• Lambda-Runtime-Aws-Request-Id – The request ID, which identifies the request that triggered the
function invocation.
205
AWS Lambda Developer Guide
Invocation response
Do not set a timeout on the GET request as the response may be delayed. Between when Lambda
bootstraps the runtime and when the runtime has an event to return, the runtime process may be frozen
for several seconds.
The request ID tracks the invocation within Lambda. Use it to specify the invocation when you send the
response.
The tracing header contains the trace ID, parent ID, and sampling decision. If the request is
sampled, the request was sampled by Lambda or an upstream service. The runtime should set the
_X_AMZN_TRACE_ID with the value of the header. The X-Ray SDK reads this to get the IDs and
determine whether to trace the request.
Invocation response
Path – /runtime/invocation/AwsRequestId/response
Method – POST
After the function has run to completion, the runtime sends an invocation response to Lambda. For
synchronous invocations, Lambda sends the response to the client.
REQUEST_ID=156cb537-e2d4-11e8-9b34-d36013741fb9
curl -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/
response" -d "SUCCESS"
Initialization error
If the function returns an error or the runtime encounters an error during initialization, the runtime uses
this method to report the error to Lambda.
Path – /runtime/init/error
Method – POST
Headers
This header consists of a string value. Lambda accepts any string, but we recommend a format of
<category.reason>. For example:
• Runtime.NoSuchHandler
• Extension.APIKeyNotFound
• Extension.ConfigInvalid
• Extension.UnknownReason
Body parameters
{
errorMessage: string (text description of the error),
206
AWS Lambda Developer Guide
Invocation error
errorType: string,
stackTrace: array of strings
}
The following example shows a Lambda function error message in which the function could not parse
the event data provided in the invocation.
{
"errorMessage" : "Error parsing event data.",
"errorType" : "InvalidEventDataException",
"stackTrace": [ ]
}
Response codes
• 202 – Accepted
• 403 – Forbidden
• 500 – Container error. Non-recoverable state. Extension should exit promptly.
Invocation error
If the function returns an error or the runtime encounters an error, the runtime uses this method to
report the error to Lambda.
Path – /runtime/invocation/AwsRequestId/error
Method – POST
Headers
This header consists of a string value. Lambda accepts any string, but we recommend a format of
<category.reason>. For example:
• Runtime.NoSuchHandler
• Extension.APIKeyNotFound
• Extension.ConfigInvalid
207
AWS Lambda Developer Guide
Invocation error
• Extension.UnknownReason
Body parameters
{
errorMessage: string (text description of the error),
errorType: string,
stackTrace: array of strings
}
The following example shows a Lambda function error message in which the function could not parse
the event data provided in the invocation.
{
"errorMessage" : "Error parsing event data.",
"errorType" : "InvalidEventDataException",
"stackTrace": [ ]
}
Response codes
• 202 – Accepted
• 400 – Bad Request
• 403 – Forbidden
• 500 – Container error. Non-recoverable state. Extension should exit promptly.
REQUEST_ID=156cb537-e2d4-11e8-9b34-d36013741fb9
ERROR="{\"errorMessage\" : \"Error parsing event data.\", \"errorType\" :
\"InvalidEventDataException\"}"
curl -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/
error" -d "$ERROR" --header "Lambda-Runtime-Function-Error-Type: Unhandled"
208
AWS Lambda Developer Guide
Extensions API
As an extension author, you can use the Lambda Extensions API to integrate deeply into the Lambda
execution environment (p. 200). Your extension can register for function and execution environment
lifecycle events. In response to these events, you can start new processes, run logic, and control and
participate in all phases of the Lambda lifecycle: initialization, invocation, and shutdown. In addition, you
can use the Runtime Logs API (p. 223) to receive a stream of logs.
An extension runs as an independent process in the execution environment and can continue to run
after the function invocation is fully processed. Because extensions run as processes, you can write
them in a different language than the function. We recommend that you implement extensions using a
compiled language. In this case, the extension is a self-contained binary that is compatible with all of the
supported runtimes. If you use a non-compiled language, ensure that you include a compatible runtime
in the extension.
Note that the Go 1.x runtime does not support extensions. To support extensions, you can create Go
functions on the provided.al2 runtime. For more information, see Migrating Lambda functions to
Amazon Linux 2.
Lambda also supports internal extensions. An internal extension runs as a separate thread in the runtime
process. The runtime starts and stops the internal extension. An alternative way to integrate with the
Lambda environment is to use language-specific environment variables and wrapper scripts (p. 231).
You can use these to configure the runtime environment and modify the startup behavior of the runtime
process.
You can add extensions to a function in two ways. For a function deployed as a .zip file archive (p. 41),
you deploy your extension as a layer (p. 101). For a function defined as a container image, you add the
extensions (p. 162) to your container image.
Note
For example extensions and wrapper scripts, see AWS Lambda Extensions on the AWS Samples
GitHub repository.
209
AWS Lambda Developer Guide
Lambda execution environment lifecycle
Topics
• Lambda execution environment lifecycle (p. 210)
• Extensions API reference (p. 218)
• Init: In this phase, Lambda creates or unfreezes an execution environment with the configured
resources, downloads the code for the function and all layers, initializes any extensions, initializes the
runtime, and then runs the function’s initialization code (the code outside the main handler). The Init
phase happens either during the first invocation, or in advance of function invocations if you have
enabled provisioned concurrency (p. 87).
The Init phase is split into three sub-phases: Extension init, Runtime init, and Function
init. These sub-phases ensure that all extensions and the runtime complete their setup tasks before
the function code runs.
• Invoke: In this phase, Lambda invokes the function handler. After the function runs to completion,
Lambda prepares to handle another function invocation.
• Shutdown: This phase is triggered if the Lambda function does not receive any invocations for a period
of time. In the Shutdown phase, Lambda shuts down the runtime, alerts the extensions to let them
stop cleanly, and then removes the environment. Lambda sends a Shutdown event to each extension,
which tells the extension that the environment is about to be shut down.
Each phase starts with an event from Lambda to the runtime and to all registered extensions. The
runtime and each extension signal completion by sending a Next API request. Lambda freezes the
execution environment when each process has completed and there are no pending events.
210
AWS Lambda Developer Guide
Lambda execution environment lifecycle
Topics
• Init phase (p. 212)
• Invoke phase (p. 201)
• Shutdown phase (p. 202)
• Permissions and configuration (p. 216)
• Failure handling (p. 217)
• Troubleshooting extensions (p. 218)
211
AWS Lambda Developer Guide
Lambda execution environment lifecycle
Init phase
During the Extension init phase, each extension needs to register with Lambda to receive events.
Lambda uses the full file name of the extension to validate that the extension has completed the
bootstrap sequence. Therefore, each Register API call must include the Lambda-Extension-Name
header with the full file name of the extension.
You can register up to 10 extensions for a function. This limit is enforced through the Register API call.
After each extension registers, Lambda starts the Runtime init phase. The runtime process calls
functionInit to start the Function init phase.
The Init phase completes after the runtime and each registered extension indicate completion by
sending a Next API request.
Note
Extensions can complete their initialization at any point in the Init phase.
212
AWS Lambda Developer Guide
Lambda execution environment lifecycle
Invoke phase
When a Lambda function is invoked in response to a Next API request, Lambda sends an Invoke event
to the runtime and to each extension that is registered for the Invoke event.
During the invocation, external extensions run in parallel with the function. They also continue running
after the function has completed. This enables you to capture diagnostic information or to send logs,
metrics, and traces to a location of your choice.
After receiving the function response from the runtime, Lambda returns the response to the client, even
if extensions are still running.
213
AWS Lambda Developer Guide
Lambda execution environment lifecycle
The Invoke phase ends after the runtime and all extensions signal that they are done by sending a Next
API request.
Event payload: The event sent to the runtime (and the Lambda function) carries the entire request,
headers (such as RequestId), and payload. The event sent to each extension contains metadata that
describes the event content. This lifecycle event includes the type of the event, the time that the
function times out (deadlineMs), the requestId, the invoked function's Amazon Resource Name
(ARN), and tracing headers.
Extensions that want to access the function event body can use an in-runtime SDK that communicates
with the extension. Function developers use the in-runtime SDK to send the payload to the extension
when the function is invoked.
{
"eventType": "INVOKE",
"deadlineMs": 676051,
"requestId": "3da1f2dc-3222-475e-9205-e2e6c6318895",
"invokedFunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:ExtensionTest",
214
AWS Lambda Developer Guide
Lambda execution environment lifecycle
"tracing": {
"type": "X-Amzn-Trace-Id",
"value":
"Root=1-5f35ae12-0c0fec141ab77a00bc047aa2;Parent=2be948a625588e32;Sampled=1"
}
}
Duration limit: The function's timeout setting (p. 70) limits the duration of the entire Invoke phase.
For example, if you set the function timeout as 360 seconds, the function and all extensions need to
complete within 360 seconds. Note that there is no independent post-invoke phase. The duration is
the sum of all invocation time (runtime + extensions) and is not calculated until the function and all
extensions have finished running.
Performance impact and extension overhead: Extensions can impact function performance. As an
extension author, you have control over the performance impact of your extension. For example, if
your extension performs compute-intensive operations, the function's duration increases because the
extension and the function code share the same CPU resources. In addition, if your extension performs
extensive operations after the function invocation completes, the function duration increases because
the Invoke phase continues until all extensions signal that they are completed.
Note
Lambda allocates CPU power in proportion to the function's memory setting. You might see
increased execution and initialization duration at lower memory settings because the function
and extension processes are competing for the same CPU resources. To reduce the execution and
initialization duration, try increasing the memory setting.
To help identify the performance impact introduced by extensions on the Invoke phase, Lambda
outputs the PostRuntimeExtensionsDuration metric. This metric measures the cumulative time
spent between the runtime Next API request and the last extension Next API request. To measure the
increase in memory used, use the MaxMemoryUsed metric. For more information about function metrics,
see Working with AWS Lambda function metrics (p. 692).
Function developers can run different versions of their functions side by side to understand the impact
of a specific extension. We recommend that extension authors publish expected resource consumption to
make it easier for function developers to choose a suitable extension.
Shutdown phase
When Lambda is about to shut down the runtime, it sends a Shutdown event to the runtime and then to
each registered external extension. Extensions can use this time for final cleanup tasks. The Shutdown
event is sent in response to a Next API request.
Duration limit: The maximum duration of the Shutdown phase depends on the configuration of
registered extensions:
For a function with external extensions, Lambda reserves up to 300 ms (500 ms for a runtime with
an internal extension) for the runtime process to perform a graceful shutdown. Lambda allocates the
remainder of the 2,000 ms limit for external extensions to shut down.
If the runtime or an extension does not respond to the Shutdown event within the limit, Lambda ends
the process using a SIGKILL signal.
215
AWS Lambda Developer Guide
Lambda execution environment lifecycle
Event payload: The Shutdown event contains the reason for the shutdown and the time remaining in
milliseconds.
{
"eventType": "SHUTDOWN",
"shutdownReason": "reason for shutdown",
"deadlineMs": "the time and date that the function times out in Unix time milliseconds"
}
216
AWS Lambda Developer Guide
Lambda execution environment lifecycle
File system and network access permissions: Extensions run in the same file system and network
name namespace as the function runtime. This means that extensions need to be compatible with the
associated operating system. If an extension requires any additional outbound network traffic rules, you
must apply these rules to the function configuration.
Note
Because the function code directory is read-only, extensions cannot modify the function code.
Environment variables: Extensions can access the function's environment variables (p. 77), except for the
following variables that are specific to the runtime process:
• AWS_EXECUTION_ENV
• AWS_LAMBDA_LOG_GROUP_NAME
• AWS_LAMBDA_LOG_STREAM_NAME
• AWS_XRAY_CONTEXT_MISSING
• AWS_XRAY_DAEMON_ADDRESS
• LAMBDA_RUNTIME_DIR
• LAMBDA_TASK_ROOT
• _AWS_XRAY_DAEMON_ADDRESS
• _AWS_XRAY_DAEMON_PORT
• _HANDLER
Failure handling
Initialization failures: If an extension fails, Lambda restarts the execution environment to enforce
consistent behavior and to encourage fail fast for extensions. Also, for some customers, the extensions
must meet mission-critical needs such as logging, security, governance, and telemetry collection.
Invoke failures (such as out of memory, function timeout): Because extensions share resources with the
runtime, memory exhaustion affects them. When the runtime fails, all extensions and the runtime itself
participate in the Shutdown phase. In addition, the runtime is restarted—either automatically as part of
the current invocation, or via a deferred re-initialization mechanism.
If there is a failure (such as a function timeout or runtime error) during Invoke, the Lambda service
performs a reset. The reset behaves like a Shutdown event. First, Lambda shuts down the runtime, then
it sends a Shutdown event to each registered external extension. The event includes the reason for the
shutdown. If this environment is used for a new invocation, the extension and runtime are re-initialized
as part of the next invocation.
Extension logs: Lambda sends the log output of extensions to CloudWatch Logs. Lambda also generates
an additional log event for each extension during Init. The log event records the name and registration
preference (event, config) on success, or the failure reason on failure.
217
AWS Lambda Developer Guide
Extensions API reference
Troubleshooting extensions
• If a Register request fails, make sure that the Lambda-Extension-Name header in the Register
API call contains the full file name of the extension.
• If the Register request fails for an internal extension, make sure that the request does not register
for the Shutdown event.
You can retrieve the value of the API endpoint from the AWS_LAMBDA_RUNTIME_API environment
variable. To send a Register request, use the prefix 2020-01-01/ before each API path. For example:
http://${AWS_LAMBDA_RUNTIME_API}/2020-01-01/extension/register
API methods
• Register (p. 218)
• Next (p. 219)
• Init error (p. 219)
• Exit error (p. 221)
Register
During Extension init, all extensions need to register with Lambda to receive events. Lambda uses
the full file name of the extension to validate that the extension has completed the bootstrap sequence.
Therefore, each Register API call must include the Lambda-Extension-Name header with the full file
name of the extension.
Internal extensions are started and stopped by the runtime process, so they are not permitted to register
for the Shutdown event.
Path – /extension/register
Method – POST
Headers
Lambda-Extension-Name – The full file name of the extension. Required: yes. Type: string.
Body parameters
events – Array of the events to register for. Required: no. Type: array of strings. Valid strings: INVOKE,
SHUTDOWN.
Response headers
Response codes
• 200 – Response body contains the function name, function version, and handler name.
218
AWS Lambda Developer Guide
Extensions API reference
{
'events': [ 'INVOKE', 'SHUTDOWN']
}
{
"functionName": "helloWorld",
"functionVersion": "$LATEST",
"handler": "lambda_function.lambda_handler"
}
Next
Extensions send a Next API request to receive the next event, which can be an Invoke event or a
Shutdown event. The response body contains the payload, which is a JSON document that contains
event data.
The extension sends a Next API request to signal that it is ready to receive new events. This is a blocking
call.
Do not set a timeout on the GET call, as the extension can be suspended for a period of time until there
is an event to return.
Path – /extension/event/next
Method – GET
Parameters
Lambda-Extension-Identifier – Unique identifier for extension (UUID string). Required: yes. Type:
UUID string.
Response header
Response codes
• 200 – Response contains information about the next event (EventInvoke or EventShutdown).
• 403 – Forbidden
• 500 – Container error. Non-recoverable state. Extension should exit promptly.
Init error
The extension uses this method to report an initialization error to Lambda. Call it when the extension
fails to initialize after it has registered. After Lambda receives the error, no further API calls succeed. The
extension should exit after it receives the response from Lambda.
219
AWS Lambda Developer Guide
Extensions API reference
Path – /extension/init/error
Method – POST
Headers
Lambda-Extension-Identifier – Unique identifier for extension. Required: yes. Type: UUID string.
This header consists of a string value. Lambda accepts any string, but we recommend a format of
<category.reason>. For example:
• Runtime.NoSuchHandler
• Extension.APIKeyNotFound
• Extension.ConfigInvalid
• Extension.UnknownReason
Body parameters
{
errorMessage: string (text description of the error),
errorType: string,
stackTrace: array of strings
}
The following example shows a Lambda function error message in which the function could not parse
the event data provided in the invocation.
{
"errorMessage" : "Error parsing event data.",
"errorType" : "InvalidEventDataException",
"stackTrace": [ ]
}
Response body
Response codes
• 202 – Accepted
• 400 – Bad Request
• 403 – Forbidden
• 500 – Container error. Non-recoverable state. Extension should exit promptly.
220
AWS Lambda Developer Guide
Extensions API reference
Exit error
The extension uses this method to report an error to Lambda before exiting. Call it when you encounter
an unexpected failure. After Lambda receives the error, no further API calls succeed. The extension
should exit after it receives the response from Lambda.
Path – /extension/exit/error
Method – POST
Headers
Lambda-Extension-Identifier – Unique identifier for extension. Required: yes. Type: UUID string.
This header consists of a string value. Lambda accepts any string, but we recommend a format of
<category.reason>. For example:
• Runtime.NoSuchHandler
• Extension.APIKeyNotFound
• Extension.ConfigInvalid
• Extension.UnknownReason
Body parameters
{
errorMessage: string (text description of the error),
errorType: string,
stackTrace: array of strings
}
The following example shows a Lambda function error message in which the function could not parse
the event data provided in the invocation.
{
"errorMessage" : "Error parsing event data.",
"errorType" : "InvalidEventDataException",
"stackTrace": [ ]
}
Response body
Response codes
• 202 – Accepted
221
AWS Lambda Developer Guide
Extensions API reference
222
AWS Lambda Developer Guide
Logs API
Lambda extensions (p. 209) can use the Lambda Runtime Logs API to subscribe to log streams directly
from within the Lambda execution environment (p. 200). Lambda streams the logs to the extension, and
the extension can then process, filter, and send the logs to any preferred destination.
The Logs API allows extensions to subscribe to three different logs streams:
• Function logs that the Lambda function generates and writes to stdout or stderr.
• Extension logs that extension code generates.
• Lambda platform logs, which record events and errors related to invocations and extensions.
Note
Lambda sends all logs to CloudWatch, even when an extension subscribes to one or more of the
log streams.
Topics
• Subscribing to receive logs (p. 224)
• Memory usage (p. 224)
• Destination protocols (p. 224)
• Buffering configuration (p. 224)
• Example subscription (p. 225)
• Sample code for Logs API (p. 225)
• Logs API reference (p. 226)
• Log messages (p. 227)
223
AWS Lambda Developer Guide
Subscribing to receive logs
To subscribe to receive logs, you need the extension identifier (Lambda-Extension-Identifier). First
register the extension (p. 218) to receive the extension identifier. Then subscribe to the Logs API during
initialization (p. 201). After the initialization phase completes, Lambda does not process subscription
requests.
Note
Logs API subscription is idempotent. Duplicate subscribe requests do not result in duplicate
subscriptions.
Memory usage
Memory usage increases linearly as the number of subscribers increases. Subscriptions consume memory
resources because each subscription opens a new memory buffer to store the logs. To help optimize
memory usage, you can adjust the buffering configuration (p. 224). Buffer memory usage counts
towards overall memory consumption in the execution environment.
Destination protocols
You can choose one of the following protocols to receive the logs:
We recommend using HTTP rather than TCP. With TCP, the Lambda platform cannot acknowledge when
it delivers logs to the application layer. Therefore, you might lose logs if your extension crashes. HTTP
does not share this limitation.
We also recommend setting up the local HTTP listener or the TCP port before subscribing to receive logs.
During setup, note the following:
• Lambda sends logs only to destinations that are inside the execution environment.
• Lambda retries the attempt to send the logs (with backoff) if there is no listener, or if the POST or
PUT request results in an error. If the log subscriber crashes, it continues to receive logs after Lambda
restarts the execution environment.
• Lambda reserves port 9001. There are no other port number restrictions or recommendations.
Buffering configuration
Lambda can buffer logs and deliver them to the subscriber. You can configure this behavior in the
subscription request by specifying the following optional fields. Note that Lambda uses the default value
for any field that you do not specify.
• timeoutMs – The maximum time (in milliseconds) to buffer a batch. Default: 1,000. Minimum: 25.
Maximum: 30,000.
• maxBytes – The maximum size (in bytes) of the logs to buffer in memory. Default: 262,144. Minimum:
262,144. Maximum: 1,048,576.
224
AWS Lambda Developer Guide
Example subscription
• maxItems – The maximum number of events to buffer in memory. Default: 10,000. Minimum: 1,000.
Maximum: 10,000.
• Lambda flushes the logs if any of the input streams are closed, for example, if the runtime crashes.
• Each subscriber can specify a different buffering configuration in their subscription request.
• Consider the buffer size that you need for reading the data. Expect to receive payloads as large as
2*maxBytes+metadata, where maxBytes is configured in the subscribe request. For example,
Lambda adds the following metadata bytes to each record:
{
"time": "2020-08-20T12:31:32.123Z",
"type": "function",
"record": "Hello World"
}
• If the subscriber cannot process incoming logs quickly enough, Lambda might drop logs to
keep memory utilization bounded. To indicate the number of dropped records, Lambda sends a
platform.logsDropped log.
Example subscription
The following example shows a request to subscribe to the platform and function logs.
If the request succeeds, the subscriber receives an HTTP 200 success response.
HTTP/1.1 200 OK
"OK"
For Python and Go code examples showing how to develop a basic Lambda extension and subscribe to
the Logs API, see AWS Lambda Extensions on the AWS Samples GitHub repository. For more information
about building a Lambda extension, see the section called “Extensions API” (p. 209).
225
AWS Lambda Developer Guide
Logs API reference
http://${AWS_LAMBDA_RUNTIME_API}/2020-08-15/logs/
The OpenAPI specification for the Logs API version 2020-08-15 is available here: logs-api-request.zip
Subscribe
To subscribe to one or more of the log streams available in the Lambda execution environment,
extensions send a Subscribe API request.
Path – /logs
Method – PUT
Body parameters
destination – See the section called “Destination protocols” (p. 224). Required: yes. Type: strings.
buffering – See the section called “Buffering configuration” (p. 224). Required: no. Type: strings.
types – An array of the types of logs to receive. Required: yes. Type: array of strings. Valid values:
"platform", "function", "extension".
schemaVersion – Required: no. Default value: "2020-08-15". Set to "2021-03-18" for the extension to
receive platform.runtimeDone (p. 229) messages.
Response parameters
The OpenAPI specifications for the subscription responses version 2020-08-15 are available for the HTTP
and TCP protocols:
• HTTP: logs-api-http-response.zip
• TCP: logs-api-tcp-response.zip
Response codes
If the request succeeds, the subscriber receives an HTTP 200 success response.
HTTP/1.1 200 OK
"OK"
If the request fails, the subscriber receives an error response. For example:
HTTP/1.1 400 OK
226
AWS Lambda Developer Guide
Log messages
{
"errorType": "Logs.ValidationError",
"errorMessage": URI port is not provided; types should not be empty"
}
Log messages
The Logs API allows extensions to subscribe to three different logs streams:
• Function – Logs that the Lambda function generates and writes to stdout or stderr.
• Extension – Logs that extension code generates.
• Platform – Logs that the runtime platform generates, which record events and errors related to
invocations and extensions.
Topics
• Function logs (p. 227)
• Extension logs (p. 227)
• Platform logs (p. 227)
Function logs
The Lambda function and internal extensions generate function logs and write them to stdout or
stderr.
The following example shows the format of a function log message. { "time":
"2020-08-20T12:31:32.123Z", "type": "function", "record": "ERROR encountered. Stack trace:\n\my-
function (line 10)\n" }
Extension logs
Extensions can generate extension logs. The log format is the same as for a function log.
Platform logs
Lambda generates log messages for platform events such as platform.start, platform.end, and
platform.fault.
Optionally, you can subscribe to the 2021-03-18 version of the Logs API schema, which includes the
platform.runtimeDone log message.
{
"time": "2020-08-20T12:31:32.123Z",
"type": "platform.start",
"record": {"requestId": "6f7f0961f83442118a7af6fe80b88d56"}
}
{
"time": "2020-08-20T12:31:32.123Z",
"type": "platform.end",
"record": {"requestId": "6f7f0961f83442118a7af6fe80b88d56"}
227
AWS Lambda Developer Guide
Log messages
The platform report log includes metrics about the invocation that the requestId specifies. The
initDurationMs field is included in the log only if the invocation included a cold start. If AWS X-Ray
tracing is active, the log includes X-Ray metadata. The following example shows a platform report log for
an invocation that included a cold start.
{
"time": "2020-08-20T12:31:32.123Z",
"type": "platform.report",
"record": {"requestId": "6f7f0961f83442118a7af6fe80b88d56",
"metrics": {"durationMs": 101.51,
"billedDurationMs": 300,
"memorySizeMB": 512,
"maxMemoryUsedMB": 33,
"initDurationMs": 116.67
}
}
}
The platform fault log captures runtime or execution environment errors. The following example shows a
platform fault log message.
{
"time": "2020-08-20T12:31:32.123Z",
"type": "platform.fault",
"record": "RequestId: d783b35e-a91d-4251-af17-035953428a2c Process exited before
completing request"
}
Lambda generates a platform extension log when an extension registers with the extensions API. The
following example shows a platform extension message.
{
"time": "2020-08-20T12:31:32.123Z",
"type": "platform.extension",
"record": {"name": "Foo.bar",
"state": "Ready",
"events": ["INVOKE", "SHUTDOWN"]
}
}
Lambda generates a platform logs subscription log when an extension subscribes to the logs API. The
following example shows a logs subscription message.
{
"time": "2020-08-20T12:31:32.123Z",
"type": "platform.logsSubscription",
"record": {"name": "Foo.bar",
"state": "Subscribed",
"types": ["function", "platform"],
}
}
Lambda generates a platform logs dropped log when an extension is not able to process the number of
logs that it is receiving. The following example shows a platform.logsDropped log message.
228
AWS Lambda Developer Guide
Log messages
"time": "2020-08-20T12:31:32.123Z",
"type": "platform.logsDropped",
"record": {"reason": "Consumer seems to have fallen behind as it has not acknowledged
receipt of logs.",
"droppedRecords": 123,
"droppedBytes" 12345
}
}
The OpenAPI specification for the Log event type in schema version 2021-03-18 is available here:
schema-2021-03-18.zip
Lambda generates the platform.runtimeDone log message when the runtime sends a Next or
Error runtime API request. The platform.runtimeDone log informs consumers of the Logs API that
the function invocation completes. Extensions can use this information to decide when to send all the
telemetry collected during that invocation.
Examples
Lambda sends the platform.runtimeDone message after the runtime sends the NEXT request when
the function invocation completes. The following examples show messages for each of the status values:
success, failure, and timeout.
{
"time": "2021-02-04T20:00:05.123Z",
"type": "platform.runtimeDone",
"record": {
"requestId":"6f7f0961f83442118a7af6fe80b88",
"status": "success"
}
}
{
"time": "2021-02-04T20:00:05.123Z",
"type": "platform.runtimeDone",
"record": {
"requestId":"6f7f0961f83442118a7af6fe80b88",
"status": "failure"
}
}
{
"time": "2021-02-04T20:00:05.123Z",
"type": "platform.runtimeDone",
"record": {
"requestId":"6f7f0961f83442118a7af6fe80b88",
229
AWS Lambda Developer Guide
Log messages
"status": "timeout"
}
}
230
AWS Lambda Developer Guide
Runtime modifications
Lambda provides language-specific environment variables (p. 77) that you can set to add options and
tools to the runtime. Lambda also provides wrapper scripts (p. 233), which allow Lambda to delegate
the runtime startup to your script. You can create a wrapper script to customize the runtime startup
behavior.
Using language-specific environment variables is the preferred way to set startup properties.
The following example uses Byte Buddy, a library for creating and modifying Java classes during the
runtime of a Java application without the help of a compiler. Byte Buddy offers an additional API for
generating Java agents. In this example, the Agent class intercepts every call of the handleRequest
method made to the RequestStreamHandler class. This class is used internally in the runtime to wrap the
handler invocations.
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.instrument.Instrumentation;
231
AWS Lambda Developer Guide
Language-specific environment variables
The agent in the preceding example uses the TimerAdvice method. TimerAdvice measures how many
milliseconds are spent with the method call and logs the method time and details, such as name and
passed arguments.
@OnMethodEnter
static long enter() {
return System.currentTimeMillis();
}
@OnMethodExit
static void exit(@Origin String method, @Enter long start, @AllArguments Object[] args)
{
StringBuilder sb = new StringBuilder();
for (Object arg : args) {
sb.append(arg);
sb.append(", ");
}
System.out.println(method + " method with args: " + sb.toString() + " took " +
(System.currentTimeMillis() - start) + " milliseconds ");
}
}
After you create a layer that contains the agent JAR, you can pass the JAR name to the runtime's JVM by
setting an environment variable.
JAVA_TOOL_OPTIONS=-javaagent:"/opt/ExampleAgent-0.0.jar"
After invoking the function with {key=lambdaInput}, you can find the following line in the logs:
232
AWS Lambda Developer Guide
Wrapper scripts
extension (p. 218). You do not need to explicitly register for the Shutdown event, as that is automatically
sent to the runtime.
import java.lang.instrument.Instrumentation;
@OnMethodEnter
static long enter() {
String invokedFunctionArn = null;
for (Object arg : args) {
if (arg instanceof Context) {
Context context = (Context) arg;
invokedFunctionArn = context.getInvokedFunctionArn();
}
}
}
Wrapper scripts
You can create a wrapper script to customize the runtime startup behavior of your Lambda function. A
wrapper script enables you to set configuration parameters that cannot be set through language-specific
environment variables.
Note
Invocations may fail if the wrapper script does not successfully start the runtime process.
• Node.js 14.x
• Node.js 12.x
233
AWS Lambda Developer Guide
Wrapper scripts
• Node.js 10.x
• Python 3.8
• Ruby 2.7
• Java 11
• Java 8 (java8.al2)
• .NET Core 3.1
When you use a wrapper script for your function, Lambda starts the runtime using your script. Lambda
sends to your script the path to the interpreter and all of the original arguments for the standard
runtime startup. Your script can extend or transform the startup behavior of the program. For example,
the script can inject and alter arguments, set environment variables, or capture metrics, errors, and other
diagnostic information.
You specify the script by setting the value of the AWS_LAMBDA_EXEC_WRAPPER environment variable as
the file system path of an executable binary or script.
1. To create the wrapper script, paste the following code into a file named importtime_wrapper:
#!/bin/bash
# the path to the interpreter and all of the originally intended arguments
args=("$@")
2. To give the script executable permissions, enter chmod +x importtime_wrapper from the
command line.
3. Deploy the script as a Lambda layer (p. 101).
4. Create a function using the Lambda console.
a. Choose your function, and then choose Code if it is not already selected.
234
AWS Lambda Developer Guide
Wrapper scripts
a. In the function code editor (p. 17), paste the following function code:
import json
b. Choose Save.
c. Under Environment variables, choose Edit.
d. Choose Add environment variable.
e. For Key, enter AWS_LAMBDA_EXEC_WRAPPER.
f. For Value, enter /opt/importtime_wrapper.
g. Choose Save.
7. To run the function, choose Test.
Because your wrapper script started the Python interpreter with the -X importtime option, the
logs show the time required for each import. For example:
...
2020-06-30T18:48:46.780+01:00 import time: 213 | 213 | simplejson
2020-06-30T18:48:46.780+01:00 import time: 50 | 263 | simplejson.raw_json
...
235
AWS Lambda Developer Guide
Custom runtimes
A runtime is responsible for running the function's setup code, reading the handler name from an
environment variable, and reading invocation events from the Lambda runtime API. The runtime passes
the event data to the function handler, and posts the response from the handler back to Lambda.
Your custom runtime runs in the standard Lambda execution environment (p. 195). It can be a shell
script, a script in a language that's included in Amazon Linux, or a binary executable file that's compiled
in Amazon Linux.
To get started with custom runtimes, see Tutorial – Publishing a custom runtime (p. 239). You can also
explore a custom runtime implemented in C++ at awslabs/aws-lambda-cpp on GitHub.
Topics
• Using a custom runtime (p. 236)
• Building a custom runtime (p. 236)
Example function.zip
.
### bootstrap
### function.sh
If there's a file named bootstrap in your deployment package, Lambda runs that file. If not, Lambda
looks for a runtime in the function's layers. If the bootstrap file isn't found or isn't executable, your
function returns an error upon invocation.
Example bootstrap
#!/bin/sh
cd $LAMBDA_TASK_ROOT
./node-v11.1.0-linux-x64/bin/node runtime.js
Your runtime code is responsible for completing some initialization tasks. Then it processes
invocation events in a loop until it's terminated. The initialization tasks run once per instance of the
function (p. 200) to prepare the environment to handle invocations.
Initialization tasks
• Retrieve settings – Read environment variables to get details about the function and environment.
236
AWS Lambda Developer Guide
Building a custom runtime
• _HANDLER – The location to the handler, from the function's configuration. The standard format is
file.method, where file is the name of the file without an extension, and method is the name of
a method or function that's defined in the file.
• LAMBDA_TASK_ROOT – The directory that contains the function code.
• AWS_LAMBDA_RUNTIME_API – The host and port of the runtime API.
See Defined runtime environment variables (p. 80) for a full list of available variables.
• Initialize the function – Load the handler file and run any global or static code that it contains.
Functions should create static resources like SDK clients and database connections once, and reuse
them for multiple invocations.
• Handle errors – If an error occurs, call the initialization error (p. 206) API and exit immediately.
Initialization counts towards billed execution time and timeout. When an execution triggers the
initialization of a new instance of your function, you can see the initialization time in the logs and AWS
X-Ray trace (p. 451).
Example log
While it runs, a runtime uses the Lambda runtime interface (p. 205) to manage incoming events and
report errors. After completing initialization tasks, the runtime processes incoming events in a loop. In
your runtime code, perform the following steps in order.
Processing tasks
• Get an event – Call the next invocation (p. 205) API to get the next event. The response body contains
the event data. Response headers contain the request ID and other information.
• Propagate the tracing header – Get the X-Ray tracing header from the Lambda-Runtime-Trace-Id
header in the API response. Set the _X_AMZN_TRACE_ID environment variable locally with the same
value. The X-Ray SDK uses this value to connect trace data between services.
• Create a context object – Create an object with context information from environment variables and
headers in the API response.
• Invoke the function handler – Pass the event and context object to the handler.
• Handle the response – Call the invocation response (p. 206) API to post the response from the
handler.
237
AWS Lambda Developer Guide
Building a custom runtime
• Handle errors – If an error occurs, call the invocation error (p. 207) API.
• Cleanup – Release unused resources, send data to other services, or perform additional tasks before
getting the next event.
You can include the runtime in your function's deployment package, or distribute the runtime separately
in a function layer. For an example walkthrough, see Tutorial – Publishing a custom runtime (p. 239).
238
AWS Lambda Developer Guide
Tutorial – Custom runtime
Prerequisites
This tutorial assumes that you have some knowledge of basic Lambda operations and the Lambda
console. If you haven't already, follow the instructions in Getting started with Lambda (p. 9) to create
your first Lambda function.
To complete the following steps, you need a command line terminal or shell to run commands.
Commands and the expected output are listed in separate blocks:
aws --version
For long commands, an escape character (\) is used to split a command over multiple lines.
On Linux and macOS, use your preferred shell and package manager. On Windows 10, you can install the
Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.
You need an IAM role to create a Lambda function. The role needs permission to send logs to
CloudWatch Logs and access the AWS services that your function uses. If you don't have a role for
function development, create one now.
The AWSLambdaBasicExecutionRole policy has the permissions that the function needs to write
logs to CloudWatch Logs.
Create a function
Create a Lambda function with a custom runtime. This example includes two files, a runtime bootstrap
file, and a function handler. Both are implemented in Bash.
The runtime loads a function script from the deployment package. It uses two variables to locate the
script. LAMBDA_TASK_ROOT tells it where the package was extracted, and _HANDLER includes the name
of the script.
239
AWS Lambda Developer Guide
Create a function
Example bootstrap
#!/bin/sh
# Processing
while true
do
HEADERS="$(mktemp)"
# Get an event. The HTTP request will block until one is received
EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/
runtime/invocation/next")
After loading the script, the runtime processes events in a loop. It uses the runtime API to retrieve an
invocation event from Lambda, passes the event to the handler, and posts the response back to Lambda.
To get the request ID, the runtime saves the headers from the API response to a temporary file, and
reads the Lambda-Runtime-Aws-Request-Id header from the file.
Note
Runtimes have additional responsibilities, including error handling, and providing context
information to the handler. For details, see Building a custom runtime (p. 236).
The script defines a handler function that takes event data, logs it to stderr, and returns it.
Example function.sh
function handler () {
EVENT_DATA=$1
echo "$EVENT_DATA" 1>&2;
RESPONSE="Echoing request: '$EVENT_DATA'"
echo $RESPONSE
}
runtime-tutorial
# bootstrap
# function.sh
Make the files executable and add them to a .zip file archive.
240
AWS Lambda Developer Guide
Create a layer
Create a layer
To separate the runtime code from the function code, create a layer that only contains the runtime.
Layers let you develop your function's dependencies independently, and can reduce storage usage when
you use the same layer with multiple functions.
241
AWS Lambda Developer Guide
Update the function
"Description": "",
"CreatedDate": "2018-11-28T07:49:14.476+0000",
"Version": 1
}
This adds the runtime to the function in the /opt directory. Lambda uses this runtime, but only if you
remove it from the function's deployment package. Update the function code to only include the handler
script.
Invoke the function to verify that it works with the runtime layer.
242
AWS Lambda Developer Guide
Update the runtime
Example bootstrap
#!/bin/sh
You can add multiple statements that each grant permission to a single account, accounts in an
organization, or all accounts.
Clean up
Delete each version of the layer.
243
AWS Lambda Developer Guide
Clean up
Because the function holds a reference to version 2 of the layer, it still exists in Lambda. The function
continues to work, but functions can no longer be configured to use the deleted version. If you then
modify the list of layers on the function, you must specify a new version or omit the deleted layer.
244
AWS Lambda Developer Guide
AVX2 vectorization
To use AVX2 with your Lambda function, make sure that your function code is accessing AVX2-optimized
code. For some languages, you can install the AVX2-supported version of libraries and packages. For
other languages, you can recompile your code and dependencies with the appropriate compiler flags
set (if the compiler supports auto-vectorization). You can also compile your code with third-party
libraries that use AVX2 to optimize math operations. For example, Intel Math Kernel Library (Intel MKL),
OpenBLAS (Basic Linear Algebra Subprograms), and AMD BLAS-like Library Instantiation Software (BLIS).
Auto-vectorized languages, such as Java, automatically use AVX2 for computations.
You can create new Lambda workloads or move existing AVX2-enabled workloads to Lambda at no
additional cost.
For more information about AVX2, see Advanced Vector Extensions 2 in Wikipedia.
For the gcc or clang compiler, add -march=haswell to the command or set -mavx2 as a command
option.
To use a specific library, follow instructions in the library's documentation to compile and build the
library. For example, to build TensorFlow from source, you can follow the installation instructions on the
TensorFlow website. Make sure to use the -march=haswell compile option.
Some libraries, such as TensorFlow, provide options in their build process to specify Intel MKL
optimization. For example, with TensorFlow, use the --config=mkl option.
You can also build popular scientific Python libraries, such as SciPy and NumPy, with Intel MKL. For
instructions on building these libraries with Intel MKL, see Numpy/Scipy with Intel MKL and Intel
Compilers on the Intel website.
245
AWS Lambda Developer Guide
AVX2 support in other languages
For more information about Intel MKL and similar libraries, see Math Kernel Library in Wikipedia, the
OpenBLAS website, and the AMD BLIS repository on GitHub.
Python
Python users generally use SciPy and NumPy libraries for compute-intensive workloads. You can
compile these libraries to enable AVX2, or you can use the Intel MKL-enabled versions of the
libraries.
Node
For compute-intensive workloads, use AVX2-enabled or Intel MKL-enabled versions of the libraries
that you need.
Java
Java's JIT compiler can auto-vectorize your code to run with AVX2 instructions. For information
about detecting vectorized code, see the Code vectorization in the JVM presentation on the
OpenJDK website.
Go
The standard Go compiler doesn't currently support auto-vectorization, but you can use gccgo, the
GCC compiler for Go. Set the -mavx2 option:
Intrinsics
It's possible to use intrinsic functions in many languages to manually vectorize your code to use
AVX2. However, we don't recommend this approach. Manually writing vectorized code takes
significant effort. Also, debugging and maintaining such code is more difficult than using code that
depends on auto-vectorization.
246
AWS Lambda Developer Guide
AWS provides a set of open-source base images that you can use to build the container image for
your function code. You can also use alternative base images from other container registries. AWS
also provides an open-source runtime client that you add to your alternative base image to make it
compatible with the Lambda service.
Additionally, AWS provides a runtime interface emulator for you to test your functions locally using tools
such as the Docker CLI.
There is no additional charge for packaging and deploying functions as container images. When a
function deployed as a container image is invoked, you pay for invocation requests and execution
duration. You do incur charges related to storing your container images in Amazon ECR. For more
information, see Amazon ECR pricing.
Topics
• Creating Lambda container images (p. 248)
• Testing Lambda container images locally (p. 254)
247
AWS Lambda Developer Guide
Creating images
AWS provides a set of open-source base images (p. 203) that you can use to create your container
image. These base images include a runtime interface client (p. 203) to manage the interaction between
Lambda and your function code.
You can also use an alternative base image from another container registry. Lambda provides open-
source runtime interface clients that you add to an alternative base image to make it compatible with
Lambda.
For example applications, including a Node.js example and a Python example, see Container image
support for Lambda on the AWS Blog.
After you create a container image in the Amazon ECR container registry, you can create and run (p. 72)
the Lambda function.
Topics
• Image types (p. 248)
• Container tools (p. 248)
• Lambda requirements for container images (p. 249)
• Container image settings (p. 249)
• Create an image from an AWS base image for Lambda (p. 249)
• Create an image from an alternative base image (p. 251)
• Create an image using the AWS SAM toolkit (p. 253)
Image types
You can use an AWS provided base image or an alternative base image, such as Alpine or Debian. Lambda
supports any image that conforms to one of the following image manifest formats:
• Docker image manifest V2, schema 2 (used with Docker version 1.10 and newer)
• Open Container Initiative (OCI) Specifications (v1.0.0 and up)
Container tools
To create your container image, you can use any development tool that supports one of the following
container image manifest formats:
• Docker image manifest V2, schema 2 (used with Docker version 1.10 and newer)
• OCI Specifications (v1.0.0 and up)
For example, you can use the Docker CLI to build, test, and deploy your container images.
248
AWS Lambda Developer Guide
Lambda requirements for container images
1. The container image must implement the Lambda Runtime API (p. 205). The AWS open-source
runtime interface clients (p. 203) implement the API. You can add a runtime interface client to your
preferred base image to make it compatible with Lambda.
2. The container image must be able to run on a read-only file system. Your function code can access a
writable /tmp directory with 512 MB of storage.
3. The default Lambda user must be able to read all the files required to run your function code. Lambda
follows security best practices by defining a default Linux user with least-privileged permissions.
Verify that your application code does not rely on files that other Linux users are restricted from
running.
4. Lambda supports only Linux-based container images.
• ENTRYPOINT – Specifies the absolute path to the entry point of the application.
• CMD – Specifies parameters that you want to pass in with ENTRYPOINT.
• WORKDIR – Specifies the absolute path to the working directory.
• ENV – Specifies an environment variable for the Lambda function.
Note
Lambda ignores the values of any unsupported container image settings in the Dockerfile.
For more information about how Docker uses the container image settings, see ENTRYPOINT in the
Dockerfile reference on the Docker Docs website. For more information about using ENTRYPOINT and
CMD, see Demystifying ENTRYPOINT and CMD in Docker on the AWS Open Source Blog.
You can specify the container image settings in the Dockerfile when you build your image. You can
also override these configurations using the Lambda console or Lambda API. This allows you to deploy
multiple functions that deploy the same container image but with different runtime configurations.
Warning
When you specify ENTRYPOINT or CMD in the Dockerfile or as an override, make sure that you
enter the absolute path. Also, do not use symlinks as the entry point to the container.
Prerequisites
249
AWS Lambda Developer Guide
Create an image from an AWS base image for Lambda
The following instructions use the AWS CLI to call AWS service API operations. To install the AWS CLI,
see Installing, updating, and uninstalling the AWS CLI in the AWS Command Line Interface User Guide.
• Docker Desktop
The following instructions use Docker CLI commands to create the container image. To install the
Docker CLI, see Get Docker on the Docker Docs website.
• Your function code
1. On your local machine, create a project directory for your new function.
2. Create a directory named app in in the project directory, and then add your function handler code to
the app directory.
3. Use a text editor to create a new Dockerfile.
• LAMBDA_TASK_ROOT=/var/task
• LAMBDA_RUNTIME_DIR=/var/runtime
FROM public.ecr.aws/lambda/nodejs:14
# Alternatively, you can pull the base image from Docker Hub: amazon/aws-lambda-
nodejs:12
# Assumes your function is named "app.js", and there is a package.json file in the app
directory.
COPY app.js package.json /var/task/
# Set the CMD to your handler (could also be done as a parameter override outside of
the Dockerfile)
CMD [ "app.handler" ]
4. Build your Docker image with the docker build command. Enter a name for the image. The
following example names the image hello-world.
5. Start the Docker image with the docker run command. For this example, enter hello-world as
the image name.
6. (Optional) Test your application locally using the runtime interface emulator (p. 254). From a new
terminal window, post an event to the following endpoint using a curl command:
This command invokes the function running in the container image and returns a response.
7. Authenticate the Docker CLI to your Amazon ECR registry.
250
AWS Lambda Developer Guide
Create an image from an alternative base image
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-
stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
9. Tag your image to match your repository name, and deploy the image to Amazon ECR using the
docker push command.
Now that your container image resides in the Amazon ECR container registry, you can create and
run (p. 72) the Lambda function.
1. Choose a base image. Lambda supports all Linux distributions, such as Alpine, Debian, and Ubuntu.
2. On your local machine, create a project directory for your new function.
3. Create a directory named app in the project directory, and then add your function handler code to
the app directory.
4. Use a text editor to create a new Dockerfile with the following configuration:
251
AWS Lambda Developer Guide
Create an image from an alternative base image
libcurl4-openssl-dev
5. Build your Docker image with the docker build command. Enter a name for the image. The
following example names the image hello-world.
6. (Optional) Test your application locally using the Runtime interface emulator (p. 254).
7. Authenticate the Docker CLI to your Amazon ECR registry.
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-
stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
9. Tag your image to match your repository name, and deploy the image to Amazon ECR using the
docker push command.
Now that your container image resides in the Amazon ECR container registry, you can create and
run (p. 72) the Lambda function.
252
AWS Lambda Developer Guide
Create an image using the AWS SAM toolkit
In your AWS SAM template, you set the Runtime type to Image and provide the URI of the base image.
For more information, see Building applications in the AWS Serverless Application Model Developer Guide.
253
AWS Lambda Developer Guide
Testing images
The AWS base images for Lambda include the RIE component. If you use an alternate base image, you
can test your image without adding RIE to the image. You can also build the RIE component into your
base image. AWS provides an open-sourced RIE component on the AWS GitHub repository.
You can use the emulator to test whether your function code is compatible with the Lambda
environment. Also use the emulator to test that your Lambda function runs to completion successfully
and provides the expected output. If you build extensions and agents into your container image, you can
use the emulator to test that the extensions and agents work correctly with the Lambda Extensions API.
For examples of how to use the RIE, see Container image support for Lambda on the AWS Blog.
Topics
• Guidelines for using the RIE (p. 254)
• Environment variables (p. 254)
• Test an image with RIE included in the image (p. 255)
• Build RIE into your base image (p. 255)
• Test an image without adding RIE to the image (p. 256)
• The RIE does not emulate Lambda’s security and authentication configurations, or Lambda
orchestration.
• The emulator supports only Linux x86-64 architectures.
• The emulator does not support AWS X-Ray tracing or other Lambda integrations.
Environment variables
The runtime interface emulator supports a subset of environment variables (p. 77) for the Lambda
function in the local running image.
If your function uses security credentials, you can configure the credentials by setting the following
environment variables:
• AWS_ACCESS_KEY_ID
• AWS_SECRET_ACCESS_KEY
• AWS_SESSION_TOKEN
• AWS_REGION
To set the function timeout, configure AWS_LAMBDA_FUNCTION_TIMEOUT. Enter the maximum number
of seconds that you want to allow the function to run.
The emulator does not populate the following Lambda environment variables. However, you can set
them to match the values that you expect when the function runs in the Lambda service:
254
AWS Lambda Developer Guide
Test an image with RIE included in the image
• AWS_LAMBDA_FUNCTION_VERSION
• AWS_LAMBDA_FUNCTION_NAME
• AWS_LAMBDA_FUNCTION_MEMORY_SIZE
2. Run your container image locally using the docker run command.
This command runs the image as a container and starts up an endpoint locally at
localhost:9000/2015-03-31/functions/function/invocations.
3. From a new terminal window, post an event to the following endpoint using a curl command:
This command invokes the Lambda function running in the container image and returns a response.
1. Create a script and save it in your project directory. Set execution permissions for the script file.
The script checks for the presence of the AWS_LAMBDA_RUNTIME_API environment variable, which
indicates the presence of the runtime API. If the runtime API is present, the script runs the runtime
interface client (p. 203). Otherwise, the script runs the runtime interface emulator.
#!/bin/sh
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
exec /usr/local/bin/aws-lambda-rie /usr/local/bin/npx aws-lambda-ric $@
else
exec /usr/local/bin/npx aws-lambda-ric $@
fi
#!/bin/sh
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
255
AWS Lambda Developer Guide
Test an image without adding RIE to the image
2. Download the runtime interface emulator from GitHub into your project directory.
3. Copy the script, install the emulator package, and change ENTRYPOINT to run the new script by
adding the following lines to your Dockerfile:
1. From your project directory, run the following command to download the RIE from GitHub and
install it on your local machine.
This command invokes the function running in the container image and returns a response.
256
AWS Lambda Developer Guide
Each service that integrates with Lambda sends data to your function in JSON as an event. The structure
of the event document is different for each event type, and contains data about the resource or request
that triggered the function. Lambda runtimes convert the event into an object and pass it to your
function.
The following example shows a test event from an Application Load Balancer (p. 349) that represents a
GET request to /lambda?query=1234ABCD.
{
"requestContext": {
"elb": {
"targetGroupArn": "arn:aws:elasticloadbalancing:us-
east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a"
}
},
"httpMethod": "GET",
"path": "/lambda",
"queryStringParameters": {
"query": "1234ABCD"
},
"headers": {
"accept": "text/html,application/xhtml+xml,applicati