
module "lambda_dd_search" {
  source = "git@github.com:theorchard/terraform-lambda.git//?ref=4.3.0"

  environment                 = var.environment
  application_family          = var.application_family
  use_container_image         = true
  lambda_name                 = var.lambda_name
  lambda_description          = "Link between Kinesis stream and OpenSearch for DDDB encoding_queue_detail."
  lambda_function_timeout     = var.lambda_default_timeout
  vpc_enabled                 = true
  vpc_subnet_ids              = module.vpc_info.default_private_subnet_ids
  vpc_id                      = module.vpc_info.vpc_id
  lambda_function_memory_size = var.lambda_memory_size

  event_source_mapping_enabled                        = true
  event_source_mapping_batch_size                     = var.kinesis_batch_size
  event_source_mapping_resource_type                  = "kinesis"
  event_source_mapping_stream_name                    = data.aws_kinesis_stream.kinesis_stream.name
  event_source_mapping_bisect_batch_on_function_error = true
  event_source_mapping_starting_position              = "AT_TIMESTAMP"
  # September 11, 2025 time stamp as Unix epoch date. Update this if you are going to re-create the event source mapping.
  # else this will re-process all old messages in kinesis stream. You can also set event_source_mapping_maximum_record_age_in_seconds
  # to only run for messages in last 4 hours or so.
  event_source_mapping_starting_position_timestamp   = "2025-09-11T00:00:00Z"
  event_source_mapping_maximum_record_age_in_seconds = "-1"
  event_source_mapping_batch_window                  = 5
  # This is set to -1 for ES lambda but we dont want to retry forever.
  event_source_mapping_maximum_retry_attempts = 5

  # when running a backfill increase the concrrency and reduce the kinesis_batch_size to 25-30 so the lambda does not take too
  # long to process a batch. Also if we increase the concurrency to 20 during normal time it will cause no invocation gaps that causes cold starts.
  lambda_function_reserved_concurrent_executions = var.lambda_function_reserved_concurrent_executions
  datadog_advanced_enabled                       = true
  datadog_enabled                                = true

  iam_managed_policy_attachments = [
    data.aws_iam_policy.os_default_rw_policy.arn,
    module.dd_search_owsrequest_policy.policy_arn_output,
    aws_iam_policy.kms_policy.arn
  ]

  lambda_function_environment_variables = {
    Environment    = var.environment
    LOGGER_LEVEL   = "INFO"
    SNS_TARGET_ARN = module.lambda_dd_search.lambda_dlq_arn
    SENTRY_DSN     = module.sentry_lambda.sentry_key_dsn_public_output

    AR_MYSQL_HOST              = var.ar_mysql_host
    AR_MYSQL_USER              = var.ar_mysql_username
    AR_MYSQL_PORT              = 3306
    DD_MYSQL_HOST              = var.dd_mysql_host
    DD_MYSQL_USER              = var.dd_mysql_username
    DD_MYSQL_PORT              = 3306
    OPENSEARCH_HOST            = data.aws_opensearch_domain.dd_search_domain.endpoint
    OPENSEARCH_PORT            = 443
    OPENSEARCH_DEFAULT_TIMEOUT = 160 # sec
    # Without this setting, too many executions cause 5xx ReadTimeout errors from OpenSearch. This config is not there for ES lambda.
    OPENSEARCH_POOL_MAXSIZE = 15
  }
}
