locals {
  deindex_lambda_name = "${var.lambda_name_prefix}-deindex-product-metadata"
}

data "aws_iam_policy" "deindex_open_search_rw_policy" {
  name = "Elasticsearch-${var.opensearch_domain_name}-RW-policy"
}

module "lambda_content_deindex_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.deindex_lambda_name

  use_container_image      = true
  lambda_description       = "Removes products from the content review queue."
  lambda_function_timeout  = "600"
  vpc_enabled              = true
  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 = [
    data.aws_iam_policy.deindex_open_search_rw_policy.arn,
  ]

  lambda_function_environment_variables = {
    ENVIRONMENT            = var.environment
    SENTRY_DSN             = module.sentry_deindex_product_metadata.sentry_key_dsn_public_output
    OPENSEARCH_ENDPOINT    = var.opensearch_endpoint
    OPENSEARCH_PORT        = var.opensearch_port
    OPENSEARCH_INDEX_ALIAS = var.opensearch_index_alias
  }
}

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

  environment                       = var.environment
  service_name                      = local.deindex_lambda_name
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
  lambda_error_monitor_enabled      = false
  lambda_invocation_monitor_enabled = false
  lambda_throttle_monitor_enabled   = false
}

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

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