Skip to content

Commit 6adbb5a

Browse files
authored
Merge pull request #687 from NHSDigital/feature/eema1-NRL-693-addCloudWatchAlarmForLambdaError
NRL-693 lambda error cloudwatch alarm
2 parents cf3a7d2 + d3df062 commit 6adbb5a

File tree

10 files changed

+122
-0
lines changed

10 files changed

+122
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module "lambda_errors_cloudwatch_metric_alarm_dev" {
2+
source = "../modules/lambda-errors-metric-alarm"
3+
name_prefix = "nhsd-nrlf--dev"
4+
5+
evaluation_periods = 1
6+
period = 60
7+
threshold = 1
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "aws_cloudwatch_metric_alarm" "metric_alarm" {
2+
alarm_name = "${var.name_prefix}--lambda-errors-metric-alarm"
3+
alarm_description = "This metric monitors the number of Lambda errors that have occurred"
4+
5+
alarm_actions = [aws_sns_topic.sns_topic.arn]
6+
7+
comparison_operator = "GreaterThanOrEqualToThreshold"
8+
evaluation_periods = var.evaluation_periods
9+
threshold = var.threshold
10+
unit = "Count"
11+
12+
metric_name = "Errors"
13+
namespace = "AWS/Lambda"
14+
period = var.period
15+
statistic = "Sum"
16+
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "aws_iam_policy" "lambda-errors-topic-kms-read-write" {
2+
name = "${var.name_prefix}-lambda-errors-topic-kms-read-write"
3+
description = "Encrypt and decrypt with the lambda errors sns topic kms key"
4+
policy = jsonencode({
5+
Version = "2012-10-17"
6+
Statement = [
7+
{
8+
Action = [
9+
"kms:Decrypt",
10+
"kms:DescribeKey",
11+
"kms:Encrypt",
12+
"kms:GenerateDataKey"
13+
]
14+
Effect = "Allow"
15+
Resource = [
16+
aws_kms_key.lambda-errors-topic-key.arn
17+
]
18+
}
19+
]
20+
})
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_kms_key" "lambda-errors-topic-key" {
2+
description = "Lambda errors SNS topic table KMS key"
3+
deletion_window_in_days = var.kms_deletion_window_in_days
4+
5+
}
6+
7+
resource "aws_kms_alias" "lambda-errors-topic-alias" {
8+
name = "alias/${var.name_prefix}-lambda-errors-topic-table-key"
9+
target_key_id = aws_kms_key.lambda-errors-topic-key.key_id
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "cloudwatch_metric_alarm_arn" {
2+
description = "The ARN of the Cloudwatch metric alarm."
3+
value = try(aws_cloudwatch_metric_alarm.metric_alarm.arn, "")
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "aws_secretsmanager_secret" "emails" {
2+
name = "${var.name_prefix}-emails"
3+
}
4+
5+
data "aws_secretsmanager_secret_version" "emails" {
6+
secret_id = data.aws_secretsmanager_secret.emails.id
7+
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "aws_sns_topic" "sns_topic" {
2+
name = "${var.name_prefix}--lambda-errors-sns-topic"
3+
kms_master_key_id = aws_kms_key.lambda-errors-topic-key.key_id
4+
}
5+
6+
resource "aws_sns_topic_subscription" "sns_subscription" {
7+
for_each = nonsensitive(toset(tolist(jsondecode(data.aws_secretsmanager_secret_version.emails.secret_string))))
8+
topic_arn = aws_sns_topic.sns_topic.arn
9+
protocol = "email"
10+
endpoint = sensitive(each.value)
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
variable "evaluation_periods" {
2+
description = "The number of periods over which data is compared to the specified threshold."
3+
type = number
4+
}
5+
6+
variable "threshold" {
7+
description = "The value against which the specified statistic is compared."
8+
type = number
9+
default = null
10+
}
11+
12+
variable "period" {
13+
description = "The period in seconds over which the specified statistic is applied."
14+
type = string
15+
default = null
16+
}
17+
18+
variable "name_prefix" {
19+
type = string
20+
description = "The prefix to apply to all resources in the module."
21+
}
22+
23+
variable "kms_deletion_window_in_days" {
24+
type = number
25+
description = "The duration in days after which the key is deleted after destruction of the resource."
26+
default = 7
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module "lambda_errors_cloudwatch_metric_alarm_dev" {
2+
source = "../modules/lambda-errors-metric-alarm"
3+
name_prefix = "nhsd-nrlf--prod"
4+
5+
evaluation_periods = 1
6+
period = 60
7+
threshold = 1
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module "lambda_errors_cloudwatch_metric_alarm_dev" {
2+
source = "../modules/lambda-errors-metric-alarm"
3+
name_prefix = "nhsd-nrlf--test"
4+
5+
evaluation_periods = 1
6+
period = 60
7+
threshold = 1
8+
}

0 commit comments

Comments
 (0)