AWS Lambda
Developer Guide

Create a Lambda Function with the Console

In this Getting Started exercise you first create a Lambda function using the AWS Lambda console. Next, you manually invoke the Lambda function using sample event data. AWS Lambda executes the Lambda function and returns results. You then verify execution results, including the logs that your Lambda function created and various CloudWatch metrics.

As you follow the steps, you will also familiarize yourself with the AWS Lambda console including:

  • Explore the blueprints. Each blueprint provides sample code and sample configurations that enable you to create Lambda functions with just a few clicks.

  • View and update configuration information of your Lambda function.

  • Invoke a Lambda function manually and explore results in the Execution results section.

  • Monitor CloudWatch metrics in the console.

Follow the steps in this section to create a simple Lambda function.

To create a Lambda function

  1. Sign in to the AWS Management Console and open the AWS Lambda console.

  2. Note that AWS Lambda offers a simple Hello World function upon introduction under the How it works label and includes a Run option, allowing you to invoke the function as a general introduction. This tutorial introduces additional options you have to create, test and update your Lambda functions, as well as other features provided by the Lambda console and provides links to each, inviting you to explore each one in depth.

    Choose Create a function under the Get Started section to proceed.

    Note

    The console shows the Get Started page only if you do not have any Lambda functions created. If you have created functions already, you will see the Lambda > Functions page. On the list page, choose Create a function to go to the Create function page.

  3. On the Create function page, choose Author from scratch.

  4. In Author from scratch, do the following:

    • In Name*, specify your Lambda function name.

    • In Runtime*, choose Python 3.7.

    • In Role*, choose Create new role from template(s):

    • In Role name*, enter a name for your role.

    • Leave the Policy templates field blank. For the purposes of this introduction, your Lambda function will have the necessary execution permissions.

      Note

      For an in-depth look at AWS Lambda's security polices, see AWS Lambda Permissions.

    • Choose Create Function.

  5. In the Add triggers panel, you can optionally choose a service that automatically triggers your Lambda function by choosing one of the service options listed.

    Depending on which service you select, you are prompted to provide relevant information for that service. For example, if you select DynamoDB, you need to provide the following:

    • The name of the DynamoDB table

    • Batch size

    • Starting position

    For this example, do not configure a trigger.

    • In Function code note that code is provided. It returns a simple "Hello from Lambda" greeting.

    • Handler shows lambda_function.lambda_handler value. It is the filename.handler-function. The console saves the sample code in the lambda_function.py file and in the code lambda_handler is the function name that receives the event as a parameter when the Lambda function is invoked. For more information, see AWS Lambda Function Handler in Python.

    • Note the embedded IDE (Integrated Development Environment). To learn more, see Creating Functions Using the AWS Lambda Console Editor.

  6. Other configuration options on this page include:

Invoke the Lambda Function and Verify Results, Logs, and Metrics

Follow the steps to invoke your Lambda function using the sample event data provided in the console.

  1. On the yourfunction page, choose Test.

  2. In the Configure test event page, choose Create new test event and in Event template, leave the default Hello World option. Enter an Event name and note the following sample event template:

    { "key3": "value3", "key2": "value2", "key1": "value1" }

    You can change key and values in the sample JSON, but don't change the event structure. If you do change any keys and values, you must update the sample code accordingly.

    Note

    If you choose to delete the test event, go to the Configure test event page and then choose Delete.

  3. Choose Create and then choose Test. Each user can create up to 10 test events per function. Those test events are not available to other users.

  4. AWS Lambda executes your function on your behalf. The handler in your Lambda function receives and then processes the sample event.

  5. Upon successful execution, view results in the console.

    • The Execution result section shows the execution status as succeeded and also shows the function execution results, returned by the return statement.

    • 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 AWS Lambda generates for each execution. These are the logs written to CloudWatch by the Lambda function. The AWS Lambda console shows these logs for your convenience.

    Note that the Click here link shows logs in the CloudWatch console. The function then adds logs to Amazon CloudWatch in the log group that corresponds to the Lambda function.

  6. Run the Lambda function a few times to gather some metrics that you can view in the next step.

  7. Choose Monitoring. This page shows graphs for the metrics that Lambda sends to CloudWatch.

    For more information on these graphs, see Accessing Amazon CloudWatch Metrics for AWS Lambda.