From 311a72467f987c57b3ecc336904d222c9cf6de2e Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Dec 2024 09:54:29 +0000 Subject: [PATCH 1/8] added lambda monitoring --- .../Lambda_monitoring_alerting.tf | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 lambda-monitoring/Lambda_monitoring_alerting.tf diff --git a/lambda-monitoring/Lambda_monitoring_alerting.tf b/lambda-monitoring/Lambda_monitoring_alerting.tf new file mode 100644 index 0000000..c625675 --- /dev/null +++ b/lambda-monitoring/Lambda_monitoring_alerting.tf @@ -0,0 +1,98 @@ +variable "lambda_name" { + type = string + default = "" +} +variable "alarm_threshold" { + type = number + default = 3 +} +variable "alarm_period" { + type = number + default = 300 +} +variable "evaluation_periods" { + type = number + default = 1 +} +variable "statistic" { + type = string + default = "Sum" +} +variable "alarm_actions" { + type = string + default = "" +} +variable "ok_actions" { + type = string + default = "" +} +variable "treat_missing_data" { + type = string + default = "notBreaching" +} +variable "insufficient_data_actions" { + type = list(string) + default = [] +} +variable "alarm_description" { + type = string + default = "Alarm for Lambda function errors exceeding threshold" +} +variable "comparison_operator" { + type = string + default = "GreaterThanOrEqualToThreshold" +} +variable "metric_name" { + type = string + default = "Lambda-Errors" +} +variable "namespace" { + type = string + default = "AWS/Lambda" +} + +resource "aws_cloudwatch_metric_alarm" "lambda_error_alarm" { + alarm_name = "${var.lambda_name}-Error-Alarm" + comparison_operator = var.comparison_operator + evaluation_periods = var.evaluation_periods + metric_name = var.metric_name + namespace = var.namespace + period = var.alarm_period + statistic = var.statistic + threshold = var.alarm_threshold + alarm_actions = [aws_sns_topic.lambda_alarm_topic.arn] + ok_actions = [aws_sns_topic.lambda_alarm_topic.arn] + insufficient_data_actions = var.insufficient_data_actions + treat_missing_data = var.treat_missing_data + alarm_description = var.alarm_description + dimensions = { + FunctionName = var.lambda_name + } +} + +variable "topic_subscription" { + type = string + default = "lambda-error-notifications" +} +variable "alarm_name" { + type = string + default = "" +} +variable "protocol" { + type = string + default = "email" +} + +resource "aws_sns_topic" "lambda_alarm_topic" { + name = var.alarm_name +} +resource "aws_sns_topic_subscription" "lambda_alarm_subscription" { + topic_arn = aws_sns_topic.lambda_alarm_topic.arn + protocol = var.protocol + endpoint = var.topic_subscription +} + +output "cloudwatch_alarm_arn" { + description = "The ARN of the CloudWatch alarm" + value = aws_cloudwatch_metric_alarm.lambda_error_alarm.arn +} From bffa415173aeb5236c5aa1412705a1f44c471959 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Dec 2024 14:51:31 +0000 Subject: [PATCH 2/8] add the lambda monitoring module code and documentation --- .../Lambda_monitoring_alerting.tf | 76 +------------------ lambda-monitoring/README.md | 58 ++++++++++++++ lambda-monitoring/outputs.tf | 4 + lambda-monitoring/variables.tf | 54 +++++++++++++ 4 files changed, 120 insertions(+), 72 deletions(-) create mode 100644 lambda-monitoring/README.md create mode 100644 lambda-monitoring/outputs.tf create mode 100644 lambda-monitoring/variables.tf diff --git a/lambda-monitoring/Lambda_monitoring_alerting.tf b/lambda-monitoring/Lambda_monitoring_alerting.tf index c625675..e352773 100644 --- a/lambda-monitoring/Lambda_monitoring_alerting.tf +++ b/lambda-monitoring/Lambda_monitoring_alerting.tf @@ -1,65 +1,13 @@ -variable "lambda_name" { - type = string - default = "" -} -variable "alarm_threshold" { - type = number - default = 3 -} -variable "alarm_period" { - type = number - default = 300 -} -variable "evaluation_periods" { - type = number - default = 1 -} -variable "statistic" { - type = string - default = "Sum" -} -variable "alarm_actions" { - type = string - default = "" -} -variable "ok_actions" { - type = string - default = "" -} -variable "treat_missing_data" { - type = string - default = "notBreaching" -} -variable "insufficient_data_actions" { - type = list(string) - default = [] -} -variable "alarm_description" { - type = string - default = "Alarm for Lambda function errors exceeding threshold" -} -variable "comparison_operator" { - type = string - default = "GreaterThanOrEqualToThreshold" -} -variable "metric_name" { - type = string - default = "Lambda-Errors" -} -variable "namespace" { - type = string - default = "AWS/Lambda" -} resource "aws_cloudwatch_metric_alarm" "lambda_error_alarm" { - alarm_name = "${var.lambda_name}-Error-Alarm" + alarm_name = var.alarm_name comparison_operator = var.comparison_operator evaluation_periods = var.evaluation_periods metric_name = var.metric_name namespace = var.namespace - period = var.alarm_period + period = var.period statistic = var.statistic - threshold = var.alarm_threshold + threshold = var.threshold alarm_actions = [aws_sns_topic.lambda_alarm_topic.arn] ok_actions = [aws_sns_topic.lambda_alarm_topic.arn] insufficient_data_actions = var.insufficient_data_actions @@ -70,19 +18,6 @@ resource "aws_cloudwatch_metric_alarm" "lambda_error_alarm" { } } -variable "topic_subscription" { - type = string - default = "lambda-error-notifications" -} -variable "alarm_name" { - type = string - default = "" -} -variable "protocol" { - type = string - default = "email" -} - resource "aws_sns_topic" "lambda_alarm_topic" { name = var.alarm_name } @@ -92,7 +27,4 @@ resource "aws_sns_topic_subscription" "lambda_alarm_subscription" { endpoint = var.topic_subscription } -output "cloudwatch_alarm_arn" { - description = "The ARN of the CloudWatch alarm" - value = aws_cloudwatch_metric_alarm.lambda_error_alarm.arn -} + diff --git a/lambda-monitoring/README.md b/lambda-monitoring/README.md new file mode 100644 index 0000000..c3c4bf5 --- /dev/null +++ b/lambda-monitoring/README.md @@ -0,0 +1,58 @@ +# Lambda Monitoring Module + +This Terraform module, **lambda-monitoring**, is designed to set up AWS monitoring and reporting for a lambda mfunction. It includes functionality for lambda and CloudWatch alarms. + +## Features + +- **SNS Alerts**: A single SNS topic that sends notifications to a specified email when health checks fail. +- **CloudWatch Alarms**: Monitors health check statuses and triggers alerts via SNS. + +## Usage + +### Example Usage + +```hcl + +locals { + lambda_name = "lambda-dev" +} +module "lambda_monitoring" { + source = "git@github.com/co-cddo/gc3-terraform-module-monitoring.git//lambda-monitoring:lambda-monitoring?ref=v1.0.0" + lambda_name = local.lambda_name + statistic = "Sum" + alarm_description = "Alarm for Lambda function errors exceeding threshold" + alarm_name = "${local.lambda_name}-Lambda-Alarm" + topic_subscription = "alerting@example.com"" +} + + +output "cloudwatch_alarm_arn" { + value = module.lambda_monitoring."alerting@example.com" +} + +``` + +### Required Variables + +- **`lambda_name`** (string): Required : A unique name for the alarm. +- **`alarm_name`** (string): Required: The descriptive name for the alarm. This name must be unique within the user's AWS account. Automatically appended with "-Error-Alarm" +- **`metric_name`** (string): Required : The name for the alarm's associated metric. + +### Optional Variables + +- **`comparison_operator`** (string): Default **GreaterThanOrEqualToThreshold** : The arithmetic operation to use when comparing the specified Statistic and Threshold. +- **`evaluation_periods`** (number): Default **1** : The number of periods over which data is compared to the specified threshold. +- **`namespace`** (string): Default **AWS/Lambda** : The namespace for the alarm's associated metric. +- **`period`** (string): Default **3** : The period in seconds over which the specified statistic is applied. Valid values are 10, 30, or any multiple of 60 +- **`statistic`** (string): Default **Sum** : The statistic to apply to the alarm's associated metric. Either of the following is supported: SampleCount, Average, Sum, Minimum, Maximum +- **`threshold`** (number): Default **3** : The value against which the specified statistic is compared. This parameter is required for alarms based on static thresholds. +- **`insufficient_data_actions`** (list(string)): Default **[]** : (Optional) The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state from any other state. +- **`treat_missing_data`** (string): (Optional): Default **missing** : Sets how this alarm is to handle missing data points. +- **`alarm_description`** (string): (Optional): Default **Alarm for Lambda function errors exceeding threshold** : + +- **`protocol`** (string): Default **email** : Protocol to use. +- **`topic_subscription`** (string): Required : Endpoint to send data to; for email this is the email address. + +### Outputs + +- **`cloudwatch_alarm_arn`** : arn of the aws cloudwatch alarm diff --git a/lambda-monitoring/outputs.tf b/lambda-monitoring/outputs.tf new file mode 100644 index 0000000..a03a293 --- /dev/null +++ b/lambda-monitoring/outputs.tf @@ -0,0 +1,4 @@ +output "cloudwatch_alarm_arn" { + description = "The ARN of the CloudWatch alarm" + value = aws_cloudwatch_metric_alarm.lambda_error_alarm.arn +} diff --git a/lambda-monitoring/variables.tf b/lambda-monitoring/variables.tf new file mode 100644 index 0000000..3c286bb --- /dev/null +++ b/lambda-monitoring/variables.tf @@ -0,0 +1,54 @@ +variable "lambda_name" { + type = string +} +variable "alarm_name" { + type = string +} +variable "comparison_operator" { + type = string + default = "GreaterThanOrEqualToThreshold" +} +variable "evaluation_periods" { + type = number + default = 1 +} +variable "metric_name" { + type = string +} +variable "namespace" { + type = string + default = "AWS/Lambda" +} +variable "period" { + type = number + default = 300 +} +variable "statistic" { + type = string + default = "Sum" +} +variable "threshold" { + type = number + default = 3 +} +variable "insufficient_data_actions" { + type = list(string) + default = [] +} +variable "treat_missing_data" { + type = string + default = "notBreaching" +} +variable "alarm_description" { + type = string + default = "Alarm for Lambda function errors exceeding threshold" +} + +variable "protocol" { + type = string + default = "email" +} +variable "topic_subscription" { + type = string + default = "lambda-error-notifications" +} \ No newline at end of file From 57a8b6f9a8f45538d97b674ea4994406dd59c765 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Dec 2024 14:55:44 +0000 Subject: [PATCH 3/8] Update README.md --- lambda-monitoring/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambda-monitoring/README.md b/lambda-monitoring/README.md index c3c4bf5..2f92c4b 100644 --- a/lambda-monitoring/README.md +++ b/lambda-monitoring/README.md @@ -35,7 +35,7 @@ output "cloudwatch_alarm_arn" { ### Required Variables - **`lambda_name`** (string): Required : A unique name for the alarm. -- **`alarm_name`** (string): Required: The descriptive name for the alarm. This name must be unique within the user's AWS account. Automatically appended with "-Error-Alarm" +- **`alarm_name`** (string): Required: The descriptive name for the alarm. This name must be unique within the user's AWS account. - **`metric_name`** (string): Required : The name for the alarm's associated metric. ### Optional Variables From 71e7c95e19e9d846914c9050889138060d212725 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Dec 2024 14:58:23 +0000 Subject: [PATCH 4/8] Update README.md --- lambda-monitoring/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lambda-monitoring/README.md b/lambda-monitoring/README.md index 2f92c4b..784a8fb 100644 --- a/lambda-monitoring/README.md +++ b/lambda-monitoring/README.md @@ -37,6 +37,7 @@ output "cloudwatch_alarm_arn" { - **`lambda_name`** (string): Required : A unique name for the alarm. - **`alarm_name`** (string): Required: The descriptive name for the alarm. This name must be unique within the user's AWS account. - **`metric_name`** (string): Required : The name for the alarm's associated metric. +- **`topic_subscription`** (string): Required : Endpoint to send data to. For email this is the email address. ### Optional Variables @@ -44,14 +45,13 @@ output "cloudwatch_alarm_arn" { - **`evaluation_periods`** (number): Default **1** : The number of periods over which data is compared to the specified threshold. - **`namespace`** (string): Default **AWS/Lambda** : The namespace for the alarm's associated metric. - **`period`** (string): Default **3** : The period in seconds over which the specified statistic is applied. Valid values are 10, 30, or any multiple of 60 -- **`statistic`** (string): Default **Sum** : The statistic to apply to the alarm's associated metric. Either of the following is supported: SampleCount, Average, Sum, Minimum, Maximum +- **`statistic`** (string): Default **Sum** : The statistic to apply to the alarm's associated metric. i.e. SampleCount, Average, Sum, Minimum, Maximum - **`threshold`** (number): Default **3** : The value against which the specified statistic is compared. This parameter is required for alarms based on static thresholds. -- **`insufficient_data_actions`** (list(string)): Default **[]** : (Optional) The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state from any other state. +- **`insufficient_data_actions`** (list(string)): Default **[]** : (Optional) The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state. - **`treat_missing_data`** (string): (Optional): Default **missing** : Sets how this alarm is to handle missing data points. - **`alarm_description`** (string): (Optional): Default **Alarm for Lambda function errors exceeding threshold** : - **`protocol`** (string): Default **email** : Protocol to use. -- **`topic_subscription`** (string): Required : Endpoint to send data to; for email this is the email address. ### Outputs From 40d735b68fa431783ef560d2accf868c855a545d Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Dec 2024 15:02:07 +0000 Subject: [PATCH 5/8] Update README.md --- lambda-monitoring/README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lambda-monitoring/README.md b/lambda-monitoring/README.md index 784a8fb..9c85229 100644 --- a/lambda-monitoring/README.md +++ b/lambda-monitoring/README.md @@ -41,17 +41,17 @@ output "cloudwatch_alarm_arn" { ### Optional Variables -- **`comparison_operator`** (string): Default **GreaterThanOrEqualToThreshold** : The arithmetic operation to use when comparing the specified Statistic and Threshold. -- **`evaluation_periods`** (number): Default **1** : The number of periods over which data is compared to the specified threshold. -- **`namespace`** (string): Default **AWS/Lambda** : The namespace for the alarm's associated metric. -- **`period`** (string): Default **3** : The period in seconds over which the specified statistic is applied. Valid values are 10, 30, or any multiple of 60 -- **`statistic`** (string): Default **Sum** : The statistic to apply to the alarm's associated metric. i.e. SampleCount, Average, Sum, Minimum, Maximum -- **`threshold`** (number): Default **3** : The value against which the specified statistic is compared. This parameter is required for alarms based on static thresholds. -- **`insufficient_data_actions`** (list(string)): Default **[]** : (Optional) The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state. -- **`treat_missing_data`** (string): (Optional): Default **missing** : Sets how this alarm is to handle missing data points. -- **`alarm_description`** (string): (Optional): Default **Alarm for Lambda function errors exceeding threshold** : - -- **`protocol`** (string): Default **email** : Protocol to use. +- **`comparison_operator`** (string): Def **GreaterThanOrEqualToThreshold** : The operation to use when comparing the specified Statistic and Threshold. +- **`evaluation_periods`** (number): Def **1** : The number of periods over which data is compared. +- **`namespace`** (string): Def **AWS/Lambda** : The namespace for the alarm's associated metric. +- **`period`** (string): Def **30** : The period in seconds over which the specified statistic is applied. 10, 30, or any multiple of 60 +- **`statistic`** (string): Def **Sum** : The statistic to apply to the alarm's associated metric. i.e. SampleCount, Average, Sum, Minimum, Maximum +- **`threshold`** (number): Def **3** : The value against which the specified statistic is compared. +- **`insufficient_data_actions`** (list(string)): Def **[]** : (Optional) The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state. +- **`treat_missing_data`** (string): (Optional): Def **missing** : Sets how this alarm is to handle missing data points. +- **`alarm_description`** (string): (Optional): Def **Alarm for Lambda function errors exceeding threshold** : + +- **`protocol`** (string): Def **email** : Protocol to use. ### Outputs From 1db22773472b6b3aa77cfbf09c34ffd75c56b7c2 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Dec 2024 15:05:42 +0000 Subject: [PATCH 6/8] Update release-notes.md --- release-notes.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/release-notes.md b/release-notes.md index 0daf765..95397e1 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,7 +1,10 @@ # Release Notes +## v1.2.0 +- Added lambda monitoring module + ## v1.0.1 - Added multiple endpoint support ## v1.0.0 -- Module created \ No newline at end of file +- Module created From e6d278e61c03b00268956622c6f9ce8507783921 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Dec 2024 15:06:02 +0000 Subject: [PATCH 7/8] Update VERSION --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 6a2b0ac..79127d8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.0.1 \ No newline at end of file +v1.2.0 From cd66c6238a44ccda2c9a4d7d8571c4788bf7172d Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 13 Dec 2024 11:05:29 +0000 Subject: [PATCH 8/8] updated documentation --- README.md | 14 +++++++++ lambda-monitoring/L-README.md | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 lambda-monitoring/L-README.md diff --git a/README.md b/README.md index 7333677..1c725ed 100644 --- a/README.md +++ b/README.md @@ -66,3 +66,17 @@ output "sns_topic_arn" { - **`health_check_ids`** (map): A map of health check IDs for each configured endpoint. - **`sns_topic_arn`** (string): ARN of the SNS topic used for health check alerts. + +# Lambda Monitoring Module + +This Terraform module, **lambda-monitoring**, is designed to set up AWS monitoring and reporting for a lambda mfunction. It includes functionality for lambda and CloudWatch alarms. + +## Features + +- **SNS Alerts**: A single SNS topic that sends notifications to a specified email when health checks fail. +- **CloudWatch Alarms**: Monitors health check statuses and triggers alerts via SNS. + +## Documentation + +Refer to the README.md in the lambda-monitoring folder for a detailed description. + diff --git a/lambda-monitoring/L-README.md b/lambda-monitoring/L-README.md new file mode 100644 index 0000000..c3c4bf5 --- /dev/null +++ b/lambda-monitoring/L-README.md @@ -0,0 +1,58 @@ +# Lambda Monitoring Module + +This Terraform module, **lambda-monitoring**, is designed to set up AWS monitoring and reporting for a lambda mfunction. It includes functionality for lambda and CloudWatch alarms. + +## Features + +- **SNS Alerts**: A single SNS topic that sends notifications to a specified email when health checks fail. +- **CloudWatch Alarms**: Monitors health check statuses and triggers alerts via SNS. + +## Usage + +### Example Usage + +```hcl + +locals { + lambda_name = "lambda-dev" +} +module "lambda_monitoring" { + source = "git@github.com/co-cddo/gc3-terraform-module-monitoring.git//lambda-monitoring:lambda-monitoring?ref=v1.0.0" + lambda_name = local.lambda_name + statistic = "Sum" + alarm_description = "Alarm for Lambda function errors exceeding threshold" + alarm_name = "${local.lambda_name}-Lambda-Alarm" + topic_subscription = "alerting@example.com"" +} + + +output "cloudwatch_alarm_arn" { + value = module.lambda_monitoring."alerting@example.com" +} + +``` + +### Required Variables + +- **`lambda_name`** (string): Required : A unique name for the alarm. +- **`alarm_name`** (string): Required: The descriptive name for the alarm. This name must be unique within the user's AWS account. Automatically appended with "-Error-Alarm" +- **`metric_name`** (string): Required : The name for the alarm's associated metric. + +### Optional Variables + +- **`comparison_operator`** (string): Default **GreaterThanOrEqualToThreshold** : The arithmetic operation to use when comparing the specified Statistic and Threshold. +- **`evaluation_periods`** (number): Default **1** : The number of periods over which data is compared to the specified threshold. +- **`namespace`** (string): Default **AWS/Lambda** : The namespace for the alarm's associated metric. +- **`period`** (string): Default **3** : The period in seconds over which the specified statistic is applied. Valid values are 10, 30, or any multiple of 60 +- **`statistic`** (string): Default **Sum** : The statistic to apply to the alarm's associated metric. Either of the following is supported: SampleCount, Average, Sum, Minimum, Maximum +- **`threshold`** (number): Default **3** : The value against which the specified statistic is compared. This parameter is required for alarms based on static thresholds. +- **`insufficient_data_actions`** (list(string)): Default **[]** : (Optional) The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state from any other state. +- **`treat_missing_data`** (string): (Optional): Default **missing** : Sets how this alarm is to handle missing data points. +- **`alarm_description`** (string): (Optional): Default **Alarm for Lambda function errors exceeding threshold** : + +- **`protocol`** (string): Default **email** : Protocol to use. +- **`topic_subscription`** (string): Required : Endpoint to send data to; for email this is the email address. + +### Outputs + +- **`cloudwatch_alarm_arn`** : arn of the aws cloudwatch alarm