# 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-detect-errors/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
}

module "lambda_detect_errors" {
  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}-detect-errors-${var.environment}"
  lambda_name              = "${var.service_name}-detect-errors"
  lambda_description       = "Detect errors from context"
  use_container_image      = true
  datadog_advanced_enabled = true

  lambda_function_environment_variables = {
    Environment = var.environment
    LOGGER_DSN  = var.logger_dsn
    SENTRY_DSN  = module.sentry_detect_errors_lambda.sentry_key_dsn_public_output
  }

  lambda_function_timeout                        = var.lambda_default_timeout
  vpc_id                                         = module.vpc_info.vpc_id
  vpc_subnet_ids                                 = ["subnet-067a6b45b079d7296", "subnet-043d235081c966332"] # temporarily hard-code subnets
  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",
  ]
}

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

  environment                       = var.environment
  service_name                      = "${var.service_name}-detect-errors"
  lambda_invocation_monitor_enabled = false
}

module "sentry_detect_errors_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}-detect-errors"
}
