locals {
  reindex_lambda_name = "${var.service_name}-reindex-product-metadata"
}

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

  environment        = var.environment
  application_family = var.application_family
  lambda_name        = local.reindex_lambda_name

  use_container_image                            = true
  lambda_description                             = "Clear and repopulate the review queue index."
  lambda_function_timeout                        = "900"
  lambda_function_reserved_concurrent_executions = "1"
  vpc_id                                         = var.vpc_id
  vpc_subnet_ids                                 = var.vpc_subnet_ids
  datadog_enabled                                = true
  datadog_advanced_enabled                       = true
  additional_tags                                = var.additional_tags

  iam_managed_policy_attachments = [
    aws_iam_policy.content_review_index_update_queue_policy.arn,
    data.aws_iam_policy.opensearch_rw_policy.arn
  ]

  lambda_function_environment_variables = {
    ENVIRONMENT                   = var.environment
    GRAPHQL_GATEWAY_URL           = var.graphql_gateway_url
    SENTRY_DSN                    = module.sentry_reindex_product_metadata.sentry_key_dsn_public_output
    SQS_QUEUE_URL                 = data.aws_sqs_queue.content_review_index_update_queue.url
    OPENSEARCH_ENDPOINT           = var.os_domain_endpoint
    OPENSEARCH_PORT               = var.os_domain_port
    OPENSEARCH_INDEX_ALIAS_WRITE  = var.opensearch_index_write_alias
    OPENSEARCH_INDEX_ALIAS_READ   = var.opensearch_index_read_alias
  }

  # Added to fix datadog logging until ddtrace version can be upgraded
  datadog_advanced_merge_xray_traces_enabled = false
}

# stage datadog creds and create dashboards.
module "datadog_lambda_content_reindex_product_metadata" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.9"

  environment  = var.environment
  service_name = local.reindex_lambda_name

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}

module "sentry_reindex_product_metadata" {
  source = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"

  environment        = var.environment
  service_name       = local.reindex_lambda_name
  application_family = var.application_family
}
