Getting started with Lambda - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Getting started with Lambda

To get started with Lambda, use the Lambda console to create a function. In a few minutes, you can create and deploy a function, invoke it, and then view logs and metrics.

Tip

To learn how to build serverless solutions, check out the Serverless Developer Guide.

Prerequisites

If you do not have an Amazon Web Services account, use the following procedure to create one.

To sign up for Amazon Web Services
  1. Open http://www.amazonaws.cn/ and choose Sign Up.

  2. Follow the on-screen instructions.

Amazon sends you a confirmation email after the sign-up process is complete. At any time, you can view your current account activity and manage your account by going to http://www.amazonaws.cn/ and choosing My Account.

After you sign up for an Amazon Web Services account, safeguard your administrative user by turning on multi-factor authentication (MFA). For instructions, see Enable a virtual MFA device for an IAM user (console) in the IAM User Guide.

To give other users access to your Amazon Web Services account resources, create IAM users. To secure your IAM users, turn on MFA and only give the IAM users the permissions needed to perform their tasks.

For more information about creating and securing IAM users, see the following topics in the IAM User Guide:

Create a Lambda function with the console

In this getting started exercise, create a Lambda function using a blueprint. A blueprint provides sample code to do some minimal processing. Most blueprints process events from specific event sources, such as Amazon Simple Storage Service (Amazon S3), Amazon DynamoDB, or a custom application.

To create a Lambda function with the console
  1. Open the Functions page of the Lambda console.

  2. Choose Create function.

  3. Select Use a blueprint.

  4. Open the Select blueprint dropdown list and search for Hello world function. Select nodejs or python.

  5. Enter a Function name.

  6. For Execution role, choose Create a new role with basic Lambda permissions. Lambda creates an execution role that grants the function permission to upload logs to Amazon CloudWatch. The Lambda function assumes the execution role when you invoke your function, and uses the execution role to create credentials for the Amazon SDK and to read data from event sources.

Invoke the function

To invoke the function from the console, create a test event.

  1. Choose the Test tab.

  2. For Test event action, choose Create new event.

  3. For Event name, enter a name for the test event.

  4. For Event sharing settings, choose Private.

  5. For Template, leave the default hello-world option.

  6. In the Event JSON, replace value1 with hello, world! Don't change key1 or the event structure. Example:

    { "key1": "hello, world!", "key2": "value2", "key3": "value3" }
  7. Choose Save, and then choose Test. Lambda invokes the function on your behalf. The function handler receives and then processes the sample event.

  8. Review the Execution result. Under Details, you should see the value that you entered in step 6: "hello, world!". The execution result also includes the following information:

    • The Summary section shows the key information from the REPORT line in the invocation log.

    • The Log output section shows the complete invocation log. Lambda writes all invocation logs to Amazon CloudWatch.

  9. Choose Test to invoke the function a few more times and gather additional metrics that you can view in the next step.

  10. Choose the Monitor tab. This page shows graphs of the metrics that Lambda sends to CloudWatch.

    Note

    It mights take 5 to 10 minutes for logs to show up after a function invocation.

    For more information on these graphs, see Monitoring functions on the Lambda console.

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.

To delete a Lambda function
  1. Open the Functions page of the Lambda console.

  2. Choose a function.

  3. Choose Actions, Delete.

  4. In the Delete function dialog box, enter delete, and then choose Delete.

To delete the log group
  1. Open the Log groups page of the CloudWatch console.

  2. Select the function's log group (/aws/lambda/my-function).

  3. Choose Actions, Delete log group(s).

  4. In the Delete log group(s) dialog box, choose Delete.

To delete the execution role
  1. Open the Roles page of the Amazon Identity and Access Management (IAM) console.

  2. Select the function's execution role (for example, my-function-role-31exxmpl).

  3. Choose Delete.

  4. In the Delete role dialog box, enter the role name and then choose Delete.

You can automate the creation and cleanup of functions, log groups, and roles with Amazon CloudFormation and the Amazon Command Line Interface (Amazon CLI). For fully functional sample applications, see Lambda sample applications.

Additional resources

After creating your first Lambda function, try a tutorial:

To learn more about serverless application development, see the following:

Accessing Lambda

You can create, invoke, and manage Lambda functions using any of the following interfaces:

  • Amazon Management Console – Provides a web interface for you to access your functions. For more information, see Lambda console.

  • Amazon Command Line Interface (Amazon CLI) – Provides commands for a broad set of Amazon services, including Lambda, and is supported on Windows, macOS, and Linux. For more information, see Using Lambda with the Amazon CLI.

  • Amazon 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 Amazon SDKs.

  • Amazon CloudFormation – Enables you to create templates that define your Lambda applications. For more information, see Amazon Lambda applications. Amazon CloudFormation also supports the Amazon Cloud Development Kit (Amazon CDK).

  • Amazon Serverless Application Model (Amazon SAM) – Provides templates and a CLI to configure and manage Amazon serverless applications. For more information, see Getting started with Amazon SAM.

Authoring and deploying functions

The following table lists the languages that Lambda supports and the tools and options that you can use with them. The available tools and options depend on:

  • 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.

Language Tools and options for authoring code

Node.js

Java

C#

Python

Ruby

  • Lambda console

  • Your own authoring environment

Go

  • Your own authoring environment

PowerShell

You deploy your function code to Lambda using a deployment package. Lambda supports two types of deployment packages: