|
| 1 | +# SNS topic that ops alarms publish to. A small Lambda forwards each |
| 2 | +# message to the Discord WH_URL webhook so notifications land in the |
| 3 | +# same channel as package-processed notices. |
| 4 | +resource "aws_sns_topic" "alerts" { |
| 5 | + name = "${local.name}-alerts" |
| 6 | +} |
| 7 | + |
| 8 | +# Anything in the DLQ means a worker invocation failed twice in a row |
| 9 | +# (process_package raised an unhandled exception or the Lambda timed out |
| 10 | +# at the 15-min cap). Always worth a look. |
| 11 | +resource "aws_cloudwatch_metric_alarm" "worker_dlq_depth" { |
| 12 | + alarm_name = "${local.name}-worker-dlq-not-empty" |
| 13 | + alarm_description = "A worker invocation failed permanently and the SQS message landed in the DLQ. Check /aws/lambda/${aws_lambda_function.worker.function_name} for the traceback." |
| 14 | + comparison_operator = "GreaterThanThreshold" |
| 15 | + evaluation_periods = 1 |
| 16 | + metric_name = "ApproximateNumberOfMessagesVisible" |
| 17 | + namespace = "AWS/SQS" |
| 18 | + period = 300 |
| 19 | + statistic = "Maximum" |
| 20 | + threshold = 0 |
| 21 | + treat_missing_data = "notBreaching" |
| 22 | + |
| 23 | + dimensions = { |
| 24 | + QueueName = aws_sqs_queue.dlq.name |
| 25 | + } |
| 26 | + |
| 27 | + alarm_actions = [aws_sns_topic.alerts.arn] |
| 28 | + ok_actions = [aws_sns_topic.alerts.arn] |
| 29 | +} |
| 30 | + |
| 31 | +# --- Discord notifier Lambda --- |
| 32 | + |
| 33 | +data "archive_file" "notifier" { |
| 34 | + type = "zip" |
| 35 | + source_file = "${path.module}/notifier/notifier.py" |
| 36 | + output_path = "${path.module}/notifier/notifier.zip" |
| 37 | +} |
| 38 | + |
| 39 | +# Pulls the latest WH_URL out of Secrets Manager at apply time so the |
| 40 | +# notifier doesn't need its own runtime SM read. |
| 41 | +data "aws_secretsmanager_secret_version" "wh_url_current" { |
| 42 | + secret_id = aws_secretsmanager_secret.wh_url.id |
| 43 | +} |
| 44 | + |
| 45 | +resource "aws_iam_role" "notifier" { |
| 46 | + name = "${local.name}-notifier-lambda" |
| 47 | + assume_role_policy = data.aws_iam_policy_document.lambda_assume.json |
| 48 | +} |
| 49 | + |
| 50 | +resource "aws_iam_role_policy_attachment" "notifier_basic" { |
| 51 | + role = aws_iam_role.notifier.name |
| 52 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 53 | +} |
| 54 | + |
| 55 | +resource "aws_cloudwatch_log_group" "notifier" { |
| 56 | + name = "/aws/lambda/${local.name}-alert-notifier" |
| 57 | + retention_in_days = 30 |
| 58 | +} |
| 59 | + |
| 60 | +resource "aws_lambda_function" "notifier" { |
| 61 | + function_name = "${local.name}-alert-notifier" |
| 62 | + role = aws_iam_role.notifier.arn |
| 63 | + filename = data.archive_file.notifier.output_path |
| 64 | + source_code_hash = data.archive_file.notifier.output_base64sha256 |
| 65 | + handler = "notifier.handler" |
| 66 | + runtime = "python3.13" |
| 67 | + timeout = 30 |
| 68 | + memory_size = 128 |
| 69 | + |
| 70 | + environment { |
| 71 | + variables = { |
| 72 | + WH_URL = data.aws_secretsmanager_secret_version.wh_url_current.secret_string |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + depends_on = [ |
| 77 | + aws_iam_role_policy_attachment.notifier_basic, |
| 78 | + aws_cloudwatch_log_group.notifier, |
| 79 | + ] |
| 80 | +} |
| 81 | + |
| 82 | +resource "aws_lambda_permission" "sns_invoke_notifier" { |
| 83 | + statement_id = "AllowSNSInvoke" |
| 84 | + action = "lambda:InvokeFunction" |
| 85 | + function_name = aws_lambda_function.notifier.function_name |
| 86 | + principal = "sns.amazonaws.com" |
| 87 | + source_arn = aws_sns_topic.alerts.arn |
| 88 | +} |
| 89 | + |
| 90 | +resource "aws_sns_topic_subscription" "notifier" { |
| 91 | + topic_arn = aws_sns_topic.alerts.arn |
| 92 | + protocol = "lambda" |
| 93 | + endpoint = aws_lambda_function.notifier.arn |
| 94 | +} |
0 commit comments