Skip to content

Commit 023fe20

Browse files
committed
[feature/PI-550-billing] billing alert
1 parent 1c7b186 commit 023fe20

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
treat_missing_data = "ignore"
6+
tags = var.tags
7+
8+
# If statistic(metric) >= threshold in dollars then trigger topic
9+
metric_name = var.metric_name
10+
comparison_operator = "GreaterThanOrEqualToThreshold"
11+
threshold = var.threshold_dollars
12+
alarm_actions = [aws_sns_topic.sns_alert_topic.arn]
13+
14+
# Evaluate a new statistic(metric) every 6 hours
15+
period = 6 * 60 * 60 # seconds
16+
17+
# Calculate statistic(metric) over the specified number evaluation periods
18+
statistic = var.metric_statistic
19+
evaluation_periods = var.metric_number_of_evaluation_periods
20+
datapoints_to_alarm = 1
21+
}
22+
23+
24+
resource "aws_sns_topic" "sns_alert_topic" {
25+
name = "${var.prefix}--billing-alarm-${var.threshold_dollars}--${var.metric_name}"
26+
tags = var.tags
27+
}
28+
29+
resource "aws_sns_topic_subscription" "email_target" {
30+
count = length(var.recipients)
31+
topic_arn = aws_sns_topic.sns_alert_topic.arn
32+
protocol = "email"
33+
endpoint = var.recipients[count.index]
34+
}
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+
}

scripts/infrastructure/policies/deployment2-policy.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,28 @@
259259
"iam:ListRoles"
260260
],
261261
"Resource": ["*"]
262+
},
263+
{
264+
"Sid": "BillingAlertPermissions",
265+
"Effect": "Allow",
266+
"Action": [
267+
"sns:CreateTopic",
268+
"sns:TagResource",
269+
"sns:SetTopicAttributes",
270+
"sns:GetTopicAttributes",
271+
"sns:ListTagsForResource",
272+
"sns:DeleteTopic",
273+
"cloudwatch:PutMetricAlarm",
274+
"cloudwatch:ListTagsForResource",
275+
"cloudwatch:ListMetrics",
276+
"cloudwatch:DescribeAlarms",
277+
"cloudwatch:DescribeAlarmsForMetric",
278+
"cloudwatch:EnableAlarmActions",
279+
"cloudwatch:DisableAlarmActions",
280+
"cloudwatch:PutMetricAlarm",
281+
"cloudwatch:TagResource"
282+
],
283+
"Resource": ["*"]
262284
}
263285
]
264286
}

0 commit comments

Comments
 (0)