data "archive_file" "this" {
  type        = "zip"
  source_dir  = "${path.module}/${local.file_name}"
  output_path = "${local.file_name}.zip"
  excludes    = ["example_config.json", "__pycache__/*"]
}

resource "aws_lambda_function" "this" {
  filename      = "${local.file_name}.zip"
  function_name = var.name
  role          = aws_iam_role.role.arn
  handler       = "main.lambda_handler"

  source_code_hash = data.archive_file.this.output_base64sha256

  runtime = "python3.10"
  timeout = var.timeout

  environment {
    variables = {
      TOPIC_ARN = aws_sns_topic.this.arn
      ENV       = var.env
      LOG_LEVEL = var.log_level
      SUBJECT   = var.subject
    }
  }

  tags = var.tags

  depends_on = [
    aws_cloudwatch_log_group.this
  ]
}
