Resource: aws_lambda_event_source_mapping

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?. For information about event source mappings, see CreateEventSourceMapping in the API docs.

Example Usage

DynamoDB

resource "aws_lambda_event_source_mapping" "example" {
  event_source_arn  = aws_dynamodb_table.example.stream_arn
  function_name     = aws_lambda_function.example.arn
  starting_position = "LATEST"
}

Kinesis

resource "aws_lambda_event_source_mapping" "example" {
  event_source_arn  = aws_kinesis_stream.example.arn
  function_name     = aws_lambda_function.example.arn
  starting_position = "LATEST"
}

Managed Streaming for Apache Kafka (MSK)

resource "aws_lambda_event_source_mapping" "example" {
  event_source_arn  = aws_msk_cluster.example.arn
  function_name     = aws_lambda_function.example.arn
  topics            = ["Example"]
  starting_position = "TRIM_HORIZON"
}

Self Managed Apache Kafka

resource "aws_lambda_event_source_mapping" "example" {
  function_name     = aws_lambda_function.example.arn
  topics            = ["Example"]
  starting_position = "TRIM_HORIZON"

  self_managed_event_source {
    endpoints = {
      KAFKA_BOOTSTRAP_SERVERS = "kafka1.example.com:9092,kafka2.example.com:9092"
    }
  }

  source_access_configuration {
    type = "VPC_SUBNET"
    uri  = "subnet:subnet-example1"
  }

  source_access_configuration {
    type = "VPC_SUBNET"
    uri  = "subnet:subnet-example2"
  }

  source_access_configuration {
    type = "VPC_SECURITY_GROUP"
    uri  = "security_group:sg-example"
  }
}

SQS

resource "aws_lambda_event_source_mapping" "example" {
  event_source_arn = aws_sqs_queue.sqs_queue_test.arn
  function_name    = aws_lambda_function.example.arn
}

SQS with event filter

resource "aws_lambda_event_source_mapping" "example" {
  event_source_arn = aws_sqs_queue.sqs_queue_test.arn
  function_name    = aws_lambda_function.example.arn

  filter_criteria {
    filter {
      pattern = jsonencode({
        body = {
          Temperature : [{ numeric : [">", 0, "<=", 100] }]
          Location : ["New York"]
        }
      })
    }
  }
}

Amazon MQ (ActiveMQ)

resource "aws_lambda_event_source_mapping" "example" {
  batch_size       = 10
  event_source_arn = aws_mq_broker.example.arn
  enabled          = true
  function_name    = aws_lambda_function.example.arn
  queues           = ["example"]

  source_access_configuration {
    type = "BASIC_AUTH"
    uri  = aws_secretsmanager_secret_version.example.arn
  }
}

Amazon MQ (RabbitMQ)

resource "aws_lambda_event_source_mapping" "example" {
  batch_size       = 1
  event_source_arn = aws_mq_broker.example.arn
  enabled          = true
  function_name    = aws_lambda_function.example.arn
  queues           = ["example"]

  source_access_configuration {
    type = "VIRTUAL_HOST"
    uri  = "/example"
  }

  source_access_configuration {
    type = "BASIC_AUTH"
    uri  = aws_secretsmanager_secret_version.example.arn
  }
}

Argument Reference

amazon_managed_kafka_event_source_config Configuration Block

destination_config Configuration Block

destination_config on_failure Configuration Block

document_db_event_source_config Configuration Block

filter_criteria Configuration Block

filter_criteria filter Configuration Block

scaling_config Configuration Block

self_managed_event_source Configuration Block

self_managed_kafka_event_source_config Configuration Block

source_access_configuration Configuration Block

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import Lambda event source mappings using the UUID (event source mapping identifier). For example:

import {
  to = aws_lambda_event_source_mapping.event_source_mapping
  id = "12345kxodurf3443"
}

Using terraform import, import Lambda event source mappings using the UUID (event source mapping identifier). For example:

% terraform import aws_lambda_event_source_mapping.event_source_mapping 12345kxodurf3443