Getting started with Lambda
To get started with Lambda, use the Lambda console to create a function. In a few minutes, you can create and deploy a function, invoke it, and then view logs and metrics.
Tip
To learn how to build serverless solutions, check out the Serverless Developer Guide.
Prerequisites
If you do not have an Amazon Web Services account, use the following procedure to create one.
To sign up for Amazon Web Services
Open http://www.amazonaws.cn/
and choose Sign Up. Follow the on-screen instructions.
Amazon 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 http://www.amazonaws.cn/
After you sign up for an Amazon Web Services account, safeguard your administrative user by turning on multi-factor authentication (MFA). For instructions, see Enable a virtual MFA device for an IAM user (console) in the IAM User Guide.
To give other users access to your Amazon Web Services account resources, create IAM users. To secure your IAM users, turn on MFA and only give the IAM users the permissions needed to perform their tasks.
For more information about creating and securing IAM users, see the following topics in the IAM 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.
-
Open the Select blueprint dropdown list and search for Hello world function. Select
nodejs
orpython
. -
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 Amazon 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 Amazon 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 Amazon CloudFormation and the Amazon Command Line Interface (Amazon 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 Amazon 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, see the following:
-
Serverless Developer Guide for directed learning paths to build serverless solutions. Also see the related Serverless Patterns Workshop
for a hands-on experience. -
The Amazon Online Tech Talks
YouTube channel includes videos about Lambda-related topics. For an overview of serverless applications and Lambda, see the Introduction to Amazon Lambda & Serverless Applications video.
Accessing Lambda
You can create, invoke, and manage Lambda functions using any of the following interfaces:
-
Amazon Management Console – Provides a web interface for you to access your functions. For more information, see Lambda console.
-
Amazon Command Line Interface (Amazon CLI) – Provides commands for a broad set of Amazon services, including Lambda, and is supported on Windows, macOS, and Linux. For more information, see Using Lambda with the Amazon CLI.
-
Amazon 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 Amazon SDKs
. -
Amazon CloudFormation – Enables you to create templates that define your Lambda applications. For more information, see Amazon Lambda applications. Amazon CloudFormation also supports the Amazon Cloud Development Kit (Amazon CDK)
. -
Amazon Serverless Application Model (Amazon SAM) – Provides templates and a CLI to configure and manage Amazon serverless applications. For more information, see Getting started with Amazon 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.