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

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

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


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

  name  = "condition-etl"
  stage = var.stage

  image_uri = local.image_uri


  environment = merge(
    {
      STAGE     = var.stage
      LOG_LEVEL = "info"
      DB_USERNAME = local.db_credentials.db_assessment_model_username
      DB_PASSWORD = local.db_credentials.db_assessment_model_password
    },
  )

}

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