EventSourceMapping L2 construct
Defines a Lambda EventSourceMapping resource.
Usually, you won't need to define the mapping yourself. This will usually be done by event sources. For example, to add an SQS event source to a function:
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as eventsources from 'aws-cdk-lib/aws-lambda-event-sources';
declare const handler: lambda.Function;
declare const queue: sqs.Queue;
handler.addEventSource(new eventsources.SqsEventSource(queue));
The SqsEventSource class will automatically create the mapping, and will also modify the Lambda's execution role so it can consume messages from the queue.
Example
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import * as cdk from 'aws-cdk-lib';
import { aws_kms as kms } from 'aws-cdk-lib';
import { aws_lambda as lambda } from 'aws-cdk-lib';
declare const eventSourceDlq: lambda.IEventSourceDlq;
declare const filters: any;
declare const function_: lambda.Function;
declare const key: kms.Key;
declare const schemaRegistry: lambda.ISchemaRegistry;
declare const sourceAccessConfigurationType: lambda.SourceAccessConfigurationType;
const eventSourceMapping = new lambda.EventSourceMapping(this, 'MyEventSourceMapping', {
target: function_,
// the properties below are optional
batchSize: 123,
bisectBatchOnError: false,
enabled: false,
eventSourceArn: 'eventSourceArn',
filterEncryption: key,
filters: [{
filtersKey: filters,
}],
kafkaBootstrapServers: ['kafkaBootstrapServers'],
kafkaConsumerGroupId: 'kafkaConsumerGroupId',
kafkaTopic: 'kafkaTopic',
logLevel: lambda.EventSourceMappingLogLevel.INFO,
maxBatchingWindow: cdk.Duration.minutes(30),
maxConcurrency: 123,
maxRecordAge: cdk.Duration.minutes(30),
metricsConfig: {
metrics: [lambda.MetricType.EVENT_COUNT],
},
onFailure: eventSourceDlq,
parallelizationFactor: 123,
provisionedPollerConfig: {
maximumPollers: 123,
minimumPollers: 123,
pollerGroupName: 'pollerGroupName',
},
reportBatchItemFailures: false,
retryAttempts: 123,
schemaRegistryConfig: schemaRegistry,
sourceAccessConfigurations: [{
type: sourceAccessConfigurationType,
uri: 'uri',
}],
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
startingPositionTimestamp: 123,
supportS3OnFailureDestination: false,
tumblingWindow: cdk.Duration.minutes(30),
});
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
import aws_cdk as cdk
from aws_cdk import aws_kms as kms
from aws_cdk import aws_lambda as lambda_
# event_source_dlq: lambda.IEventSourceDlq
# filters: Any
# function_: lambda.Function
# key: kms.Key
# schema_registry: lambda.ISchemaRegistry
# source_access_configuration_type: lambda.SourceAccessConfigurationType
event_source_mapping = lambda_.EventSourceMapping(self, "MyEventSourceMapping",
target=function_,
# the properties below are optional
batch_size=123,
bisect_batch_on_error=False,
enabled=False,
event_source_arn="eventSourceArn",
filter_encryption=key,
filters=[{
"filters_key": filters
}],
kafka_bootstrap_servers=["kafkaBootstrapServers"],
kafka_consumer_group_id="kafkaConsumerGroupId",
kafka_topic="kafkaTopic",
log_level=lambda_.EventSourceMappingLogLevel.INFO,
max_batching_window=cdk.Duration.minutes(30),
max_concurrency=123,
max_record_age=cdk.Duration.minutes(30),
metrics_config=lambda.MetricsConfig(
metrics=[lambda_.MetricType.EVENT_COUNT]
),
on_failure=event_source_dlq,
parallelization_factor=123,
provisioned_poller_config=lambda.ProvisionedPollerConfig(
maximum_pollers=123,
minimum_pollers=123,
poller_group_name="pollerGroupName"
),
report_batch_item_failures=False,
retry_attempts=123,
schema_registry_config=schema_registry,
source_access_configurations=[lambda.SourceAccessConfiguration(
type=source_access_configuration_type,
uri="uri"
)],
starting_position=lambda_.StartingPosition.TRIM_HORIZON,
starting_position_timestamp=123,
support_s3_on_failure_destination=False,
tumbling_window=cdk.Duration.minutes(30)
)
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.*;
import software.amazon.awscdk.services.kms.*;
import software.amazon.awscdk.services.lambda.*;
IEventSourceDlq eventSourceDlq;
Object filters;
Function function_;
Key key;
ISchemaRegistry schemaRegistry;
SourceAccessConfigurationType sourceAccessConfigurationType;
EventSourceMapping eventSourceMapping = EventSourceMapping.Builder.create(this, "MyEventSourceMapping")
.target(function_)
// the properties below are optional
.batchSize(123)
.bisectBatchOnError(false)
.enabled(false)
.eventSourceArn("eventSourceArn")
.filterEncryption(key)
.filters(List.of(Map.of(
"filtersKey", filters)))
.kafkaBootstrapServers(List.of("kafkaBootstrapServers"))
.kafkaConsumerGroupId("kafkaConsumerGroupId")
.kafkaTopic("kafkaTopic")
.logLevel(EventSourceMappingLogLevel.INFO)
.maxBatchingWindow(Duration.minutes(30))
.maxConcurrency(123)
.maxRecordAge(Duration.minutes(30))
.metricsConfig(MetricsConfig.builder()
.metrics(List.of(MetricType.EVENT_COUNT))
.build())
.onFailure(eventSourceDlq)
.parallelizationFactor(123)
.provisionedPollerConfig(ProvisionedPollerConfig.builder()
.maximumPollers(123)
.minimumPollers(123)
.pollerGroupName("pollerGroupName")
.build())
.reportBatchItemFailures(false)
.retryAttempts(123)
.schemaRegistryConfig(schemaRegistry)
.sourceAccessConfigurations(List.of(SourceAccessConfiguration.builder()
.type(sourceAccessConfigurationType)
.uri("uri")
.build()))
.startingPosition(StartingPosition.TRIM_HORIZON)
.startingPositionTimestamp(123)
.supportS3OnFailureDestination(false)
.tumblingWindow(Duration.minutes(30))
.build();
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK;
using Amazon.CDK.AWS.KMS;
using Amazon.CDK.AWS.Lambda;
IEventSourceDlq eventSourceDlq;
var filters;
Function function_;
Key key;
ISchemaRegistry schemaRegistry;
SourceAccessConfigurationType sourceAccessConfigurationType;
var eventSourceMapping = new EventSourceMapping(this, "MyEventSourceMapping", new EventSourceMappingProps {
Target = function_,
// the properties below are optional
BatchSize = 123,
BisectBatchOnError = false,
Enabled = false,
EventSourceArn = "eventSourceArn",
FilterEncryption = key,
Filters = new [] { new Dictionary<string, object> {
{ "filtersKey", filters }
} },
KafkaBootstrapServers = new [] { "kafkaBootstrapServers" },
KafkaConsumerGroupId = "kafkaConsumerGroupId",
KafkaTopic = "kafkaTopic",
LogLevel = EventSourceMappingLogLevel.INFO,
MaxBatchingWindow = Duration.Minutes(30),
MaxConcurrency = 123,
MaxRecordAge = Duration.Minutes(30),
MetricsConfig = new MetricsConfig {
Metrics = new [] { MetricType.EVENT_COUNT }
},
OnFailure = eventSourceDlq,
ParallelizationFactor = 123,
ProvisionedPollerConfig = new ProvisionedPollerConfig {
MaximumPollers = 123,
MinimumPollers = 123,
PollerGroupName = "pollerGroupName"
},
ReportBatchItemFailures = false,
RetryAttempts = 123,
SchemaRegistryConfig = schemaRegistry,
SourceAccessConfigurations = new [] { new SourceAccessConfiguration {
Type = sourceAccessConfigurationType,
Uri = "uri"
} },
StartingPosition = StartingPosition.TRIM_HORIZON,
StartingPositionTimestamp = 123,
SupportS3OnFailureDestination = false,
TumblingWindow = Duration.Minutes(30)
});
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
var eventSourceDlq IEventSourceDlq
var filters interface{}
var function_ Function
var key Key
var schemaRegistry ISchemaRegistry
var sourceAccessConfigurationType SourceAccessConfigurationType
eventSourceMapping := awscdk.Aws_lambda.NewEventSourceMapping(this, jsii.String("MyEventSourceMapping"), &EventSourceMappingProps{
Target: function_,
// the properties below are optional
BatchSize: jsii.Number(123),
BisectBatchOnError: jsii.Boolean(false),
Enabled: jsii.Boolean(false),
EventSourceArn: jsii.String("eventSourceArn"),
FilterEncryption: key,
Filters: []map[string]interface{}{
map[string]interface{}{
"filtersKey": filters,
},
},
KafkaBootstrapServers: []*string{
jsii.String("kafkaBootstrapServers"),
},
KafkaConsumerGroupId: jsii.String("kafkaConsumerGroupId"),
KafkaTopic: jsii.String("kafkaTopic"),
LogLevel: awscdk.*Aws_lambda.EventSourceMappingLogLevel_INFO,
MaxBatchingWindow: cdk.Duration_Minutes(jsii.Number(30)),
MaxConcurrency: jsii.Number(123),
MaxRecordAge: cdk.Duration_*Minutes(jsii.Number(30)),
MetricsConfig: &MetricsConfig{
Metrics: []MetricType{
awscdk.*Aws_lambda.MetricType_EVENT_COUNT,
},
},
OnFailure: eventSourceDlq,
ParallelizationFactor: jsii.Number(123),
ProvisionedPollerConfig: &ProvisionedPollerConfig{
MaximumPollers: jsii.Number(123),
MinimumPollers: jsii.Number(123),
PollerGroupName: jsii.String("pollerGroupName"),
},
ReportBatchItemFailures: jsii.Boolean(false),
RetryAttempts: jsii.Number(123),
SchemaRegistryConfig: schemaRegistry,
SourceAccessConfigurations: []SourceAccessConfiguration{
&SourceAccessConfiguration{
Type: sourceAccessConfigurationType,
Uri: jsii.String("uri"),
},
},
StartingPosition: awscdk.*Aws_lambda.StartingPosition_TRIM_HORIZON,
StartingPositionTimestamp: jsii.Number(123),
SupportS3OnFailureDestination: jsii.Boolean(false),
TumblingWindow: cdk.Duration_*Minutes(jsii.Number(30)),
})
Initializer
new EventSourceMapping(scope: Construct, id: string, props: EventSourceMappingProps)
EventSourceMapping(scope: Construct, id: str, props: EventSourceMappingProps)
new EventSourceMapping(Construct scope, String id, EventSourceMappingProps props)
new EventSourceMapping(Construct scope, string id, EventSourceMappingProps props)
NewEventSourceMapping(Scope Construct, Id *string, Props EventSourceMappingProps)
Construct Props
batchSizenumberbatch_sizeint | floatbatchSizeNumberBatchSizedoubleBatchSize*float64= Amazon Kinesis, Amazon DynamoDB,…#Your function receives an event with all the retrieved records.
Valid Range: Minimum value of 1. Maximum value of 10000.
default
Default: Amazon Kinesis, Amazon DynamoDB, and Amazon MSK is 100 records. The default for Amazon SQS is 10 messages. For standard SQS queues, the maximum is 10,000. For FIFO SQS queues, the maximum is 10.
bisectBatchOnErrorbooleanbisect_batch_on_errorboolbisectBatchOnErrorBooleanBisectBatchOnErrorboolBisectBatchOnError*bool= false#eventSourceArnstringevent_source_arnstreventSourceArnStringEventSourceArnstringEventSourceArn*string= not set if using a self managed …#Any record added to this stream can invoke the Lambda function.
default
Default: not set if using a self managed Kafka cluster, throws an error otherwise
filterEncryptionIKeyfilter_encryptionIKeyfilterEncryptionIKeyFilterEncryptionIKeyFilterEncryptionIKey= none#filters{ [key: string]: any }[]filterstyping.List[typing.Mapping[str, Any]]filtersList<Map<String, Object>>FiltersIDictionary<string, object>[]Filters*[]*map[string]interface{}= none#kafkaBootstrapServersstring[]kafka_bootstrap_serverstyping.List[str]kafkaBootstrapServersList<String>KafkaBootstrapServersstring[]KafkaBootstrapServers*[]*string= none#They are in the format abc.example.com:9096.
kafkaConsumerGroupIdstringkafka_consumer_group_idstrkafkaConsumerGroupIdStringKafkaConsumerGroupIdstringKafkaConsumerGroupId*string= none#notes
The consumer group ID must be unique among all your Kafka event sources. After creating a Kafka event source mapping with the consumer group ID specified, you cannot update this value. The value must have a length between 1 and 200 and full the pattern '[a-zA-Z0-9-/:_+=.@-]'. For more information, see Customizable consumer group ID.
logLevelEventSourceMappingLogLevellog_levelEventSourceMappingLogLevellogLevelEventSourceMappingLogLevelLogLevelEventSourceMappingLogLevelLogLevelEventSourceMappingLogLevel= No logging#notes
This configuration controls the verbosity of the logs generated by the polling infrastructure that reads events from the event source. The logs provide insights into the internal operations of the event source mapping, including connection status, polling behavior, and error conditions.
maxBatchingWindowDurationmax_batching_windowDurationmaxBatchingWindowDurationMaxBatchingWindowDurationMaxBatchingWindowDuration= Duration.seconds(0)#Maximum of Duration.minutes(5)
maxConcurrencynumbermax_concurrencyint | floatmaxConcurrencyNumberMaxConcurrencydoubleMaxConcurrency*float64= No specific limit.#maxRecordAgeDurationmax_record_ageDurationmaxRecordAgeDurationMaxRecordAgeDurationMaxRecordAgeDuration= infinite or until the record exp…#Valid Range:
- Minimum value of 60 seconds
- Maximum value of 7 days
default
Default: infinite or until the record expires.
metricsConfigMetricsConfigmetrics_configMetricsConfigmetricsConfigMetricsConfigMetricsConfigMetricsConfigMetricsConfigMetricsConfig= Enhanced monitoring is disabled#onFailureIEventSourceDlqon_failureIEventSourceDlqonFailureIEventSourceDlqOnFailureIEventSourceDlqOnFailureIEventSourceDlq= discarded records are ignored#parallelizationFactornumberparallelization_factorint | floatparallelizationFactorNumberParallelizationFactordoubleParallelizationFactor*float64= 1#Valid Range:
- Minimum value of 1
- Maximum value of 10
provisionedPollerConfigProvisionedPollerConfigprovisioned_poller_configProvisionedPollerConfigprovisionedPollerConfigProvisionedPollerConfigProvisionedPollerConfigProvisionedPollerConfigProvisionedPollerConfigProvisionedPollerConfig= no provisioned pollers#When specified, allows control over the minimum and maximum number of pollers that can be provisioned to process events from the source.
reportBatchItemFailuresbooleanreport_batch_item_failuresboolreportBatchItemFailuresBooleanReportBatchItemFailuresboolReportBatchItemFailures*bool= false#retryAttemptsnumberretry_attemptsint | floatretryAttemptsNumberRetryAttemptsdoubleRetryAttempts*float64= infinite or until the record exp…#Set to undefined if you want lambda to keep retrying infinitely or until the record expires.
Valid Range:
- Minimum value of 0
- Maximum value of 10000
default
Default: infinite or until the record expires.
schemaRegistryConfigISchemaRegistryschema_registry_configISchemaRegistryschemaRegistryConfigISchemaRegistrySchemaRegistryConfigISchemaRegistrySchemaRegistryConfigISchemaRegistry= none#sourceAccessConfigurationsSourceAccessConfiguration[]source_access_configurationstyping.List[SourceAccessConfiguration]sourceAccessConfigurationsList<SourceAccessConfiguration>SourceAccessConfigurationsSourceAccessConfiguration[]SourceAccessConfigurations*[]SourceAccessConfiguration= none#startingPositionStartingPositionstarting_positionStartingPositionstartingPositionStartingPositionStartingPositionStartingPositionStartingPositionStartingPosition= no starting position#startingPositionTimestampnumberstarting_position_timestampint | floatstartingPositionTimestampNumberStartingPositionTimestampdoubleStartingPositionTimestamp*float64= no timestamp#supportS3OnFailureDestinationbooleansupport_s3_on_failure_destinationboolsupportS3OnFailureDestinationBooleanSupportS3OnFailureDestinationboolSupportS3OnFailureDestination*bool= false#Kinesis, DynamoDB, MSK and self managed kafka event support S3 OFD
Properties
envResourceEnvironmentenvResourceEnvironmentenvResourceEnvironmentEnvResourceEnvironmentEnvResourceEnvironmentrequired#notes
For resources that are created and managed in a Stack (those created by creating new class instances like new Role(), new Bucket(), etc.), this is always the same as the environment of the stack they belong to.
For referenced resources (those obtained from referencing methods like Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be different than the stack they were imported into.
eventSourceMappingArnstringevent_source_mapping_arnstreventSourceMappingArnStringEventSourceMappingArnstringEventSourceMappingArn*stringrequired#eventSourceMappingIdstringevent_source_mapping_idstreventSourceMappingIdStringEventSourceMappingIdstringEventSourceMappingId*stringrequired#eventSourceMappingRefEventSourceMappingReferenceevent_source_mapping_refEventSourceMappingReferenceeventSourceMappingRefEventSourceMappingReferenceEventSourceMappingRefEventSourceMappingReferenceEventSourceMappingRefEventSourceMappingReferencerequired#PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringrequiredstatic#Methods
applyCrossStackReferenceStrength(strength: ReferenceStrength): voiddef apply_cross_stack_reference_strength(self, strength: ReferenceStrength) -> Nonepublic void applyCrossStackReferenceStrength(ReferenceStrength strength)public void ApplyCrossStackReferenceStrength(ReferenceStrength strength)func ApplyCrossStackReferenceStrength(Strength ReferenceStrength)#notes · 1 parameter
When set, any cross-stack reference to this resource will use the specified mechanism instead of the global default determined by the @aws-cdk/core:defaultCrossStackReferences context key. This is useful for selectively weakening specific references to avoid the "deadly embrace" problem without changing the app-wide default.
Parameters
strengthstrengthstrengthstrengthStrength— - The reference strength to use for this resource.
applyRemovalPolicy(policy: RemovalPolicy): voiddef apply_removal_policy(self, policy: RemovalPolicy) -> Nonepublic void applyRemovalPolicy(RemovalPolicy policy)public void ApplyRemovalPolicy(RemovalPolicy policy)func ApplyRemovalPolicy(Policy RemovalPolicy)#notes
The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.
The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).
fromEventSourceMappingId(scope: Construct, id: string, eventSourceMappingId: string): IEventSourceMapping@staticmethod def from_event_source_mapping_id(scope: Construct, id: str, event_source_mapping_id: str) -> IEventSourceMappingpublic static IEventSourceMapping fromEventSourceMappingId(Construct scope, String id, String eventSourceMappingId)public static IEventSourceMapping FromEventSourceMappingId(Construct scope, string id, string eventSourceMappingId)func FromEventSourceMappingId(Scope Construct, Id *string, EventSourceMappingId *string) IEventSourceMappingstatic#isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#notes · 1 parameter · returns
Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.
Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.
Parameters
xxxxX— Any object.
Returns: true if x is an object created from a class which extends Construct.
isOwnedResource(construct: IConstruct): boolean@staticmethod def is_owned_resource(construct: IConstruct) -> boolpublic static Boolean isOwnedResource(IConstruct construct)public static bool IsOwnedResource(IConstruct construct)func IsOwnedResource(Construct IConstruct) *boolstatic#isResource(construct: IConstruct): boolean@staticmethod def is_resource(construct: IConstruct) -> boolpublic static Boolean isResource(IConstruct construct)public static bool IsResource(IConstruct construct)func IsResource(Construct IConstruct) *boolstatic#toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct#Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.