From 9074693372f8574735c85d1a82c60f93465ce92d Mon Sep 17 00:00:00 2001 From: Kaustubh Poshiya Date: Fri, 31 Jan 2025 15:57:15 +0530 Subject: [PATCH 1/2] feat: tracing config and lambda event source mapping --- README.md | 6 ++++ main.tf | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ variables.tf | 34 ++++++++++++++++++ 3 files changed, 137 insertions(+) diff --git a/README.md b/README.md index 2c41931..2f933b8 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ No modules. | [aws_iam_role.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | | [aws_iam_role_policy_attachment.lambda_basic_execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | | [aws_iam_role_policy_attachment.lambda_vpc_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | +| [aws_lambda_event_source_mapping.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping) | resource | | [aws_lambda_function.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource | | [aws_lambda_function_url.function_url](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function_url) | resource | | [aws_lambda_permission.triggers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource | @@ -40,10 +41,14 @@ No modules. | [description](#input\_description) | Lambda function description | `any` | `null` | no | | [env\_vars\_from\_parameter\_store](#input\_env\_vars\_from\_parameter\_store) | Lambda environment variables from SSM parameter store | `map(any)` | `{}` | no | | [environment\_variables](#input\_environment\_variables) | Environment Variables for Lambda Functions | `map(any)` | `{}` | no | +| [event\_source\_mapping](#input\_event\_source\_mapping) | Map of event source mapping | `any` | `{}` | no | | [function\_name](#input\_function\_name) | Lambda function name | `any` | n/a | yes | | [function\_url](#input\_function\_url) | Create lambda function url | `bool` | `false` | no | | [function\_url\_cors](#input\_function\_url\_cors) | Function url cors | `any` | `{}` | no | | [handler](#input\_handler) | Name of Handler | `any` | `null` | no | +| [image\_config\_command](#input\_image\_config\_command) | The CMD for the docker image | `list(string)` | `[]` | no | +| [image\_config\_entry\_point](#input\_image\_config\_entry\_point) | The ENTRYPOINT for the docker image | `list(string)` | `[]` | no | +| [image\_config\_working\_directory](#input\_image\_config\_working\_directory) | The working directory for the docker image | `string` | `null` | no | | [image\_uri](#input\_image\_uri) | uri of image | `any` | `null` | no | | [lambda\_memory](#input\_lambda\_memory) | Required Memory for Lambda function | `number` | `128` | no | | [lambda\_runtime](#input\_lambda\_runtime) | Lambda language | `any` | `null` | no | @@ -60,6 +65,7 @@ No modules. | [source\_file](#input\_source\_file) | Lambda source file | `string` | `""` | no | | [subnets](#input\_subnets) | Subnets | `list(any)` | `null` | no | | [tags](#input\_tags) | Tags | `map` | `{}` | no | +| [tracing\_mode](#input\_tracing\_mode) | Tracing mode of the Lambda Function. Valid value can be either PassThrough or Active. | `string` | `null` | no | ## Outputs diff --git a/main.tf b/main.tf index e000da2..7194e77 100644 --- a/main.tf +++ b/main.tf @@ -67,6 +67,22 @@ resource "aws_lambda_function" "lambda" { } } + dynamic "tracing_config" { + for_each = var.tracing_mode == null ? [] : [true] + content { + mode = var.tracing_mode + } + } + + dynamic "image_config" { + for_each = length(var.image_config_entry_point) > 0 || length(var.image_config_command) > 0 || var.image_config_working_directory != null ? [true] : [] + content { + entry_point = var.image_config_entry_point + command = var.image_config_command + working_directory = var.image_config_working_directory + } + } + tags = var.tags } @@ -119,3 +135,84 @@ resource "aws_lambda_function_url" "function_url" { } } } + +# ------------------------------------------------------------------------------ +# LAMBDA EVENT SOURCE MAPPING +# ------------------------------------------------------------------------------ +resource "aws_lambda_event_source_mapping" "this" { + for_each = { for k, v in var.event_source_mapping : k => v } + + function_name = aws_lambda_function.lambda.arn + + event_source_arn = try(each.value.event_source_arn, null) + + batch_size = try(each.value.batch_size, null) + maximum_batching_window_in_seconds = try(each.value.maximum_batching_window_in_seconds, null) + enabled = try(each.value.enabled, true) + starting_position = try(each.value.starting_position, null) + starting_position_timestamp = try(each.value.starting_position_timestamp, null) + parallelization_factor = try(each.value.parallelization_factor, null) + maximum_retry_attempts = try(each.value.maximum_retry_attempts, null) + maximum_record_age_in_seconds = try(each.value.maximum_record_age_in_seconds, null) + bisect_batch_on_function_error = try(each.value.bisect_batch_on_function_error, null) + function_response_types = try(each.value.function_response_types, null) + tumbling_window_in_seconds = try(each.value.tumbling_window_in_seconds, null) + + dynamic "destination_config" { + for_each = try(each.value.destination_arn_on_failure, null) != null ? [true] : [] + content { + on_failure { + destination_arn = each.value["destination_arn_on_failure"] + } + } + } + + dynamic "scaling_config" { + for_each = try([each.value.scaling_config], []) + content { + maximum_concurrency = try(scaling_config.value.maximum_concurrency, null) + } + } + + dynamic "filter_criteria" { + for_each = try(each.value.filter_criteria, null) != null ? [true] : [] + + content { + dynamic "filter" { + for_each = try(flatten([each.value.filter_criteria]), []) + + content { + pattern = try(filter.value.pattern, null) + } + } + } + } + + dynamic "document_db_event_source_config" { + for_each = try(each.value.document_db_event_source_config, []) + + content { + database_name = document_db_event_source_config.value.database_name + collection_name = try(document_db_event_source_config.value.collection_name, null) + full_document = try(document_db_event_source_config.value.full_document, null) + } + } + + dynamic "metrics_config" { + for_each = try([each.value.metrics_config], []) + + content { + metrics = metrics_config.value.metrics + } + } + + dynamic "provisioned_poller_config" { + for_each = try([each.value.provisioned_poller_config], []) + content { + maximum_pollers = try(provisioned_poller_config.value.maximum_pollers, null) + minimum_pollers = try(provisioned_poller_config.value.minimum_pollers, null) + } + } + + tags = merge(var.tags, try(each.value.tags, {})) +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index fd30f24..950c40d 100644 --- a/variables.tf +++ b/variables.tf @@ -126,3 +126,37 @@ variable "tags" { description = "Tags" default = {} } + +variable "tracing_mode" { + description = "Tracing mode of the Lambda Function. Valid value can be either PassThrough or Active." + type = string + default = null +} + +variable "image_config_entry_point" { + description = "The ENTRYPOINT for the docker image" + type = list(string) + default = [] +} + +variable "image_config_command" { + description = "The CMD for the docker image" + type = list(string) + default = [] +} + +variable "image_config_working_directory" { + description = "The working directory for the docker image" + type = string + default = null +} + +############################################ +# Lambda Event Source Mapping +############################################ + +variable "event_source_mapping" { + description = "Map of event source mapping" + type = any + default = {} +} \ No newline at end of file From d5965bb89708225d1a91792b74aec2847c875190 Mon Sep 17 00:00:00 2001 From: Kaustubh Poshiya Date: Fri, 14 Feb 2025 14:14:05 +0530 Subject: [PATCH 2/2] feat: source code hash for zip and update example.md for event source mapping --- EXAMPLE.md | 18 ++++++++++++++++++ main.tf | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/EXAMPLE.md b/EXAMPLE.md index 8ffe3e8..5582429 100644 --- a/EXAMPLE.md +++ b/EXAMPLE.md @@ -141,4 +141,22 @@ module "lambda_function" { } } } +``` + +## Lambda Event Source Mapping +The example below demonstrates how to configure Lambda Event source Mapping +``` +module "lambda_function" { + source = "git::https://github.com/TechHoldingLLC/terraform-aws-lambda-function.git?ref=v1.0.11" + + # ...omitted for brevity + + event_source_mapping = { + event_source_arn = module.sqs.arn + batch_size = 10 + scaling_config = { + maximum_concurrency = 10 + } + } +} ``` \ No newline at end of file diff --git a/main.tf b/main.tf index 7194e77..da527a0 100644 --- a/main.tf +++ b/main.tf @@ -45,7 +45,7 @@ resource "aws_lambda_function" "lambda" { s3_bucket = try(data.aws_s3_object.lambda[0].bucket, null) s3_key = try(data.aws_s3_object.lambda[0].key, null) s3_object_version = try(data.aws_s3_object.lambda[0].version_id, null) - source_code_hash = try(data.aws_s3_object.lambda[0].metadata.source_code_hash, null) + source_code_hash = try(data.aws_s3_object.lambda[0].metadata.source_code_hash, data.archive_file.lambda[0].output_base64sha256, null) filename = try(data.archive_file.lambda[0].output_path, null) image_uri = var.image_uri package_type = var.package_type @@ -95,7 +95,7 @@ resource "aws_lambda_permission" "triggers" { function_name = aws_lambda_function.lambda.function_name - statement_id = try(each.value.statement_id ,format("Allow%sLambdaInvoke", try(each.key, ""))) + statement_id = try(each.value.statement_id, format("Allow%sLambdaInvoke", try(each.key, ""))) action = "lambda:InvokeFunction" principal = try(each.value.principal, format("%s.amazonaws.com", try(each.value.service, ""))) source_arn = try(each.value.source_arn, null)