Getting started with Lambda
To get started with Lambda, use the Lambda console to create a function. In a few minutes, you can create a function, invoke it, and then view logs and metrics.
Prerequisites
If you do not have an AWS account, complete the following steps to create one.
To sign up for an AWS account
Follow the online instructions.
Part of the sign-up procedure involves receiving a phone call and entering a verification code on the phone keypad.
When you sign up for an AWS account, an AWS account root user is created. The root user has access to all AWS services and resources in the account. As a security best practice, assign administrative access to an administrative user, and use only the root user to perform tasks that require root user access.
AWS sends you a confirmation email after the sign-up process is
complete. At any time, you can view your current account activity and manage your account by
going to https://aws.amazon.com/
After you sign up for an AWS account, create an administrative user so that you don't use the root user for everyday tasks.
Secure your AWS account root user
-
Sign in to the AWS Management Console
as the account owner by choosing Root user and entering your AWS account email address. On the next page, enter your password. For help signing in by using root user, see Signing in as the root user in the AWS Sign-In User Guide.
-
Turn on multi-factor authentication (MFA) for your root user.
For instructions, see Enable a virtual MFA device for your AWS account root user (console) in the IAM User Guide.
Create an administrative user
-
For your daily administrative tasks, grant administrative access to an administrative user in AWS IAM Identity Center (successor to AWS Single Sign-On).
For instructions, see Getting started in the AWS IAM Identity Center (successor to AWS Single Sign-On) User Guide.
Sign in as the administrative user
-
To sign in with your IAM Identity Center user, use the sign-in URL that was sent to your email address when you created the IAM Identity Center user.
For help signing in using an IAM Identity Center user, see Signing in to the AWS access portal in the AWS Sign-In User Guide.
Create a Lambda function with the console
In this getting started exercise, create a Lambda function using a blueprint. A blueprint provides sample code to do some minimal processing. Most blueprints process events from specific event sources, such as Amazon Simple Storage Service (Amazon S3), Amazon DynamoDB, or a custom application.
To create a Lambda function with the console
Open the Functions page
of the Lambda console. -
Choose Create function.
-
Select Use a blueprint.
-
In the Filter blueprints box, search hello-world-python and select the hello-world-python blueprint.
-
Choose Configure.
-
Under Basic information, do the following:
-
Enter a Function name.
-
For Execution role, choose Create a new role with basic Lambda permissions. Lambda creates an execution role that grants the function permission to upload logs to Amazon CloudWatch. The Lambda function assumes the execution role when you invoke your function, and uses the execution role to create credentials for the AWS SDK and to read data from event sources.
-
Invoke the function
To invoke the function from the console, create a test event.
-
Choose the Test tab.
-
For Test event action, choose Create new event.
-
For Event name, enter a name for the test event.
-
For Event sharing settings, choose Private.
-
For Template, leave the default hello-world option.
-
In the Event JSON, replace
value1
withhello, world!
Don't changekey1
or the event structure. Example:{ "key1": "
hello, world!
", "key2": "value2", "key3": "value3" } -
Choose Save, and then choose Test. Lambda invokes the function on your behalf. The function handler receives and then processes the sample event.
-
Review the Execution result. Under Details, you should see the value that you entered in step 6:
"hello, world!"
. The execution result also includes the following information:-
The Summary section shows the key information from the
REPORT
line in the invocation log. -
The Log output section shows the complete invocation log. Lambda writes all invocation logs to Amazon CloudWatch.
-
-
Choose Test to invoke the function a few more times and gather additional metrics that you can view in the next step.
-
Choose the Monitor tab. This page shows graphs of the metrics that Lambda sends to CloudWatch.
Note It mights take 5 to 10 minutes for logs to show up after a function invocation.
For more information on these graphs, see Monitoring functions on the Lambda console.
Clean up
If you are done working with the example function, delete it. You can also delete the log group that stores the function's logs, and the execution role that the console created.
To delete a Lambda function
Open the Functions page
of the Lambda console. -
Choose a function.
-
Choose Actions, Delete.
-
In the Delete function dialog box, enter delete, and then choose Delete.
To delete the log group
-
Open the Log groups page
of the CloudWatch console. -
Select the function's log group (
/aws/lambda/my-function
). -
Choose Actions, Delete log group(s).
-
In the Delete log group(s) dialog box, choose Delete.
To delete the execution role
-
Open the Roles page
of the AWS Identity and Access Management (IAM) console. -
Select the function's execution role (for example,
my-function-role-
).31exxmpl
-
Choose Delete.
-
In the Delete role dialog box, enter the role name and then choose Delete.
You can automate the creation and cleanup of functions, log groups, and roles with AWS CloudFormation and the AWS Command Line Interface (AWS CLI). For fully functional sample applications, see Lambda sample applications.
Additional resources
After creating your first Lambda function, try a tutorial:
-
Tutorial: Using an Amazon S3 trigger to invoke a Lambda function: Use the Lambda console to create a trigger that invokes your function every time that you add an object to an Amazon S3 bucket.
-
Tutorial: Using Lambda with API Gateway: Create an Amazon API Gateway REST API that invokes a Lambda function.
-
Tutorial: Using AWS Lambda with scheduled events: Configure a Lambda function to run every minute. Configure Amazon Simple Notification Service (Amazon SNS) to email you if the function returns an error.
To learn more about serverless application development with Lambda, see the following:
-
The AWS Online Tech Talks
YouTube channel includes videos about Lambda-related topics. For an overview of serverless applications and Lambda, see the Introduction to AWS Lambda & Serverless Applications video.
Accessing Lambda
You can create, invoke, and manage Lambda functions using any of the following interfaces:
-
AWS Management Console – Provides a web interface for you to access your functions. For more information, see Lambda console.
-
AWS Command Line Interface (AWS CLI) – Provides commands for a broad set of AWS services, including Lambda, and is supported on Windows, macOS, and Linux. For more information, see Using Lambda with the AWS CLI.
-
AWS SDKs – Provide language-specific APIs and manage many of the connection details, such as signature calculation, request retry handling, and error handling. For more information, see AWS SDKs
. -
AWS CloudFormation – Enables you to create templates that define your Lambda applications. For more information, see AWS Lambda applications. AWS CloudFormation also supports the AWS Cloud Development Kit (AWS CDK)
. -
AWS Serverless Application Model (AWS SAM) – Provides templates and a CLI to configure and manage AWS serverless applications. For more information, see Getting started with AWS SAM.
Authoring and deploying functions
The following table lists the languages that Lambda supports and the tools and options that you can use with them. The available tools and options depend on:
-
The language that you use to write your Lambda function code.
-
The libraries that you use in your code. The Lambda runtimes provide some of the libraries, and you must upload any additional libraries that you use.
Language | Tools and options for authoring code |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
You deploy your function code to Lambda using a deployment package. Lambda supports two types of deployment packages:
A .zip file archive that contains your function code and its dependencies.
A container image that is compatible with the Open Container Initiative (OCI)
specification.