File tree Expand file tree Collapse file tree 4 files changed +95
-0
lines changed
infrastructure/terraform/per_account
scripts/infrastructure/policies Expand file tree Collapse file tree 4 files changed +95
-0
lines changed Original file line number Diff line number Diff line change @@ -101,3 +101,16 @@ module "vpc" {
101
101
resource "aws_route53_zone" "dev-ns" {
102
102
name = " api.cpm.dev.national.nhs.uk"
103
103
}
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 259
259
" iam:ListRoles"
260
260
],
261
261
"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" : [" *" ]
262
284
}
263
285
]
264
286
}
You can’t perform that action at this time.
0 commit comments