See: Description
| Interface | Description |
|---|---|
| DynamoEventSourceProps | |
| KafkaEventSourceProps |
(experimental) Properties for a Kafka event source.
|
| KinesisEventSourceProps | |
| ManagedKafkaEventSourceProps |
(experimental) Properties for a MSK event source.
|
| S3EventSourceProps | |
| SelfManagedKafkaEventSourceProps |
(experimental) Properties for a self managed Kafka cluster event source.
|
| SnsEventSourceProps |
(experimental) Properties forwarded to the Lambda Subscription.
|
| SqsEventSourceProps | |
| StreamEventSourceProps |
(experimental) The set of properties for event sources that follow the streaming model, such as, Dynamo, Kinesis and Kafka.
|
| Class | Description |
|---|---|
| ApiEventSource | |
| ApiEventSource.Builder |
(experimental) A fluent builder for
ApiEventSource. |
| DynamoEventSource |
(experimental) Use an Amazon DynamoDB stream as an event source for AWS Lambda.
|
| DynamoEventSource.Builder |
(experimental) A fluent builder for
DynamoEventSource. |
| DynamoEventSourceProps.Builder |
A builder for
DynamoEventSourceProps |
| DynamoEventSourceProps.Jsii$Proxy |
An implementation for
DynamoEventSourceProps |
| KafkaEventSourceProps.Builder |
A builder for
KafkaEventSourceProps |
| KafkaEventSourceProps.Jsii$Proxy |
An implementation for
KafkaEventSourceProps |
| KinesisEventSource |
(experimental) Use an Amazon Kinesis stream as an event source for AWS Lambda.
|
| KinesisEventSource.Builder |
(experimental) A fluent builder for
KinesisEventSource. |
| KinesisEventSourceProps.Builder |
A builder for
KinesisEventSourceProps |
| KinesisEventSourceProps.Jsii$Proxy |
An implementation for
KinesisEventSourceProps |
| ManagedKafkaEventSource |
(experimental) Use a MSK cluster as a streaming source for AWS Lambda.
|
| ManagedKafkaEventSource.Builder |
(experimental) A fluent builder for
ManagedKafkaEventSource. |
| ManagedKafkaEventSourceProps.Builder |
A builder for
ManagedKafkaEventSourceProps |
| ManagedKafkaEventSourceProps.Jsii$Proxy |
An implementation for
ManagedKafkaEventSourceProps |
| S3EventSource |
(experimental) Use S3 bucket notifications as an event source for AWS Lambda.
|
| S3EventSource.Builder |
(experimental) A fluent builder for
S3EventSource. |
| S3EventSourceProps.Builder |
A builder for
S3EventSourceProps |
| S3EventSourceProps.Jsii$Proxy |
An implementation for
S3EventSourceProps |
| SelfManagedKafkaEventSource |
(experimental) Use a self hosted Kafka installation as a streaming source for AWS Lambda.
|
| SelfManagedKafkaEventSource.Builder |
(experimental) A fluent builder for
SelfManagedKafkaEventSource. |
| SelfManagedKafkaEventSourceProps.Builder |
A builder for
SelfManagedKafkaEventSourceProps |
| SelfManagedKafkaEventSourceProps.Jsii$Proxy |
An implementation for
SelfManagedKafkaEventSourceProps |
| SnsDlq |
(experimental) An SNS dead letter queue destination configuration for a Lambda event source.
|
| SnsEventSource |
(experimental) Use an Amazon SNS topic as an event source for AWS Lambda.
|
| SnsEventSource.Builder |
(experimental) A fluent builder for
SnsEventSource. |
| SnsEventSourceProps.Builder |
A builder for
SnsEventSourceProps |
| SnsEventSourceProps.Jsii$Proxy |
An implementation for
SnsEventSourceProps |
| SqsDlq |
(experimental) An SQS dead letter queue destination configuration for a Lambda event source.
|
| SqsEventSource |
(experimental) Use an Amazon SQS queue as an event source for AWS Lambda.
|
| SqsEventSource.Builder |
(experimental) A fluent builder for
SqsEventSource. |
| SqsEventSourceProps.Builder |
A builder for
SqsEventSourceProps |
| SqsEventSourceProps.Jsii$Proxy |
An implementation for
SqsEventSourceProps |
| StreamEventSource |
(experimental) Use an stream as an event source for AWS Lambda.
|
| StreamEventSourceProps.Builder |
A builder for
StreamEventSourceProps |
| StreamEventSourceProps.Jsii$Proxy |
An implementation for
StreamEventSourceProps |
| Enum | Description |
|---|---|
| AuthenticationMethod |
(experimental) The authentication method to use with SelfManagedKafkaEventSource.
|
---
An event source mapping is an AWS Lambda resource that reads from an event source and invokes a Lambda function. You can use event source mappings to process items from a stream or queue in services that don't invoke Lambda functions directly. Lambda provides event source mappings for the following services. Read more about lambda event sources here.
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.
The following code sets up a lambda function with an SQS queue event source -
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Function fn = new Function(this, "MyFunction", new FunctionProps()); Queue queue = new Queue(this, "MyQueue"); Object eventSource = fn.addEventSource(new SqsEventSource(queue)); Object eventSourceId = eventSource.getEventSourceId();
The eventSourceId property contains the event source id. This will be a
token that will resolve to the final value at the time of
deployment.
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:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import software.amazon.awscdk.aws_sqs;
import aws.cdk.lib.aws.lambda.event.sources.SqsEventSource;
import software.amazon.awscdk.Duration;
Queue queue = new Queue(this, "MyQueue", new QueueProps()
.visibilityTimeout(Duration.seconds(30))// default,
.receiveMessageWaitTime(Duration.seconds(20)));
lambda.addEventSource(new SqsEventSource(queue, new SqsEventSourceProps()
.batchSize(10)// default
.maxBatchingWindow(Duration.minutes(5))));
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.
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import software.amazon.awscdk.aws_s3;
import aws.cdk.lib.aws.lambda.event.sources.S3EventSource;
Bucket bucket = new Bucket(...);
lambda.addEventSource(new S3EventSource(bucket, new S3EventSourceProps()
.events(asList(s3.EventType.getOBJECT_CREATED(), s3.EventType.getOBJECT_REMOVED()))
.filters(asList(new NotificationKeyFilter().prefix("subdir/")))));
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.
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import software.amazon.awscdk.aws_sns;
import aws.cdk.lib.aws.lambda.event.sources.SnsEventSource;
Topic topic = new Topic(...);
Queue deadLetterQueue = new Queue(this, "deadLetterQueue");
lambda.addEventSource(new SnsEventSource(topic, new SnsEventSourceProps()
.filterPolicy(Map.of(...))
.deadLetterQueue(deadLetterQueue)));
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.
You can write Lambda functions to process change events from a DynamoDB Table. An event is emitted to a DynamoDB stream (if configured) whenever a write (Put, Delete, Update) operation is performed against the table. See Using AWS Lambda with Amazon DynamoDB for more information about configuring Lambda function event sources with DynamoDB.
To process events with a Lambda function, first create or update a DynamoDB table and enable a stream specification. Then, create a DynamoEventSource
and add it to your Lambda function. The following parameters will impact Amazon DynamoDB's polling behavior:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import software.amazon.awscdk.aws_dynamodb;
import software.amazon.awscdk.aws_lambda;
import software.amazon.awscdk.aws_sqs;
import aws.cdk.lib.aws.lambda.event.sources.DynamoEventSource;
import aws.cdk.lib.aws.lambda.event.sources.SqsDlq;
Table table = Table.Builder.create(...)
.partitionKey()...
.stream(dynamodb.StreamViewType.getNEW_IMAGE())
.build();
Queue deadLetterQueue = new Queue(this, "deadLetterQueue");public void () {
}new Function(...);
functionaddEventSource(new DynamoEventSource(table, new DynamoEventSourceProps()
.startingPosition(lambda.StartingPosition.getTRIM_HORIZON())
.batchSize(5)
.bisectBatchOnError(true)
.onFailure(new SqsDlq(deadLetterQueue))
.retryAttempts(10)));
You can write Lambda functions to process streaming data in Amazon Kinesis Streams. For more information about Amazon Kinesis, see Amazon Kinesis Service. To learn more about configuring Lambda function event sources with kinesis and view a sample event, see Amazon Kinesis Event.
To set up Amazon Kinesis as an event source for AWS Lambda, you first create or update an Amazon Kinesis stream and select custom values for the event source parameters. The following parameters will impact Amazon Kinesis's polling behavior:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import software.amazon.awscdk.aws_lambda;
import software.amazon.awscdk.aws_kinesis;
import aws.cdk.lib.aws.lambda.event.sources.KinesisEventSource;
Stream stream = new Stream(this, "MyStream");
myFunction.addEventSource(new KinesisEventSource(stream, new KinesisEventSourceProps()
.batchSize(100)// default
.startingPosition(lambda.StartingPosition.getTRIM_HORIZON())));
You can write Lambda functions to process data either from Amazon MSK or a self managed Kafka cluster.
The following code sets up Amazon MSK as an event source for a lambda function. Credentials will need to be configured to access the MSK cluster, as described in Username/Password authentication.
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import software.amazon.awscdk.aws_lambda;
import software.amazon.awscdk.aws_lambda;
import aws.cdk.lib.aws.secretmanager.Secret;
import aws.cdk.lib.aws.lambda.event.sources.ManagedKafkaEventSource;
// Your MSK cluster arn
String cluster = "arn:aws:kafka:us-east-1:0123456789019:cluster/SalesCluster/abcd1234-abcd-cafe-abab-9876543210ab-4";
// The Kafka topic you want to subscribe to
String topic = "some-cool-topic";
// The secret that allows access to your MSK cluster
// You still have to make sure that it is associated with your cluster as described in the documentation
Object secret = Secret.Builder.create(this, "Secret").secretName("AmazonMSK_KafkaSecret").build();
myFunction.addEventSource(new ManagedKafkaEventSource(new ManagedKafkaEventSourceProps()
.clusterArn(clusterArn)
.topic(topic)
.secret(secret)
.batchSize(100)// default
.startingPosition(lambda.StartingPosition.getTRIM_HORIZON())));
The following code sets up a self managed Kafka cluster as an event source. Username and password based authentication will need to be set up as described in Managing access and permissions.
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import software.amazon.awscdk.aws_lambda;
import aws.cdk.lib.aws.secretmanager.Secret;
import aws.cdk.lib.aws.lambda.event.sources.SelfManagedKafkaEventSource;
// The list of Kafka brokers
Array bootstrapServers = asList("kafka-broker:9092");
// The Kafka topic you want to subscribe to
String topic = "some-cool-topic";
// The secret that allows access to your self hosted Kafka cluster
Object secret = Secret.Builder.create(this, "Secret")....build();
myFunction.addEventSource(new SelfManagedKafkaEventSource(new SelfManagedKafkaEventSourceProps()
.bootstrapServers(bootstrapServers)
.topic(topic)
.secret(secret)
.batchSize(100)// default
.startingPosition(lambda.StartingPosition.getTRIM_HORIZON())));
If your self managed Kafka cluster is only reachable via VPC also configure vpc vpcSubnets and securityGroup.
Eventually, this module will support all the event sources described under Supported Event Sources in the AWS Lambda Developer Guide.
Copyright © 2021. All rights reserved.