-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
101 lines (87 loc) · 3.51 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
experiments = [module_variable_optional_attrs]
}
provider "aws" {
region = "us-east-1"
}
//-----------------------------------------------------------------------------
// Estimated Charges SNS Topic & Subcription
//-----------------------------------------------------------------------------
resource "aws_sns_topic" "estimated_charges_alarm_topic" {
name = "estimated-charges-alarm-topic"
}
resource "aws_sns_topic_subscription" "estimated_charges_alarm_email_subscription" {
topic_arn = aws_sns_topic.estimated_charges_alarm_topic.arn
protocol = "email"
endpoint = var.email
}
resource "aws_sns_topic_subscription" "estimated_charges_alarm_sns_subscription" {
topic_arn = aws_sns_topic.estimated_charges_alarm_topic.arn
protocol = "sms"
endpoint = var.phone_number
}
//-----------------------------------------------------------------------------
// Estimated Charges Alarm
//-----------------------------------------------------------------------------
resource "aws_cloudwatch_metric_alarm" "estimated_charges_alarm" {
for_each = { for threshold in var.charge_thresholds : threshold.name => threshold }
alarm_name = each.key
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "EstimatedCharges"
namespace = "AWS/Billing"
period = "21600"
statistic = "Maximum"
threshold = each.value.threshold
alarm_description = "CloudWatch Billing Alarm which triggers when your AWS bill goes above ${each.value.threshold} dollars."
alarm_actions = [aws_sns_topic.estimated_charges_alarm_topic.arn]
dimensions = { Currency = "USD" }
}
//-----------------------------------------------------------------------------
// Network Egress SNS Topic & Subcription
//-----------------------------------------------------------------------------
resource "aws_sns_topic" "network_egress_alarm_topic" {
name = "estimated-charges-alarm-topic"
}
resource "aws_sns_topic_subscription" "network_egress_alarm_email_subscription" {
topic_arn = aws_sns_topic.network_egress_alarm_topic.arn
protocol = "email"
endpoint = var.email
}
resource "aws_sns_topic_subscription" "network_egress_alarm_sns_subscription" {
topic_arn = aws_sns_topic.network_egress_alarm_topic.arn
protocol = "sms"
endpoint = var.phone_number
}
//-----------------------------------------------------------------------------
// Network Egress Alarm
//-----------------------------------------------------------------------------
data "aws_instances" "running_instances" {
count = var.egress_threshold == null ? 0 : 1
filter {
name = "tag:MonitorEgress"
values = ["true"]
}
}
resource "aws_cloudwatch_metric_alarm" "asg_network_egress_alarm" {
for_each = { for instance in data.aws_instances.running_instances : instance.arn => instance }
alarm_name = "${each.value.arn}-egress-alarm"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "NetworkOut"
namespace = "AWS/EC2"
period = "300"
statistic = "Maximum"
threshold = var.egress_threshold
alarm_description = "CloudWatch EC2 Alarm which triggers when Egress goes above ${var.egress_threshold} GB for ${each.value.id}."
alarm_actions = [aws_sns_topic.network_egress_alarm_topic.arn]
dimensions = {
InstanceId = each.value.id
}
}