Skip to content

Commit 9a6ab1c

Browse files
committed
[feature/PI-550-billing] billing alert
1 parent eace681 commit 9a6ab1c

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

infrastructure/terraform/per_account/dev/main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,16 @@ module "vpc" {
101101
resource "aws_route53_zone" "dev-ns" {
102102
name = "api.cpm.dev.national.nhs.uk"
103103
}
104+
105+
module "billing-alert" {
106+
source = "../modules/billing-alert"
107+
prefix = "${local.project}--${terraform.workspace}"
108+
metric_name = "EstimatedCharges"
109+
metric_statistic = "Maximum"
110+
metric_number_of_evaluation_periods = 1
111+
threshold_dollars = 20
112+
recipients = [] # get from secrets
113+
tags = {
114+
Name = "${local.project}--${replace(terraform.workspace, "_", "-")}"
115+
}
116+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
resource "aws_cloudwatch_metric_alarm" "account_billing_alarm" {
2+
alarm_name = "${var.prefix}--billing-alarm--${var.threshold_dollars}--${var.metric_name}"
3+
alarm_description = "Billing Alarm of ${var.threshold_dollars} USD (${var.metric_name})"
4+
namespace = "AWS/Billing"
5+
tags = var.tags
6+
7+
# If statistic(metric) >= threshold in dollars then trigger topic
8+
metric_name = var.metric_name
9+
comparison_operator = "GreaterThanOrEqualToThreshold"
10+
threshold = var.threshold_dollars
11+
alarm_actions = [aws_sns_topic.sns_alert_topic.arn]
12+
13+
# Evaluate a new statistic(metric) every 6 hours
14+
period = 6 * 60 * 60 # seconds
15+
16+
# Calculate statistic(metric) over the specified number evaluation periods
17+
statistic = var.metric_statistic
18+
evaluation_periods = var.metric_number_of_evaluation_periods
19+
datapoints_to_alarm = 1
20+
}
21+
22+
23+
resource "aws_sns_topic" "sns_alert_topic" {
24+
name = "${var.prefix}--billing-alarm-${var.threshold_dollars}--${var.metric_name}"
25+
tags = var.tags
26+
}
27+
28+
resource "aws_sns_topic_subscription" "email_target" {
29+
count = length(var.recipients)
30+
topic_arn = aws_sns_topic.sns_alert_topic.arn
31+
protocol = "email"
32+
endpoint = var.recipients[count.index]
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
variable "prefix" {
2+
type = string
3+
}
4+
5+
variable "threshold_dollars" {
6+
type = number
7+
}
8+
variable "recipients" {
9+
type = list(string)
10+
}
11+
12+
variable "metric_name" {
13+
type = string
14+
}
15+
16+
variable "metric_number_of_evaluation_periods" {
17+
type = number
18+
}
19+
20+
variable "metric_statistic" {
21+
type = string
22+
}
23+
24+
variable "tags" {
25+
26+
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ urllib3 = "<3"
2727
orjson = "^3.9.15"
2828
attrs = "^24.2.0"
2929
locust = "^2.29.1"
30+
gevent = "<24.10.2"
3031

3132
[tool.poetry.group.dev.dependencies]
3233
pre-commit = "^4.0.0"

scripts/infrastructure/policies/deployment2-policy.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@
256256
"acm:ListCertificates",
257257
"resource-groups:ListGroups",
258258
"lambda:ListEventSourceMappings",
259-
"iam:ListRoles"
259+
"iam:ListRoles",
260+
"sns:CreateTopic",
261+
"sns:TagResource",
262+
"sns:SetTopicAttributes",
263+
"sns:GetTopicAttributes"
260264
],
261265
"Resource": ["*"]
262266
}

0 commit comments

Comments
 (0)