AWS Lambda Event Source Mapping
This page shows how to write Terraform and CloudFormation for Lambda Event Source Mapping and write them securely.
aws_lambda_event_source_mapping (Terraform)
The Event Source Mapping in Lambda can be configured in Terraform with the resource name aws_lambda_event_source_mapping
. The following sections describe 2 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_lambda_event_source_mapping" "consumer-sqs" {
event_source_arn = aws_sqs_queue.test-sqs.arn
enabled = true
function_name = aws_lambda_function.consumer-sqs.arn
batch_size = 1
}
resource "aws_lambda_event_source_mapping" "dog_processor_trigger" {
event_source_arn = aws_kinesis_stream.caught_dogs_stream.arn
function_name = "dogProcessor"
batch_size = 1
starting_position = "LATEST"
enabled = true
Parameters
-
batch_size
optional - number -
bisect_batch_on_function_error
optional - bool -
enabled
optional - bool -
event_source_arn
required - string -
function_arn
optional computed - string -
function_name
required - string -
id
optional computed - string -
last_modified
optional computed - string -
last_processing_result
optional computed - string -
maximum_batching_window_in_seconds
optional - number -
maximum_record_age_in_seconds
optional computed - number -
maximum_retry_attempts
optional computed - number -
parallelization_factor
optional computed - number -
starting_position
optional - string -
starting_position_timestamp
optional - string -
state
optional computed - string -
state_transition_reason
optional computed - string -
topics
optional - set of string -
uuid
optional computed - string -
destination_config
list block-
on_failure
list block-
destination_arn
required - string
-
-
Explanation in Terraform Registry
Provides a Lambda event source mapping. This allows Lambda functions to get events from Kinesis, DynamoDB, SQS, Amazon MQ and Managed Streaming for Apache Kafka (MSK). For information about Lambda and how to use it, see [What is AWS Lambda?][1]. For information about event source mappings, see [CreateEventSourceMapping][2] in the API docs.
Tips: Best Practices for The Other AWS Lambda Resources
In addition to the aws_lambda_permission, AWS Lambda has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_lambda_permission
Ensure to limit your Lambda function permission as much as possible
It is better for limiting the Lambda function permission to set `source_arn` if the ARN can be specified to grant permissions.
AWS::Lambda::EventSourceMapping (CloudFormation)
The EventSourceMapping in Lambda can be configured in CloudFormation with the resource name AWS::Lambda::EventSourceMapping
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::Lambda::EventSourceMapping
Properties:
StartingPosition: LATEST
EventSourceArn: !GetAtt StatsTable.StreamArn
FunctionName: !GetAtt TableStreamConsumerLambdaFunction.Arn
MaximumRetryAttempts: 1
Type: "AWS::Lambda::EventSourceMapping"
Properties:
BatchSize: 10
Enabled: True
EventSourceArn:
Fn::Join:
Type: AWS::Lambda::EventSourceMapping
Properties:
BatchSize: 100
Enabled: True
EventSourceArn: !GetAtt CsdRoute.StreamArn
FunctionName:
Type: AWS::Lambda::EventSourceMapping
Properties:
BatchSize: 100
Enabled: True
EventSourceArn: !GetAtt CsdRoute.StreamArn
FunctionName:
Type: AWS::Lambda::EventSourceMapping
Properties:
BatchSize: 10
Enabled: true
EventSourceArn: !GetAtt SQSPaymentResponseToTwitch.Arn
FunctionName: !GetAtt LFPaymentResponseToTwitch.Arn
"path": "/ResourceTypes/AWS::Lambda::EventSourceMapping/Properties/BatchSize/Value",
"value": {
"ValueType": "AWS::Lambda::EventSourceMapping.BatchSize"
}
},
{
"Type": "AWS::Lambda::EventSourceMapping",
"DependsOn": [
"LambdaTriggerPolicyUser",
"LambdaExecutionRole"
],
"Properties": {
"Type": "AWS::Lambda::EventSourceMapping",
"Properties": {
"MaximumBatchingWindowInSeconds": 20,
"EventSourceArn": {
"Fn::GetAtt": [
"KinesisStream",
"Type": "AWS::Lambda::EventSourceMapping",
"Properties": {
"MaximumBatchingWindowInSeconds": 20,
"EventSourceArn": {
"Fn::GetAtt": [
"KinesisStream",
"Type": "AWS::Lambda::EventSourceMapping",
"Properties": {
"MaximumBatchingWindowInSeconds": 20,
"EventSourceArn": {
"Fn::GetAtt": [
"KinesisStream",
Parameters
-
BatchSize
optional - Integer -
BisectBatchOnFunctionError
optional - Boolean -
DestinationConfig
optional - DestinationConfig -
Enabled
optional - Boolean -
EventSourceArn
optional - String -
FunctionName
required - String -
MaximumBatchingWindowInSeconds
optional - Integer -
MaximumRecordAgeInSeconds
optional - Integer -
MaximumRetryAttempts
optional - Integer -
ParallelizationFactor
optional - Integer -
StartingPosition
optional - String -
StartingPositionTimestamp
optional - Double -
Topics
optional - List -
Queues
optional - List -
SourceAccessConfigurations
optional - List of SourceAccessConfiguration -
TumblingWindowInSeconds
optional - Integer -
FunctionResponseTypes
optional - List -
SelfManagedEventSource
optional - SelfManagedEventSource
Explanation in CloudFormation Registry
The
AWS::Lambda::EventSourceMapping
resource creates a mapping between an event source and an AWS Lambda function. Lambda reads items from the event source and triggers the function.For details about each event source type, see the following topics. In particular, each of the topics describes the required and optional parameters for the specific event source.
Frequently asked questions
What is AWS Lambda Event Source Mapping?
AWS Lambda Event Source Mapping is a resource for Lambda of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Lambda Event Source Mapping?
For Terraform, the tochukaso/terraform and rodrigogregorioneri/localstack_terraform source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the broswen/StatsStream, mozilla-iam/cis_functions and mmanoj880/DevOps source code examples are useful. See the CloudFormation Example section for further details.