Invoking Lambda functions - AWS Lambda

Invoking Lambda functions

You can invoke Lambda functions directly using the Lambda console, a function URL HTTP(S) endpoint, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits. You can also configure other AWS 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 AWS service

For another AWS service to invoke your function directly, you need to create a trigger. A trigger is a resource you configure to allow another AWS 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 AWS Serverless Application Model (AWS SAM). To learn more see AWS::Serverless::Function in the AWS 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 AWS service you want to invoke your function.

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

    Depending on the AWS service you choose to invoke your function, the trigger configuration options will be different.

For a full list of the AWS 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 AWS CLI, the Lambda API, or an AWS SDK. You can also add an event source mapping using AWS SAM or AWS CloudFormation. To create an event source mapping in the Lambda console, follow the instructions to create a trigger in Invoking Lambda functions from another AWS service, and select one of the AWS services that support event source mappings as your source.

To create an event source mapping using the AWS CLI, Lambda API, or an AWS SDK, and to see a list of the AWS 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 AWS SAM, see AWS::Serverless::Function in the AWS 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 AWS Lambda.