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. On the Configure triggers page, you can optionally choose a service that automatically triggers your Lambda function by 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 Getting Started exercise, do not configure a trigger and choose Next.

  5. On the Configure function page, do the following:

    1. Review the preconfigured Lambda function configuration information, including:

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

    2. Enter the function name hello-world-python in Name.

    3. In the Lambda Function Code section, do the following:

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

    4. In the Lambda function handler and role section, do the following:

      1. Note the Handler* value. It is of the form python-file-name.handler-function.

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

      3. In Role name, type a name for the role.

      4. In Role 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.

      Note

      Optionally, you could select Choose an existing role if you already have a role created with specific permissions beyond basic execution. You can also select Create a custom role. When you choose this option, a window appears where you can edit the permissions policy inline.

    5. In the Advanced settings section, leave the default Lambda function configuration values.

      The memory and timeout values are sufficient for the Lambda function you are creating. These configurations influence the performance of your code. For more information, see Lambda Functions.

    6. Choose Next.

    7. Choose Create Function to create a Lambda function.

      The console saves the code into a file and then zips the file, which is the deployment package. The console then uploads the deployment package to AWS Lambda creating your Lambda function. The console shows the hello-world-python Lambda function, you can now perform various action including test the function:

      Note the tabs in the console:

      • Code – Shows the Lambda function code.

      • Configuration – shows current function configuration and you can change the configuration as needed. After you change any configuration settings, you choose Save to save the updated configuration.

      • Triggers – Shows any triggers you configured for this function (does not apply to this Getting Started exercise).

      • Monitoring – Provides various CloudWatch metrics for your Lambda function. In the next section, you invoke your hello-world-python Lambda function and review these metrics.

Next Step

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