Menu
AWS Lambda
Developer Guide

Building Lambda Functions

You upload your application code in the form of one or more Lambda functions to AWS Lambda, a compute service, and the service can run the code on your behalf. AWS Lambda takes care of provisioning and managing the servers to run the code upon invocation.

Typically, the lifecycle for an AWS Lambda-based application includes authoring code, deploying code to AWS Lambda, and then monitoring and troubleshooting. The following are general questions that come up in each of these lifecycle phases:

  • Authoring code for your Lambda function – What languages are supported? Is there a programming model that I need to follow? How do I package my code and dependencies for uploading to AWS Lambda? What tools are available?

     

  • Uploading code and creating Lambda functions – How do I upload my code package to AWS Lambda? How do I tell AWS Lambda where to begin executing my code? How do I specify compute requirements like memory and timeout?

     

  • Monitoring and troubleshooting – For my Lambda function that is in production, what metrics are available? If there are any failures, how do I get logs or troubleshoot issues?

The following sections provide introductory information and the Example section at the end provides working examples for you to explore.

Authoring Code for Your Lambda Function

You can author your Lambda function code in the languages that are supported by AWS Lambda. For a list of supported languages, see Lambda Execution Environment and Available Libraries. There are tools for authoring code, such as the AWS Lambda console, Eclipse IDE, and Visual Studio IDE. But the available tools and options depend on the following:

  • Language you choose to write your Lambda function code.

  • Libraries that you use in your code. AWS Lambda runtime provides some of the libraries and you must upload any additional libraries that you use.

The following table lists languages, and the available tools and options that you can use.

Language Tools and Options for Authoring Code More Info

Node.js

You can use the console if the languages you choose do not require compilation, the code is saved in a single file, and it does not depend on any libraries.

Java

The AWS Toolkit also creates the deployment package, which is explained in Deploying Code and Creating a Lambda Function.

C#

The AWS Toolkit also creates the deployment package, which is explained in Deploying Code and Creating a Lambda Function.

Python

  • AWS Lambda console

  • Your own authoring environment

You can use the console if the languages you choose do not require compilation, the code is saved in a single file, and it does not depend on any libraries.

In addition, regardless of the language you choose, there is a pattern to writing Lambda function code. For example, how you write the handler method of your Lambda function (that is, the method that AWS Lambda first calls when it begins executing the code), how you pass events to the handler, what statements you can use in your code to generate logs in CloudWatch Logs, how to interact with AWS Lambda runtime and obtain information such as the time remaining before timeout, and how to handle exceptions. The Programming Model section provides information for each of the supported languages.

Note

After you familiarize yourself with AWS Lambda, see the Use Cases, which provide step-by-step instructions to help you explore the end-to-end experience.

Deploying Code and Creating a Lambda Function

To create a Lambda function, you first package your code and dependencies in a deployment package. Then, you upload the deployment package to AWS Lambda to create your Lambda function.

Creating a Deployment Package – Organizing Code and Dependencies

You must first organize your code and dependencies in certain ways and create a deployment package. Instructions to create a deployment package vary depending on the language you choose to author the code. For example, you can use build plugins such as Jenkins (for Node.js and Python), and Maven (for Java) to create the deployment packages. For more information, see Creating a Deployment Package.

When you create Lambda functions using the console, the console creates the deployment package for you, and then uploads it to create your Lambda function.

Uploading a Deployment Package – Creating a Lambda Function

AWS Lambda provides the CreateFunction operation, which is what you use to create a Lambda function. You can use the AWS Lambda console, AWS CLI, and AWS SDKs to create a Lambda function. Internally, all of these interfaces call the CreateFunction operation.

In addition to providing your deployment package, you can provide configuration information when you create your Lambda function including the compute requirements of your Lambda function, the name of the handler method in your Lambda function, and the runtime, which depends on the language you chose to author your code. For more information, see Lambda Functions.

Testing a Lambda Function

If your Lambda function is designed to process events of a specific type, you can use sample event data to test your Lambda function using one of the following methods:

The console provides sample event data. The same data is also provided in the Sample Events Published by Event Sources topic, which you can use in the AWS CLI to invoke your Lambda function.

Monitoring and Troubleshooting

After your Lambda function is in production, AWS Lambda automatically monitors functions on your behalf, reporting metrics through Amazon CloudWatch. For more information, see Accessing Amazon CloudWatch Metrics for AWS Lambda.

To help you troubleshoot failures in a function, Lambda logs all requests handled by your function and also automatically stores logs that your code generates in Amazon CloudWatch Logs. For more information, see Accessing Amazon CloudWatch Logs for AWS Lambda.

AWS Lambda-Based Application Examples

This guide provides several examples with step-by-step instructions. If you are new to AWS Lambda, we recommend you try the following exercises:

  • Getting Started – The Getting Started exercise provides a console-based experience. The sample code is authored in Python. You can edit the code in the console, using the Code Editor and upload it to AWS Lambda, and test it using sample event data provided in the console.

     

  • Use Cases – If you cannot author your code using the console, you must create your own deployment packages and use the AWS CLI (or SDKs) to create your Lambda function. For more information, see Authoring Code for Your Lambda Function. Most examples in the Uses Cases section use the AWS CLI. If you are new to AWS Lambda, we recommend that you try one of these exercises.

The following topics provide additional information.

Programming Model

Creating a Deployment Package

AWS Lambda Function Versioning and Aliases

Using Amazon CloudWatch