Skip to content

Commit bc49b92

Browse files
committed
[feature/PI-550-billing] billing alert
1 parent 7718325 commit bc49b92

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

.github/workflows/_deploy.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ jobs:
7373
needs: [get-branch-from-workflow-file, build]
7474
runs-on: [self-hosted, ci]
7575
steps:
76+
- uses: actions/checkout@v4
77+
with:
78+
ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }}
7679
- uses: ./.github/actions/terraform/
7780
with:
7881
command: init
@@ -87,6 +90,9 @@ jobs:
8790
needs: [get-branch-from-workflow-file, terraform--init]
8891
runs-on: [self-hosted, ci]
8992
steps:
93+
- uses: actions/checkout@v4
94+
with:
95+
ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }}
9096
- uses: ./.github/actions/terraform/
9197
with:
9298
command: plan
@@ -102,6 +108,9 @@ jobs:
102108
environment: ${{ inputs.account }}
103109
runs-on: [self-hosted, ci]
104110
steps:
111+
- uses: actions/checkout@v4
112+
with:
113+
ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }}
105114
- uses: ./.github/actions/terraform/
106115
with:
107116
command: apply
@@ -116,6 +125,9 @@ jobs:
116125
needs: [get-branch-from-workflow-file, terraform--apply]
117126
runs-on: [self-hosted, ci]
118127
steps:
128+
- uses: actions/checkout@v4
129+
with:
130+
ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }}
119131
- uses: ./.github/actions/make/
120132
if: ${{ env.SCOPE == 'per_workspace'}}
121133
with:
@@ -137,6 +149,9 @@ jobs:
137149
needs: [get-branch-from-workflow-file, apigee--deploy]
138150
runs-on: [self-hosted, ci]
139151
steps:
152+
- uses: actions/checkout@v4
153+
with:
154+
ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }}
140155
- if: ${{ env.ACCOUNT != 'mgmt'}}
141156
uses: ./.github/actions/make/
142157
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ cpm.cdx.json
8585
# test output files
8686
test_failure.json
8787
test_success_*.json
88+
pyrightconfig.json

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+
}

0 commit comments

Comments
 (0)