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
-
Sign in to the AWS Management Console and open the AWS Lambda console.
-
Note that AWS Lambda offers a simple
Hello Worldfunction 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.
-
On the Create function page, choose
Author from scratch. -
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.
-
-
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_handlervalue. It is thefilename.handler-function. The console saves the sample code in thelambda_function.pyfile and in the codelambda_handleris 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.
-
-
Other configuration options on this page include:
-
Environment variables – for Lambda functions enable you to dynamically pass settings to your function code and libraries, without making changes to your code. For more information, see AWS Lambda Environment Variables.
-
Tags – are key-value pairs that you attach to AWS resources to better organize them. For more information, see Tagging Lambda Functions.
-
Execution role – which allows you to administer security on your function, using defined roles and policies or creating new ones. For more information, see AWS Lambda Permissions.
-
Basic settings – allows you to dictate the memory allocation and timeout limit for your Lambda function. For more information, see AWS Lambda Limits.
-
Network – allows you to select a VPC your function will access. For more information, see Configuring a Lambda Function to Access Resources in an Amazon VPC.
-
Debugging and error handling – allows you to select a AWS Lambda Function Dead Letter Queues resource to analyze failed function invocation retries. It also allows you to enable active tracing. For more information, see Using AWS X-Ray.
-
Concurrency – allows you to allocate a specific limit of concurrent executions allowed for this function. For more information, see Function Level Concurrent Execution Limit.
-
Auditing and compliance – logs function invocations for operational and risk auditing, governance and compliance. For more information, see Using AWS Lambda with AWS CloudTrail.
-
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.
-
On the
yourfunctionpage, choose Test. -
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.
-
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.
-
AWS Lambda executes your function on your behalf. The
handlerin your Lambda function receives and then processes the sample event. -
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
returnstatement. -
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.
-
-
Run the Lambda function a few times to gather some metrics that you can view in the next step.
-
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.


