Skip to content

Commit

Permalink
feat: changing the cost filters to a map
Browse files Browse the repository at this point in the history
  • Loading branch information
gambol99 committed Jan 2, 2025
1 parent ca36689 commit e781857
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .tflint.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ rule "terraform_module_pinned_source" {
}

rule "terraform_standard_module_structure" {
enabled = true
enabled = false
}

rule "terraform_workspace_remote" {
Expand Down
51 changes: 50 additions & 1 deletion examples/budgets/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,55 @@
# to build your own root module that invokes this module
#####################################################################################

locals {
## The region
region = "eu-west-2"

## The budgets
budgets = [
{
name = "AWS Monthly Budget for ${local.region} (Actual)"
budget_type = "COST"
limit_amount = "100.0"
limit_unit = "USD"
time_unit = "MONTHLY"

notification = {
comparison_operator = "GREATER_THAN"
threshold = "80"
threshold_type = "PERCENTAGE"
notification_type = "ACTUAL"
}

cost_filter = {
"Region": {
values = [local.region]
}
}
},
{
name = "AWS Monthly Budget for ${local.region} (Forecast)"
budget_type = "COST"
limit_amount = "100.0"
limit_unit = "USD"
time_unit = "MONTHLY"

notification = {
comparison_operator = "GREATER_THAN"
threshold = "80"
threshold_type = "PERCENTAGE"
notification_type = "FORECASTED"
}

cost_filter = {
"Region": {
values = [local.region]
}
}
},
]
}

## Read the secret for aws secrets manager
data "aws_secretsmanager_secret" "notification" {
name = var.notification_secret_name
Expand All @@ -17,7 +66,7 @@ data "aws_secretsmanager_secret_version" "notification" {
module "budgets" {
source = "../../modules/budgets"

budgets = var.budgets
budgets = local.budgets
notifications = {
email = {
addresses = var.notification_emails
Expand Down
55 changes: 1 addition & 54 deletions examples/budgets/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,4 @@ variable "notification_secret_name" {
description = "The name of the secret containing the email address to notify when a budget exceeds its threshold"
type = string
default = "notification/secret"
}

variable "budgets" {
description = "A collection of budgets to provision"
type = list(object({
name = string
budget_type = optional(string, "COST")
limit_amount = optional(string, "100.0")
limit_unit = optional(string, "PERCENTAGE")
time_unit = optional(string, "MONTHLY")

notification = optional(object({
comparison_operator = string
threshold = number
threshold_type = string
notification_type = string
}), null)

auto_adjust_data = optional(list(object({
auto_adjust_type = string
})), [])

cost_filter = optional(list(object({
name = string
values = list(string)
})), [])

cost_types = optional(object({
include_credit = optional(bool, false)
include_discount = optional(bool, false)
include_other_subscription = optional(bool, false)
include_recurring = optional(bool, false)
include_refund = optional(bool, false)
include_subscription = optional(bool, false)
include_support = optional(bool, false)
include_tax = optional(bool, false)
include_upfront = optional(bool, false)
use_blended = optional(bool, false)
}), {
include_credit = false
include_discount = false
include_other_subscription = false
include_recurring = false
include_refund = false
include_subscription = true
include_support = false
include_tax = false
include_upfront = false
use_blended = false
})
}))
default = []
}

}
2 changes: 1 addition & 1 deletion modules/budgets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ The `terraform-docs` utility is used to generate this README. Follow the below s
|------|-------------|------|---------|:--------:|
| <a name="input_notifications"></a> [notifications](#input\_notifications) | The configuration as to how the budget notifications should be sent | <pre>object({<br/> email = optional(object({<br/> addresses = list(string)<br/> }), null)<br/> slack = optional(object({<br/> lambda_name = optional(string, "budget-notifications")<br/> secret_name = optional(string, null)<br/> webhook_url = optional(string, null)<br/> }), null)<br/> teams = optional(object({<br/> webhook_url = string<br/> }), null)<br/> })</pre> | n/a | yes |
| <a name="input_accounts_id_to_name"></a> [accounts\_id\_to\_name](#input\_accounts\_id\_to\_name) | A mapping of account id and account name - used by notification lamdba to map an account ID to a human readable name | `map(string)` | `{}` | no |
| <a name="input_budgets"></a> [budgets](#input\_budgets) | A collection of budgets to provision | <pre>list(object({<br/> name = string<br/> budget_type = optional(string, "COST")<br/> limit_amount = optional(string, "100.0")<br/> limit_unit = optional(string, "PERCENTAGE")<br/> time_unit = optional(string, "MONTHLY")<br/><br/> notification = optional(object({<br/> comparison_operator = string<br/> threshold = number<br/> threshold_type = string<br/> notification_type = string<br/> }), null)<br/><br/> auto_adjust_data = optional(list(object({<br/> auto_adjust_type = string<br/> })), [])<br/><br/> cost_filter = optional(list(object({<br/> name = string<br/> values = list(string)<br/> })), [])<br/><br/> cost_types = optional(object({<br/> include_credit = optional(bool, false)<br/> include_discount = optional(bool, false)<br/> include_other_subscription = optional(bool, false)<br/> include_recurring = optional(bool, false)<br/> include_refund = optional(bool, false)<br/> include_subscription = optional(bool, false)<br/> include_support = optional(bool, false)<br/> include_tax = optional(bool, false)<br/> include_upfront = optional(bool, false)<br/> use_blended = optional(bool, false)<br/> }), {<br/> include_credit = false<br/> include_discount = false<br/> include_other_subscription = false<br/> include_recurring = false<br/> include_refund = false<br/> include_subscription = true<br/> include_support = false<br/> include_tax = false<br/> include_upfront = false<br/> use_blended = false<br/> })<br/><br/> tags = optional(map(string), {})<br/> }))</pre> | `[]` | no |
| <a name="input_budgets"></a> [budgets](#input\_budgets) | A collection of budgets to provision | <pre>list(object({<br/> name = string<br/> budget_type = optional(string, "COST")<br/> limit_amount = optional(string, "100.0")<br/> limit_unit = optional(string, "PERCENTAGE")<br/> time_unit = optional(string, "MONTHLY")<br/><br/> notification = optional(object({<br/> comparison_operator = string<br/> threshold = number<br/> threshold_type = string<br/> notification_type = string<br/> }), null)<br/><br/> auto_adjust_data = optional(list(object({<br/> auto_adjust_type = string<br/> })), [])<br/><br/> cost_filter = optional(map(object({<br/> values = list(string)<br/> })), {})<br/><br/> cost_types = optional(object({<br/> include_credit = optional(bool, false)<br/> include_discount = optional(bool, false)<br/> include_other_subscription = optional(bool, false)<br/> include_recurring = optional(bool, false)<br/> include_refund = optional(bool, false)<br/> include_subscription = optional(bool, false)<br/> include_support = optional(bool, false)<br/> include_tax = optional(bool, false)<br/> include_upfront = optional(bool, false)<br/> use_blended = optional(bool, false)<br/> }), {<br/> include_credit = false<br/> include_discount = false<br/> include_other_subscription = false<br/> include_recurring = false<br/> include_refund = false<br/> include_subscription = true<br/> include_support = false<br/> include_tax = false<br/> include_upfront = false<br/> use_blended = false<br/> })<br/><br/> tags = optional(map(string), {})<br/> }))</pre> | `[]` | no |
| <a name="input_create_sns_topic"></a> [create\_sns\_topic](#input\_create\_sns\_topic) | A flag to determine if the SNS topic should be created | `bool` | `true` | no |
| <a name="input_identity_center_role"></a> [identity\_center\_role](#input\_identity\_center\_role) | The name of the role to use when redirecting through Identity Center | `string` | `null` | no |
| <a name="input_identity_center_start_url"></a> [identity\_center\_start\_url](#input\_identity\_center\_start\_url) | The start URL of your Identity Center instance | `string` | `null` | no |
Expand Down
4 changes: 2 additions & 2 deletions modules/budgets/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ resource "aws_budgets_budget" "this" {
for_each = each.value.cost_filter

content {
name = cost_filter.value.name
name = cost_filter.key
values = cost_filter.value.values
}
}
Expand All @@ -77,4 +77,4 @@ resource "aws_budgets_budget" "this" {
}

depends_on = [module.notifications]
}
}
5 changes: 2 additions & 3 deletions modules/budgets/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ variable "budgets" {
auto_adjust_type = string
})), [])

cost_filter = optional(list(object({
name = string
cost_filter = optional(map(object({
values = list(string)
})), [])
})), {})

cost_types = optional(object({
include_credit = optional(bool, false)
Expand Down
2 changes: 1 addition & 1 deletion terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_version = ">= 1.0"

required_providers {
# tflint-ignore: terraform_required_providers
# tflint-ignore: terraform_unused_required_providers
aws = {
source = "hashicorp/aws"
}
Expand Down
23 changes: 22 additions & 1 deletion tests/module.tftest.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
mock_provider "aws" {}
mock_provider "aws" {
mock_data "aws_region" {
defaults = {
name = "us-west-2"
current_region = "us-west-2"
}
}

mock_data "aws_partition" {
defaults = {
partition = "aws"
}
}

mock_data "aws_caller_identity" {
defaults = {
account_id = "123456789012"
user_arn = "arn:aws:iam::123456789012:user/terraform"
user_id = "AIDACKCEVSQ6C2EXAMPLE"
}
}
}

run "basic_account_budget" {
command = plan
Expand Down

0 comments on commit e781857

Please sign in to comment.