provider "aws" {
  region = var.aws_region
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/documents/calculate-payments/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

data "aws_vpc" "main" {
  tags = {
    Name = "prod"
  }
}

data "aws_subnets" "private" {
  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.main.id]
  }

  tags = {
    Name = "private0*"
  }
}


#################################
#
# lambda configuration
#
##################################
module "lambda_documents_calculate_payments" {
  source                   = "git@github.com:theorchard/terraform-lambda.git?ref=3.1.4"
  environment              = var.environment
  lambda_name              = var.service_name
  lambda_description       = "Calculate Mass Payout payments with related taxes"
  datadog_enabled          = var.datadog_enabled
  datadog_advanced_enabled = var.datadog_advanced_enabled

  application_family       = var.application_family

  lambda_function_provisioned_concurrent_executions = var.lambda_provisioned_concurrent_executions
  lambda_function_timeout                           = "900"
  lambda_function_memory_size                       = 1024
  use_container_image                               = true

  vpc_enabled             = true
  vpc_id                  = data.aws_vpc.main.id
  vpc_subnet_ids          = data.aws_subnets.private.ids
  zappa_s3_policy_enabled = false

  lambda_function_environment_variables = {
    ENVIRONMENT = var.environment
  }

  iam_managed_policy_attachments = [
    module.lambda_documents_calculate_payments_owsrequest.policy_arn_output
  ]

}

module "lambda_documents_calculate_payments_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git?ref=1.0.1"

  environment_name = var.environment
  service_name     = var.service_name
}

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

  environment                       = var.environment
  service_name                      = var.service_name
  lambda_invocation_monitor_enabled = false

  notification_endpoints            = "@slack-The_Orchard-documents-public"
  escalation_notification_endpoints = "@slack-The_Orchard-documents-public"
}

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

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

data "aws_iam_policy_document" "lambda_documents_calculate_payments_invoke_policy_document" {
  statement {
    effect = "Allow"
    actions = [
      "lambda:InvokeFunction"
    ]
    resources = [
      module.lambda_documents_calculate_payments.lambda_arn
    ]
  }
}

resource "aws_iam_policy" "lambda_documents_calculate_payments_invoke_policy" {
  name        = "${var.environment}-${var.service_name}-invoke-policy"
  description = "Policy for invoking ${var.service_name} lamdba"
  policy      = data.aws_iam_policy_document.lambda_documents_calculate_payments_invoke_policy_document.json
}
