# Preference Center subscription updates lambda.

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

  application_family = var.application_family
  environment        = var.environment
  platform           = "python"
  service_name       = var.lambda_subscription_preference_sforce_updates_name
  teams              = [var.environment]
}

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

  application_family       = var.application_family
  datadog_enabled          = true
  datadog_advanced_enabled = true
  environment              = var.environment
  lambda_name              = var.lambda_subscription_preference_sforce_updates_name
  lambda_description       = "Lambda for handling Preference Center subscription updates sync into Sforce."
  use_container_image      = true
  vpc_id                   = module.vpc_info.vpc_id
  vpc_subnet_ids           = module.vpc_info.default_private_subnet_ids
  lambda_function_timeout  = 900

  // The lambda will be triggered by MSK events from Update and Delete topics
  msk_event_enabled                      = true
  event_source_mapping_batch_size        = 1
  msk_topics                             = [var.subscription_event_updates_topic_name]
  event_source_mapping_msk_cluster_name  = "${var.environment}-${var.msk_cluster_name}"
  event_source_mapping_msk_cluster_uuid  = data.aws_msk_cluster.kafka_fansifter.cluster_uuid
  event_source_mapping_starting_position = "LATEST"
  event_source_mapping_failure_arn       = module.failure_sns_topic.topic_arn

  lambda_function_reserved_concurrent_executions = "12"

  datadog_function_destination_lambda_name = "DatadogLambdaFunction"
  iam_managed_policy_attachments = [
    aws_iam_policy.failure_destination_policy.arn
  ]

  lambda_function_environment_variables = {
    SENTRY_DSN               = module.lambda_subscription_preference_sforce_updates_sentry_project.sentry_key_dsn_public_output
    ENVIRONMENT              = var.environment
    KAFKA_TARGET_TOPIC       = var.subscription_sforce_sink_topic_name
    KAFKA_BOOTSTRAP_SERVERS  = data.aws_msk_cluster.kafka_fansifter.bootstrap_brokers_tls
    KAFKA_SECURITY_PROTOCOL  = "SSL"
    KAFKA_MESSAGE_HEADER_KEY = "CamelHeader.sObjectIdValue"
  }
}

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

  environment  = var.environment
  service_name = var.lambda_subscription_preference_sforce_updates_name
  teams        = ["kdh", "fansifter"]

  notification_endpoints = "@slack-kafka-data-highway-alerts"
}
