# 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-validate-product/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_validate_product" {
  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}-validate-product-${var.environment}"
  lambda_name              = "${var.service_name}-validate-product"
  lambda_description       = "Validate product"
  use_container_image      = true
  datadog_advanced_enabled = true

  lambda_function_environment_variables = {
    Environment         = var.environment
    LOGGER_DSN          = var.logger_dsn
    SENTRY_DSN          = module.sentry_validate_product_lambda.sentry_key_dsn_public_output
    OA_USER             = var.oa_user
    GRAPHQL_GATEWAY_URL = var.graphql_gateway_url
    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 "datadog_validate_product_lambda" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.16.1"

  environment                       = var.environment
  service_name                      = "${var.service_name}-validate-product"
  lambda_invocation_monitor_enabled = false
}

module "sentry_validate_product_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}-validate-product"
}
