AWS Lambda Event Sources
This module includes classes that allow using various AWS services as event
sources for AWS Lambda via the high-level lambda.addEventSource(source)
API.
NOTE: In most cases, it is also possible to use the resource APIs to invoke an
AWS Lambda function. This library provides a uniform API for all Lambda event
sources regardless of the underlying mechanism they use.
SQS
Amazon Simple Queue Service (Amazon SQS) allows you to build asynchronous
workflows. For more information about Amazon SQS, see Amazon Simple Queue
Service. You can configure AWS Lambda to poll for these messages as they arrive
and then pass the event to a Lambda function invocation. To view a sample event,
see Amazon SQS Event.
To set up Amazon Simple Queue Service as an event source for AWS Lambda, you
first create or update an Amazon SQS queue and select custom values for the
queue parameters. The following parameters will impact Amazon SQS's polling
behavior:
- visibilityTimeoutSec: May impact the period between retries.
- receiveMessageWaitTimeSec: Will determine long
poll
duration. The default value is 20 seconds.
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources';
const queue = new sqs.Queue(this, 'MyQueue', {
visibilityTimeoutSec: 30
receiveMessageWaitTimeSec: 20
});
lambda.addEventSource(new SqsEventSource(queue, {
batchSize: 10
});
S3
You can write Lambda functions to process S3 bucket events, such as the
object-created or object-deleted events. For example, when a user uploads a
photo to a bucket, you might want Amazon S3 to invoke your Lambda function so
that it reads the image and creates a thumbnail for the photo.
You can use the bucket notification configuration feature in Amazon S3 to
configure the event source mapping, identifying the bucket events that you want
Amazon S3 to publish and which Lambda function to invoke.
import { S3EventSource } from '@aws-cdk/aws-lambda-event-sources';
const bucket = new s3.Bucket(...);
lambda.addEventSource(new S3EventSource(bucket, {
events: [ s3.EventType.ObjectCreated, s3.EventType.ObjectDeleted ],
filters: [ { prefix: 'subdir/' } ]
}));
SNS
You can write Lambda functions to process Amazon Simple Notification Service
notifications. When a message is published to an Amazon SNS topic, the service
can invoke your Lambda function by passing the message payload as a parameter.
Your Lambda function code can then process the event, for example publish the
message to other Amazon SNS topics, or send the message to other AWS services.
This also enables you to trigger a Lambda function in response to Amazon
CloudWatch alarms and other AWS services that use Amazon SNS.
For an example event, see Appendix: Message and JSON
Formats and
Amazon SNS Sample
Event.
For an example use case, see Using AWS Lambda with Amazon SNS from Different
Accounts.
import { SnsEventSource } from '@aws-cdk/aws-lambda-event-sources';
const topic = new sns.Topic(...);
lambda.addEventSource(new SnsEventSource(topic));
When a user calls the SNS Publish API on a topic that your Lambda function is
subscribed to, Amazon SNS will call Lambda to invoke your function
asynchronously. Lambda will then return a delivery status. If there was an error
calling Lambda, Amazon SNS will retry invoking the Lambda function up to three
times. After three tries, if Amazon SNS still could not successfully invoke the
Lambda function, then Amazon SNS will send a delivery status failure message to
CloudWatch.
Roadmap
Eventually, this module will support all the event sources described under
Supported Event
Sources
in the AWS Lambda Developer Guide.