provider "aws" {
  region = var.aws_region
}

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

locals {
  lambda_es_documents_products_name = "${var.service_name}-products"
}

# the lambda(s) will use container image(s) stored in an ecr repo
resource "aws_ecr_repository" "lambda_es_documents_products_lambda_ecr_repo" {
  name = var.service_name

  encryption_configuration {
    encryption_type = "KMS"
  }

  image_tag_mutability = "MUTABLE"
  image_scanning_configuration {
    scan_on_push = true
  }

  tags = {
    environment        = var.environment
    service_name       = local.lambda_es_documents_products_name
    application_family = var.application_family
    terraformed        = true
  }
}

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

  environment        = var.environment
  lambda_name        = local.lambda_es_documents_products_name
  lambda_description = "Maps to events on release-related cdc topics, so we can sync product documents to the products elasticsearch"
  application_family = var.application_family
  vpc_id             = var.vpc_id
  vpc_subnet_ids     = var.vpc_subnet_ids

  lambda_function_memory_size = 720

  use_container_image = true

  // The lambda will be triggered by events on the respective kafka cdc topics
  msk_event_enabled                     = true
  msk_topics                            = var.products_msk_topics
  event_source_mapping_msk_cluster_name = var.trigger_msk_cluster_name
  event_source_mapping_msk_cluster_uuid = var.trigger_msk_cluster_uuid

  datadog_advanced_enabled = true

  lambda_function_environment_variables = {
    Environment           = var.environment
    SCHEMA_REGISTRY_URL   = var.schema_registry_url
    PRODUCT_DOCS_ES_TOPIC = var.product_docs_es_topic
  }
}

// lambda monitoring
module "lambda_es_documents_products_dashboards_and_monitors" {
  source       = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=4.19.1"
  service_name = local.lambda_es_documents_products_name
  environment  = var.environment
}
