From 820955f725a00f0f26e8ee88195a02a6933a966a Mon Sep 17 00:00:00 2001 From: Moritz Zimmer Date: Tue, 8 Nov 2022 11:05:35 +0100 Subject: [PATCH] fix: allow multiple filters in event source mapping filter criteria --- README.md | 25 +++++++++++---- event_source_mappings.tf | 10 ++++-- .../dynamodb-with-alias/main.tf | 32 +++++++++++++------ .../kinesis/main.tf | 24 ++++++++++++-- .../with-event-source-mappings/sqs/main.tf | 24 ++++++++++++-- 5 files changed, 89 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index a47495e..1974d02 100644 --- a/README.md +++ b/README.md @@ -135,13 +135,6 @@ module "lambda" { batch_size = 50 starting_position = "LATEST" - // Lambda event filtering, see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html - filter_criteria = { - pattern = jsonencode({ - eventName : ["MODIFY"] - }) - } - // optionally configure a SNS or SQS destination for discarded batches, required IAM // permissions will be added automatically by this module, // see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventsourcemapping.html @@ -150,6 +143,24 @@ module "lambda" { // optionally overwrite function_name in case an alias should be used in the // event source mapping, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html function_name = aws_lambda_alias.example.arn + + // Lambda event filtering, see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html + filter_criteria = [ + { + pattern = jsonencode({ + data : { + Key1 : ["Value1"] + } + }) + }, + { + pattern = jsonencode({ + data : { + Key2 : [{ "anything-but" : ["Value2"] }] + } + }) + } + ] } table_2 = { diff --git a/event_source_mappings.tf b/event_source_mappings.tf index 1de306c..58ea186 100644 --- a/event_source_mappings.tf +++ b/event_source_mappings.tf @@ -57,11 +57,15 @@ resource "aws_lambda_event_source_mapping" "event_source" { } dynamic "filter_criteria" { - for_each = try(each.value["filter_criteria"], null) != null ? [true] : [] + for_each = try(each.value.filter_criteria, null) != null ? [true] : [] content { - filter { - pattern = try(each.value["filter_criteria"].pattern, null) + dynamic "filter" { + for_each = try(flatten([each.value.filter_criteria]), []) + + content { + pattern = try(filter.value.pattern, null) + } } } } diff --git a/examples/with-event-source-mappings/dynamodb-with-alias/main.tf b/examples/with-event-source-mappings/dynamodb-with-alias/main.tf index 75353ff..4ef288e 100644 --- a/examples/with-event-source-mappings/dynamodb-with-alias/main.tf +++ b/examples/with-event-source-mappings/dynamodb-with-alias/main.tf @@ -64,25 +64,37 @@ module "lambda" { table_1 = { event_source_arn = aws_dynamodb_table.table_1.stream_arn + // optionally overwrite function_name in case an alias should be used in the + // event source mapping, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html + function_name = aws_lambda_alias.example.arn + // optionally overwrite arguments from https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping batch_size = 50 maximum_retry_attempts = 3 - // Lambda event filtering, see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html - filter_criteria = { - pattern = jsonencode({ - eventName : ["MODIFY"] - }) - } - // optionally configure a SNS or SQS destination for discarded batches, required IAM // permissions will be added automatically by this module, // see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventsourcemapping.html destination_arn_on_failure = aws_sqs_queue.errors.arn - // optionally overwrite function_name in case an alias should be used in the - // event source mapping, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html - function_name = aws_lambda_alias.example.arn + + // Lambda event filtering, see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html + filter_criteria = [ + { + pattern = jsonencode({ + data : { + Key1 : ["Value1"] + } + }) + }, + { + pattern = jsonencode({ + data : { + Key2 : [{ "anything-but" : ["Value2"] }] + } + }) + } + ] } table_2 = { diff --git a/examples/with-event-source-mappings/kinesis/main.tf b/examples/with-event-source-mappings/kinesis/main.tf index 7350f89..c7f1af0 100644 --- a/examples/with-event-source-mappings/kinesis/main.tf +++ b/examples/with-event-source-mappings/kinesis/main.tf @@ -36,13 +36,31 @@ module "lambda" { stream_1 = { event_source_arn = aws_kinesis_stream.stream_1.arn + // optionally overwrite function_name in case an alias should be used in the + // event source mapping, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html + // function_name = aws_lambda_alias.example.arn + // optionally overwrite arguments from https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping batch_size = 50 starting_position = "LATEST" // optionally overwrite default 'starting_position' - // optionally overwrite function_name in case an alias should be used in the - // event source mapping, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html - // function_name = aws_lambda_alias.example.arn + // Lambda event filtering, see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html + filter_criteria = [ + { + pattern = jsonencode({ + data : { + Key1 : ["Value1"] + } + }) + }, + { + pattern = jsonencode({ + data : { + Key2 : [{ "anything-but" : ["Value2"] }] + } + }) + } + ] } stream_2 = { diff --git a/examples/with-event-source-mappings/sqs/main.tf b/examples/with-event-source-mappings/sqs/main.tf index 6ac13cd..1086623 100644 --- a/examples/with-event-source-mappings/sqs/main.tf +++ b/examples/with-event-source-mappings/sqs/main.tf @@ -32,6 +32,10 @@ module "lambda" { queue_1 = { event_source_arn = aws_sqs_queue.queue_1.arn + // optionally overwrite function_name in case an alias should be used in the + // event source mapping, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html + // function_name = aws_lambda_alias.example.arn + // optionally overwrite arguments from https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping batch_size = 5 @@ -39,9 +43,23 @@ module "lambda" { maximum_concurrency = 2 } - // optionally overwrite function_name in case an alias should be used in the - // event source mapping, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html - // function_name = aws_lambda_alias.example.arn + // Lambda event filtering, see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html + filter_criteria = [ + { + pattern = jsonencode({ + body : { + Key1 : ["Value1"] + } + }) + }, + { + pattern = jsonencode({ + body : { + Key2 : [{ "anything-but" : ["Value2"] }] + } + }) + } + ] } queue_2 = {