Skip to content

Commit

Permalink
Merge pull request #4 from co-cddo/lambda-monitoring
Browse files Browse the repository at this point in the history
Add basic lambda monitoring
  • Loading branch information
paulhhallam authored Dec 16, 2024
2 parents 476620b + cd66c62 commit d246b7b
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.0.1
v1.2.0
58 changes: 58 additions & 0 deletions lambda-monitoring/L-README.md
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]/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 = "[email protected]""
}
output "cloudwatch_alarm_arn" {
value = module.lambda_monitoring."[email protected]"
}
```

### 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
30 changes: 30 additions & 0 deletions lambda-monitoring/Lambda_monitoring_alerting.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

resource "aws_cloudwatch_metric_alarm" "lambda_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.period
statistic = var.statistic
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
treat_missing_data = var.treat_missing_data
alarm_description = var.alarm_description
dimensions = {
FunctionName = var.lambda_name
}
}

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
}


58 changes: 58 additions & 0 deletions lambda-monitoring/README.md
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]/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 = "[email protected]""
}
output "cloudwatch_alarm_arn" {
value = module.lambda_monitoring."[email protected]"
}
```

### 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.
- **`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

- **`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

- **`cloudwatch_alarm_arn`** : arn of the aws cloudwatch alarm
4 changes: 4 additions & 0 deletions lambda-monitoring/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "cloudwatch_alarm_arn" {
description = "The ARN of the CloudWatch alarm"
value = aws_cloudwatch_metric_alarm.lambda_error_alarm.arn
}
54 changes: 54 additions & 0 deletions lambda-monitoring/variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 4 additions & 1 deletion release-notes.md
Original file line number Diff line number Diff line change
@@ -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
- Module created

0 comments on commit d246b7b

Please sign in to comment.