In this tutorial, you will learn the basics of running code on AWS Lambda without provisioning or managing servers. We will walk through how to create a Hello World Lambda function using the AWS Lambda console. We will then show you how to manually invoke the Lambda function using sample event data and review your output metrics.

Everything done in this tutorial is free tier eligible.

Running Serverless Code with AWS Requires an Account

Create a Free Account in Minutes

The AWS Free Tier includes 1 million free requests and up to 3.2 million seconds of compute time per month with AWS Lambda.

View AWS Free Tier Details »

 

When you click here, the AWS Management Console will open in a new browser window, so you can keep this step-by-step guide open.  Find Lambda under Compute and click to open the AWS Lambda Console.

P1

(click to zoom)

P1

Blueprints provide example code to do some minimal processing. Most blueprints process events from specific event sources, such as Amazon S3, DynamoDB, or a custom application.

a.  In the AWS Lambda console, select Create a Function.

Note:
The console shows this 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.

P2

(click to zoom)

P2

b.  Select Blueprints.

c.  In the Filter box, type in hello-world-python and select the hello-world-python blueprint.

d.  Then click Configure.

P3

(click to zoom)

P3

A Lambda function consists of code you provide, associated dependencies, and configuration. The configuration information you provide includes the compute resources you want to allocate (for example, memory), execution timeout, and an IAM role that AWS Lambda can assume to execute your Lambda function on your behalf.


a.  You will now enter Basic Information about your Lambda function.

Basic Information:
  • Name: You can name your Lambda function here. For this tutorial, enter hello-world-python.
  • Role: You will 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. Select Create new role from template(s).
  • Role name: type lambda_basic_execution
Lambda Function Code:
  • In this section, you can review the example code authored in Python.

b.  Go to the bottom of the page and select Create Function.

P4

(click to zoom)

P4

c.  Runtime: Currently, you can author your Lambda function code in Java, Node.js, C#, Go or Python. For this tutorial, leave this on Python 2.7 as the runtime.

d.  Handler: You can specify a handler (a method/function in your code) where AWS Lambda can begin executing your code. AWS Lambda provides event data as input to this handler, which processes the event.

In this example, Lambda identifies this from the code sample and this should be pre-populated with lambda_function.lambda_handler.

P5

(click to zoom)

P5

e.  Scroll down to configure your memory, timeout, and VPC settings.  For this tutorial, leave the default Lambda function configuration values.

Picture1

(click to zoom)

Picture1

The console shows the hello-world-python Lambda function - you can now test the function, verify results, and review the logs.


a.  Select Configure Test Event from the drop-down menu called "Select a test event...".

P7

(click to zoom)

P7

b.  The editor pops up to enter an event to test your function.  

  • Choose Hello World from the Sample event template list from the Input test event page. 
  • Type in an event name like HelloWorldEvent.
  • You can change the values in the sample JSON, but don’t change the event structure. For this tutorial, replace value1 with hello, world!.

Select Create.

P8

(click to zoom)

P8

c.  Select Test.

P9

(click to zoom)

P9

d.  Upon successful execution, view the results in the console:

  • The Execution results section verifies that the execution succeeded.
  • The Summary section shows the key information reported in the Log output.
  • The Log output section will show the logs generated by the Lambda function execution. 
P91

(click to zoom)

P91

AWS Lambda automatically monitors Lambda functions and reports metrics through Amazon CloudWatch. To help you monitor your code as it executes, Lambda automatically tracks the number of requests, the latency per request, and the number of requests resulting in an error and publishes the associated metrics. 


a.  Invoke the Lambda function a few more times by repeatedly clicking the Test button.  This will generate the metrics that can be viewed in the next step.

b.  Select Monitoring to view the results.

P92

(click to zoom)

P92

c.  Scroll down to view the metrics for your Lambda function.  Lambda metrics are reported through Amazon CloudWatch. You can leverage these metrics to set custom alarms. For more information about CloudWatch, see the Amazon CloudWatch Developer Guide.

The Monitoring tab will show six CloudWatch metrics: Invocation count, Invocation duration, Invocation errors, Throttled invocations, Iterator age, and DLQ errors.

With AWS Lambda, you pay for what you use. After you hit your AWS Lambda free tier limit, you are charged based on the number of requests for your functions (invocation count) and the time your code executes (invocation duration).  For more information, see AWS Lambda Pricing.

P93

(click to zoom)

P93

While you will not get charged for keeping your Lambda function, you can easily delete it from the AWS Lambda console.  


a.  Select the Actions button and click Delete Function.

P94

(click to zoom)

P94

b.  You will be asked to confirm your termination - select Delete.

P95

(click to zoom)

P95

You have created your first AWS Lambda function. This is your first step in learning how to run applications without needing to provision or manage servers. Lambda automatically scales your applications by running your code in response to each trigger, scaling precisely with the size of your workloads.

Now that you have learned to create a Lambda function, you can progress to the next tutorial where you will learn how to trigger a Lambda function to process data that is uploaded into an Amazon S3 bucket. You’ll use a Lambda function to create, in real-time, a thumbnail of images that are uploaded to a S3 bucket.

Use AWS Lambda with Amazon S3 »

Yes
No