data "terraform_remote_state" "shared" {
  backend = "s3"
  config = {
    bucket = "assessment-model-terraform-state"
    key    = "env:/${var.stage}/terraform.tfstate"
    region = "eu-west-2"
  }
}

data "aws_secretsmanager_secret_version" "db_credentials" {
  secret_id = "${var.stage}/assessment_model/db_credentials"
}

locals {
  db_credentials = jsondecode(data.aws_secretsmanager_secret_version.db_credentials.secret_string)
}

module "lambda" {
  source = "../../modules/lambda_with_sqs"

  name  = "bulk-address2uprn-combiner"
  stage = var.stage

  image_uri = local.image_uri

  timeout     = 900
  memory_size = 2048

  maximum_concurrency = var.maximum_concurrency
  batch_size          = var.batch_size

  environment = {
    STAGE          = var.stage
    LOG_LEVEL      = "info"
    S3_BUCKET_NAME = data.terraform_remote_state.shared.outputs.retrofit_sap_data_bucket_name
    DB_USERNAME    = local.db_credentials.db_assessment_model_username
    DB_PASSWORD    = local.db_credentials.db_assessment_model_password
  }
}

resource "aws_iam_role_policy_attachment" "bulk_address2uprn_combiner_s3" {
  role       = module.lambda.role_name
  policy_arn = data.terraform_remote_state.shared.outputs.bulk_address2uprn_combiner_s3_arn
}
