Menu
AWS Lambda
Developer Guide

Step 2.1: Create a Hello World Lambda Function

Follow the steps in this section to create a Hello World Lambda function. In this step, you will do the following:

  • Select a blueprint – For this exercise, you use the hello-world-python blueprint. It provides sample code authored in Python. The language used for the Lambda function does not matter for this exercise. Later you can create your own Lambda functions in any of the supported languages.

    Blueprints provide example code to do some minimal processing. Most blueprints process events from specific event sources, such as Amazon S3, DynamoDB, or custom application. For example, if you select an s3-get-object blueprint, it provides sample code that processes an object-created event published by Amazon S3 that Lambda receives as parameter.

  • Configure function – Because you select a blueprint for this exercise, the console will have some of the configuration information prepopulated. For example, it preconfigures either Python 3.6 or Python 2.7 as the runtime, provides example code, identifies the handler in the code sample, and other configuration information such as memory and timeout. For more information about configuring functions, see Lambda Functions. For more information about the function configuration parameters, see CreateFunction.

    You will also create an IAM role (referred as the execution role) with the necessary permissions that AWS Lambda can assume to invoke your Lambda function on your behalf.

To create a Hello World Lambda function

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

  2. Choose Get Started Now.

    Note

    The console shows the Get Started Now 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 Lambda function to go to the Lambda > New function page.

  3. On the Select blueprint page, first explore the available blueprints. Then, select a specific blueprint for this Getting Started exercise.

    1. Review the blueprints. You can also use the Filter to search for specific blueprints. For example:

      • Enter S3 in Filter to get only the list of blueprints available to process Amazon S3 events.

      • Enter dynamodb in Filter to get a list of available blueprints to process Amazon DynamoDB events.

    2. For this Getting Started exercise, enter hello-world-python in Filter, and then choose the hello-world-python blueprint.

  4. In Basic information, do the following:

    • In Name*, specify your Lambda function name.

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

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

    • In Policy templates, Lambda provides a list of optional templates that, should you select one, automatically creates the role with the requisite permissions attached to that policy. For a list of the Policy templates, see Policy Templates. For the purpose of this tutorial, you can leave this field blank because your Lambda function already has the basic execution permission it needs.

    • In the Lambda Function Code section, do the following:

      • Review the sample code. Note that the console saves this code as lambda_handler.py. The console then zips the file, and uploads it to AWS Lambda creating your Lambda function.

      • The sample code processes incoming events of the following form:

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

        After creating the Lambda function, you invoke it using sample events of this form in the next section.

    • Choose Create function.

  5. Under Configuration in the Lambda function code section, note the following

    • Runtime is Python 2.7

    • Code authored in Python is provided. It reads incoming event data and logs some of the information to CloudWatch.

    • 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 Lambda Function Handler (Python).

  6. Choose the Triggers tab. Under the Triggers page, you can optionally choose a service that automatically triggers your Lambda function by choosing the Add trigger button and then choosing the gray box with ellipses (...) to display a list of available services.

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

    2. For this example, do not configure a trigger and choose the Monitoring tab.

    .

  7. The Monitoring page will provide immediate CloudWatch metrics for your Lambda function invocations, as well as links to other helpful guides, including Troubleshooting Lambda-Based Applications.

  8. Choose the Configuration tab and then note the other tabs in the console:

Next Step

Step 2.2: Invoke the Lambda Function Manually and Verify Results, Logs, and Metrics