Invoking Lambda functions - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Invoking Lambda functions

You can invoke Lambda functions directly using the Lambda console, a function URL HTTP(S) endpoint, the Lambda API, an Amazon SDK, the Amazon Command Line Interface (Amazon CLI), and Amazon toolkits. You can also configure other Amazon services to invoke your function in response to events or external requests, or on a schedule. For example, Amazon Simple Storage Service (Amazon S3) can invoke your function when an object is created in an S3 bucket, or Amazon EventBridge (CloudWatch Events) can invoke your function on a schedule. You can also configure Lambda to read items from a stream or a queue and invoke your function to process them.

When you invoke a function, you can choose to invoke it synchronously or asynchronously. With synchronous invocation, you wait for the function to process the event and return a response. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately. For asynchronous invocation, Lambda handles retries and can send invocation records to a destination.

Invoking Lambda functions from another Amazon Web Service

For another Amazon service to invoke your function directly, you need to create a trigger. A trigger is a resource you configure to allow another Amazon service to invoke your function when certain events or conditions occur. Your function can have multiple triggers. Each trigger acts as a client invoking your function independently, and each event that Lambda passes to your function has data from only one trigger.

You can create a trigger for your function using the Lambda console. You can also configure another service to invoke your function by using the Events property in the Amazon Serverless Application Model (Amazon SAM). To learn more see AWS::Serverless::Function in the Amazon Serverless Application Model Developer Guide.

To create a trigger using the Lambda console
  1. Open the functions page of the Lambda console.

  2. Select the function you want to create a trigger for.

  3. In the Function overview pane, choose add trigger.

  4. Select the Amazon service you want to invoke your function.

  5. Fill out the options in the Trigger configuration pane and choose Add.

    Depending on the Amazon Web Service you choose to invoke your function, the trigger configuration options will be different.

For a full list of the Amazon services that can invoke your Lambda function by using a trigger, and for more information about configuring triggers for different services, see Using Lambda with other services.

Invoking Lambda functions from a stream or queue

For your Lambda function to process items from a stream or a queue, such as an Amazon Kinesis stream or an Amazon Simple Queue Service (Amazon SQS) queue, you need to create an event source mapping. An event source mapping is a resource in Lambda that reads items from a stream or a queue and creates events containing batches of items to send to your Lambda function. Each event that your function processes can contain hundreds or thousands of items.

You can create an event source mapping for your Lambda function using the Lambda console, the Amazon CLI, the Lambda API, or an Amazon SDK. You can also add an event source mapping using Amazon SAM or Amazon CloudFormation. To create an event source mapping in the Lambda console, follow the instructions to create a trigger in Invoking Lambda functions from another Amazon Web Service, and select one of the Amazon services that support event source mappings as your source.

To create an event source mapping using the Amazon CLI, Lambda API, or an Amazon SDK, and to see a list of the Amazon services which event source mappings can be used with, refer to Lambda event source mappings. For more information about creating an event source mapping using the Events property in Amazon SAM, see AWS::Serverless::Function in the Amazon Serverless Application Model Developer Guide.

Errors and retries

Depending on how your function is invoked, scaling behavior and the types of errors that occur can vary. When you invoke a function synchronously, you receive errors in the response and can retry. When you invoke asynchronously, use an event source mapping, or configure another service to invoke your function, the retry requirements and the way that your function scales to handle large numbers of events will vary. For more information, see Error handling and automatic retries in Amazon Lambda.