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

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

resource "aws_security_group" "lambda_send_sme_ack_security_group" {
  name        = "${var.environment}-send-sme-ack-${var.service_name}-security-group"
  description = "Security group for ${var.service_name}-send-sme-ack-${var.environment} in ${var.swb_vpc_id}"
  vpc_id      = var.swb_vpc_id

  tags = {
    environment  = var.environment
    terraformed  = true
    name         = "${var.environment}-send-sme-ack-${var.service_name}-security-group"
    service_name = var.service_name
  }
}

resource "aws_security_group_rule" "allow_egress_for_lambda_send_sme_ack" {
  type              = "egress"
  from_port         = 0
  to_port           = 0
  protocol          = "-1"
  cidr_blocks       = ["0.0.0.0/0"]
  security_group_id = aws_security_group.lambda_send_sme_ack_security_group.id
}


module "lambda_send_sme_ack" {
  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}-send-sme-ack-${var.environment}"
  lambda_name              = "${var.service_name}-send-sme-ack"
  lambda_description       = "Send SME acknowledgement XML"
  use_container_image      = true
  datadog_advanced_enabled = true

  lambda_function_environment_variables = {
    Environment             = var.environment
    LOGGER_DSN              = var.logger_dsn
    SENTRY_DSN              = module.sentry_send_sme_ack_lambda.sentry_key_dsn_public_output
    SEND_SME_ACK            = false
    SWITCHBOARD_ROLE_ARN    = var.swb_role_arn
    SWITCHBOARD_SECRET_ARN  = var.swb_secret_arn
    SWITCHBOARD_GRAPHQL_URL = var.swb_graphql_url
  }

  lambda_function_timeout                        = var.lambda_default_timeout
  vpc_id                                         = var.swb_vpc_id
  vpc_subnet_ids                                 = var.swb_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/qa-ddex-ingester-common-lambda-policy",
    "arn:aws:iam::437795906767:policy/qa-switchboard-cross-account-assume-role-policy",
  ]
}

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

  environment        = var.environment
  service_name       = "${var.service_name}-send-sme-ack"
  application_family = var.application_family
}

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

  environment                       = var.environment
  service_name                      = "${var.service_name}-send-sme-ack"
  lambda_invocation_monitor_enabled = false
}

module "secrets" {
  source       = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each     = toset(var.secrets_manager_secret_names)
  environment  = var.environment
  service_name = "${var.service_name}-send-sme-ack"

  secret_name        = each.value
  application_family = var.application_family
}
