Create your first Lambda function
To get started with Lambda, use the Lambda console to create a function. In a few minutes, you can create and deploy a function and test it in the console.
As you carry out the tutorial, you'll learn some fundamental Lambda concepts, like how to pass arguments to your function using the Lambda event object. You'll also learn how to return log outputs from your function, and how to view your function's invocation logs in CloudWatch Logs.
To keep things simple, you create your function using either the Python or Node.js runtime. With these interpreted languages, you can edit function code directly in the console's built-in code editor. With compiled languages like Java and C#, you need to create a deployment package on your local build machine and upload it to Lambda. To learn about deploying functions to Lambda using other runtimes, see the links in the Additional resources and next steps section.
Tip
To learn how to build serverless solutions, check out the Serverless Developer Guide.
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 a 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, secure your AWS account root user, enable AWS IAM Identity Center, and 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 a user with administrative access
-
Enable IAM Identity Center.
For instructions, see Enabling AWS IAM Identity Center in the AWS IAM Identity Center User Guide.
-
In IAM Identity Center, grant administrative access to a user.
For a tutorial about using the IAM Identity Center directory as your identity source, see Configure user access with the default IAM Identity Center directory in the AWS IAM Identity Center User Guide.
Sign in as the user with administrative access
-
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.
Assign access to additional users
-
In IAM Identity Center, create a permission set that follows the best practice of applying least-privilege permissions.
For instructions, see Create a permission set in the AWS IAM Identity Center User Guide.
-
Assign users to a group, and then assign single sign-on access to the group.
For instructions, see Add groups in the AWS IAM Identity Center User Guide.
Create a Lambda function with the console
In this example, your function takes a JSON object containing two integer values labeled "length"
and "width"
.
The function multiplies these values to calculate an area and returns this as a JSON string.
Your function also prints the calculated area, along with the name of its CloudWatch log group. Later in the tutorial, you’ll learn to use CloudWatch Logs to view records of your functions’ invocation.
To create your function, you first use the console to create a basic Hello world function. In the following step, you then add your own function code.
To create a Hello world Lambda function with the console
Open the Functions page
of the Lambda console. -
Choose Create function.
-
Select Author from scratch.
-
In the Basic information pane, for Function name enter
.myLambdaFunction
-
For Runtime, choose either Node.js 20.x or Python 3.12
-
Leave architecture set to x86_64 and choose Create function.
Lambda creates a function that returns the message Hello from Lambda!
Lambda also creates an execution role
for your function. An execution role is an AWS Identity and Access Management (IAM) role that grants a Lambda function
permission to access AWS services and resources. For your function, the role that Lambda creates grants basic permissions to write to CloudWatch Logs.
You now use the console's built-in code editor to replace the Hello world code that Lambda created with your own function code.
Invoke the Lambda function using the console
To invoke your function using the Lambda console, you first create a test event to send to your function. The event is a JSON formatted
document containing two key-value pairs with the keys "length"
and "width"
.
To create the test event
-
In the Code source pane, choose Test.
-
Select Create new event.
-
For Event name enter
myTestEvent
. -
In the Event JSON panel, replace the default values by pasting in the following:
{ "length": 6, "width": 7 }
-
Choose Save.
You now test your function and use the Lambda console and CloudWatch Logs to view records of your function’s invocation.
To test your function and view invocation records in the console
-
In the Code source pane, choose Test. When your function finishes running, you’ll see the response and function logs displayed in the Execution results tab. You should see results similar to the following.
In this example, you invoked your code using the console's test feature. This means that you can view your function's execution results directly in the console. When your function is invoked outside the console, you need to use CloudWatch Logs.
To view your function's invocation records in CloudWatch Logs
-
Open the Log groups
page of the CloudWatch console. -
Choose the log group for your function (
/aws/lambda/myLambdaFunction
). This is the log group name that your function printed to the console. -
In the Log streams tab, choose the log stream for your function's invocation.
You should see output similar to the following:
Clean up
When you're finished 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,
myLambdaFunction-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).
Additional resources and next steps
Now you’ve created and tested a simple Lambda function using the console, take these next steps:
-
Learn to add dependencies to your code and deploy it using a .zip deployment package. Choose from the following links for the languages you're interested in.
-
Carry out the tutorial Using an Amazon S3 trigger to invoke a Lambda function to learn how to configure a Lambda function to be invoked by another AWS service.
-
Choose one of the following tutorials for a more complex example of using Lambda with other AWS services.
-
Using Lambda with API Gateway: Create an Amazon API Gateway REST API that invokes a Lambda function.
-
Using a Lambda function to access an Amazon RDS database: Use a Lambda function to write data to an Amazon Relational Database Service (Amazon RDS) database through RDS Proxy.
-
Using an Amazon S3 trigger to create thumbnail images: Use a Lambda function to create a thumbnail every time an image file is uploaded to an Amazon S3 bucket.
-