forked from kristijaneftimovpcg/terraform-aws-cost-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
175 lines (151 loc) · 5.01 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
data "archive_file" "lambda" {
type = "zip"
source_file = "${path.module}/lambda.py"
output_path = "${path.module}/lambda_function_payload.zip"
}
data "aws_caller_identity" "current" {}
resource "aws_lambda_function" "budget_calculation" {
filename = data.archive_file.lambda.output_path
function_name = var.lambda-name
source_code_hash = data.archive_file.lambda.output_base64sha256
role = aws_iam_role.lambda_role.arn
handler = "lambda.lambda_handler"
runtime = var.python-runtime
timeout = 60
memory_size = 128
environment {
variables = {
sns_topic_arn = aws_sns_topic.budget_notifications.arn
region = var.aws-region
project_name = var.project-name
calculation_type = var.calculation-type
account_id = data.aws_caller_identity.current.account_id
}
}
}
resource "aws_iam_role" "lambda_role" {
name = var.lambda-role-name
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}
resource "aws_iam_policy_attachment" "lambda_policy_attachment" {
name = "lambda-cloudwatch-policy-attachment"
roles = [aws_iam_role.lambda_role.name]
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_cloudwatch_event_rule" "cost_trigger" {
name = var.cloudwatch-rule-name
schedule_expression = var.cron-expression
depends_on = [aws_lambda_function.budget_calculation]
}
resource "aws_cloudwatch_event_target" "lambda_target" {
rule = aws_cloudwatch_event_rule.cost_trigger.name
target_id = "invoke-lambda"
arn = aws_lambda_function.budget_calculation.arn
}
resource "aws_lambda_permission" "invoke_lambda" {
statement_id = "AllowExecutionFromCloudWatchRules"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.budget_calculation.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.cost_trigger.arn
}
data "aws_iam_policy_document" "lambda_access_document" {
statement {
effect = "Allow"
actions = [
"ce:GetCostAndUsage",
"ce:GetCostForecast",
"sns:Publish"
]
resources = ["*"]
}
depends_on = [aws_sns_topic.budget_notifications]
}
resource "aws_iam_policy" "lambda_access_policy" {
name = "lambda-access"
path = "/"
description = "IAM policy for setting permissions for Lambda"
policy = data.aws_iam_policy_document.lambda_access_document.json
}
resource "aws_iam_role_policy_attachment" "lambda_access_attachment" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.lambda_access_policy.arn
depends_on = [data.aws_iam_policy_document.lambda_access_document]
}
resource "aws_sns_topic" "budget_notifications" {
name = var.sns-topic-name
display_name = var.sns-topic-name
}
data "aws_iam_policy_document" "sns_permissions" {
policy_id = "__cost_calculation"
statement {
effect = "Allow"
actions = [
"SNS:Subscribe",
"SNS:SetTopicAttributes",
"SNS:RemovePermission",
"SNS:Receive",
"SNS:Publish",
"SNS:ListSubscriptionsByTopic",
"SNS:GetTopicAttributes",
"SNS:DeleteTopic",
"SNS:AddPermission",
]
principals {
type = "AWS"
identifiers = ["*"]
}
resources = [
aws_sns_topic.budget_notifications.arn
]
}
}
resource "aws_sns_topic_policy" "cost_calculation" {
arn = aws_sns_topic.budget_notifications.arn
policy = data.aws_iam_policy_document.sns_permissions.json
}
resource "aws_sns_topic_subscription" "email_subscription" {
for_each = toset(var.sns-endpoint)
topic_arn = aws_sns_topic.budget_notifications.arn
protocol = var.sns-protocol
endpoint = each.value
endpoint_auto_confirms = true
}
resource "aws_sns_topic_subscription" "email_PO_subscription" {
count = var.sns-po-endpoint != "" ? 1 : 0
topic_arn = aws_sns_topic.budget_notifications.arn
protocol = var.sns-protocol
endpoint = var.sns-po-endpoint
endpoint_auto_confirms = true
}
resource "aws_budgets_budget" "monthly_cost_budget" {
count = var.budget-threshold > 0 ? 1 : 0 // deploy the resource, only if budget-threshold is greater than 0
name = "MonthlyCostBudget-${var.project-name}"
budget_type = "COST"
limit_amount = var.budget-threshold
limit_unit = "USD"
time_unit = "MONTHLY"
cost_types {
include_tax = true
include_subscription = true
use_blended = false
}
notification {
comparison_operator = "GREATER_THAN"
notification_type = "ACTUAL"
threshold = 80.0
threshold_type = "PERCENTAGE"
subscriber_email_addresses = var.sns-endpoint
}
}