Skip to content

Commit 7a853b0

Browse files
author
Steven Nemetz
committed
Support budget by tag
1 parent ed3a504 commit 7a853b0

File tree

12 files changed

+177
-18
lines changed

12 files changed

+177
-18
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
Terraform module to setup AWS budgets and notifications
44

5-
This is fairly specific for now. It can create budgets for multiple linked accounts. Each with it's own limit.
5+
For now it can manage 2 types of budgets:
6+
- Account budgets for a single account or multiple linked accounts. Each with it's own limit.
7+
- Budget by tag
68
Notification is only supported for a single email address
79

810
NOTE: Setting up notification and subscribers is not currently supported in Terraform

examples/cost-tag-monthly/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Example: Monthly budget by tag

examples/cost-tag-monthly/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module "budget-1" {
2+
source = "../../"
3+
emails = ["user@company.com", "user2@company.com"]
4+
budget_name = "service-1"
5+
budget_name_prefix = "Testing-"
6+
tag_value = "service-1"
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
provider "aws" {
2+
region = "${var.region}"
3+
4+
# Make it faster by skipping something
5+
skip_get_ec2_platforms = true
6+
skip_metadata_api_check = true
7+
skip_region_validation = true
8+
skip_credentials_validation = true
9+
skip_requesting_account_id = true
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "region" {
2+
default = "us-west-2"
3+
}

examples/cost-tag-quarterly/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Example: Quarterly budget by tag

examples/cost-tag-quarterly/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module "budget-1" {
2+
source = "../../"
3+
emails = ["user@company.com", "user2@company.com"]
4+
budget_name = "service-1"
5+
budget_name_prefix = "Testing-"
6+
tag_value = "service-1"
7+
time_unit = "QUARTERLY"
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
provider "aws" {
2+
region = "${var.region}"
3+
4+
# Make it faster by skipping something
5+
skip_get_ec2_platforms = true
6+
skip_metadata_api_check = true
7+
skip_region_validation = true
8+
skip_credentials_validation = true
9+
skip_requesting_account_id = true
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "region" {
2+
default = "us-west-2"
3+
}

main.tf

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,70 @@ data "aws_caller_identity" "current" {}
55

66
# https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_budgets_CostTypes.html
77
#
8-
/*
8+
/**/
99
# This is future work for single budget that can set most things
1010
locals {
1111
cost_filters = {
1212
AZ = {
1313
AZ = ""
1414
}
15+
1516
LinkedAccount = {
16-
LinkedAccount = "${join(",", var.account_ids)}"
17+
#LinkedAccount = "${join(",", var.account_ids)}"
1718
}
19+
1820
Operation = {
1921
Operation = ""
2022
}
23+
2124
PurchaseType = {
2225
PurchaseType = ""
2326
}
27+
2428
Service = {
2529
Service = ""
2630
}
31+
2732
TagKeyValue = {
28-
TagKeyValue = "Stack$$${var.service}"
33+
TagKeyValue = "user:${var.tag_key}$$${var.tag_value}"
2934
}
35+
3036
UsageType = {
3137
UsageType = ""
3238
}
3339
}
3440
}
41+
3542
/**/
43+
resource "aws_budgets_budget" "budget" {
44+
count = "${length(var.budgets) == 0 ? 1 : 0}"
45+
account_id = "${data.aws_caller_identity.current.account_id}"
46+
name = "${var.budget_name_prefix}${var.budget_name}-${title(lower(var.time_unit))}"
47+
budget_type = "${var.budget_type}"
48+
limit_unit = "${var.limit_unit}"
49+
time_unit = "${var.time_unit}"
50+
cost_filters = "${local.cost_filters[var.cost_filter_type]}"
51+
52+
limit_amount = "${var.time_unit == "ANNUALLY" ?
53+
var.limit_amount * 12 :
54+
var.time_unit == "QUARTERLY" ?
55+
var.limit_amount * 3 :
56+
var.limit_amount
57+
}"
58+
59+
time_period_start = "${var.time_unit == "ANNUALLY" ?
60+
"${substr(timestamp(), 0, 5)}01-01_00:00" :
61+
var.time_unit == "QUARTERLY" ?
62+
"${substr(timestamp(), 0, 5)}${format("%02d", (substr(timestamp(), 5, 2) - 1) / 3 * 3 + 1)}-01_00:00" :
63+
"${substr(timestamp(), 0, 8)}01_00:00"
64+
}"
65+
66+
lifecycle {
67+
ignore_changes = ["time_period_start"]
68+
}
69+
}
70+
71+
# Multiple linked account budgets
3672
resource "aws_budgets_budget" "budgets" {
3773
count = "${length(var.budgets)}"
3874
account_id = "${data.aws_caller_identity.current.account_id}"

0 commit comments

Comments
 (0)