provider "aws" {
  region = var.aws_region
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/lambda-content/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

locals {
  populate_lambda_name = "${var.service_name}-review-queue-populate"
}

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

  environment        = var.environment
  application_family = var.application_family
  lambda_name        = local.populate_lambda_name

  use_container_image                               = true
  lambda_description                                = "React to updates to art_relations.releases"
  lambda_function_timeout                           = "600"
  lambda_function_reserved_concurrent_executions    = var.replication_consumer_lambda_reserved_concurrency
  lambda_function_provisioned_concurrent_executions = var.lambda_function_provisioned_concurrent_executions
  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

  lambda_function_environment_variables = {
    ENVIRONMENT         = var.environment
    GRAPHQL_GATEWAY_URL = var.graphql_gateway_url
    SENTRY_DSN          = module.sentry_review_queue_populate.sentry_key_dsn_public_output
    SCHEMA_REGISTRY_URL = var.schema_registry_url
  }

  msk_event_enabled                      = true
  event_source_mapping_msk_cluster_name  = var.trigger_msk_cluster_name
  event_source_mapping_msk_cluster_uuid  = var.trigger_msk_cluster_uuid
  event_source_mapping_batch_size        = 1
  kafka_topics = [var.digital_product_submit_msk_topic]
  event_source_mapping_starting_position = "LATEST"

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

module "datadog_lambda_content_review_queue_populate" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.9"

  environment                       = var.environment
  service_name                      = local.populate_lambda_name
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints

  lambda_error_critical_number          = 20
  lambda_error_critical_recovery_number = 19

  lambda_error_warning_number          = 15
  lambda_error_warning_recovery_number = 14
}

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

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