# Provider determines the method (AWS) and some basic provider properties
provider "aws" {
  region = var.region
}

# Caller Identity
data "aws_caller_identity" "current" {}

# Terraform backends cannot contain interpolations - of the form ${var.environment}-${var.service_name}-state
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ddex-ingester/lambda-write-catalog-ingestion/terraform.tfstate"
    region  = "us-east-1"
    encrypt = true
  }
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git//?ref=3.1.0"

  environment = var.environment
}

data "aws_rds_cluster" "ddex_ingster_rds" {
  cluster_identifier = "prod-ddex-ingester"
}

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

  environment              = var.environment
  application_family       = var.application_family
  use_custom_function_name = true
  custom_function_name     = "${var.service_name}-write-catalog-ingestion-${var.environment}"
  lambda_name              = "${var.service_name}-write-catalog-ingestion"
  lambda_description       = "Handles writing catalog ingestion data into an S3 location for Snowpipe to ingest"
  use_container_image      = true
  datadog_advanced_enabled = true

  lambda_function_environment_variables = {
    Environment = var.environment
    LOGGER_DSN  = var.logger_dsn
    SENTRY_DSN  = module.sentry_write_catalog_ingestion_lambda.sentry_key_dsn_public_output
    OA_USER     = var.oa_user
    RDS_HOST    = data.aws_rds_cluster.ddex_ingster_rds.endpoint
    RDS_DB_NAME = data.aws_rds_cluster.ddex_ingster_rds.database_name
    RDS_RW_USER = var.rds_rw_user
  }

  lambda_function_timeout                        = var.lambda_default_timeout
  vpc_id                                         = var.vpc_id
  vpc_subnet_ids                                 = var.vpc_subnet_ids
  lambda_function_reserved_concurrent_executions = var.lambda_concurrent_executions
  datadog_enabled                                = var.datadog_enabled
  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/prod-ddex-ingester-common-lambda-policy",
    "arn:aws:iam::437795906767:policy/prod-ddex-ingester-rds-rw-password-secrets-policy"
  ]
}

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

  environment        = var.environment
  application_family = var.application_family
  service_name       = "${var.service_name}-write-catalog-ingestion"
}

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

  environment                       = var.environment
  service_name                      = "${var.service_name}-write-catalog-ingestion"
  lambda_invocation_monitor_enabled = false
}

resource "datadog_monitor" "lambda_write_catalog_sme_analytics_ingestion_errors" {
  name               = "DDEX sme_analytics status monitor"
  type               = "log alert"
  message            = <<EOT
SME_ANALYTICS_PROVIDER DDEX ingestion failed
Notify: @slack-data-alerts
EOT
  escalation_message = "SME_ANALYTICS_PROVIDER DDEX ingestion failed Notify: @slack-data-alerts"

  query = "logs(\"service:ddex-ingester-write-catalog-ingestion environment:prod DDEX_PROVIDER\\: SME_ANALYTICS_PROVIDER STATUS Failure -Writing\").index(\"*\").rollup(\"count\").last(\"10m\") > 1"

  monitor_thresholds {
    critical          = 1
    warning           = 0
    warning_recovery  = 0
    critical_recovery = 0
  }

  enable_logs_sample  = true
  include_tags        = true
  notify_no_data      = false
  notify_audit        = false
  renotify_interval   = 15
  renotify_statuses   = ["alert"]
  timeout_h           = 1
  require_full_window = false

  priority = 2

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}",
    "application_family:data-platform",
  ]
}
