Monitor and Control Amazon Cloud Costs with Terraform

For every Cloud project, you need to set budgets and alerts with Terraform

Clearview
ClearviewTeam

--

AWS Budgets with Terraform

by: Nedim Hadzimahmutovic

Why set up a budget for your cloud account?

If you wonder why most startups fail it is simple, they run out of money and there have been many horror stories about crazy big cloud bills.

At Clearview, when we put a new client on the Cloud, we know they will get a bill eventually. It’s our responsibility to make sure that the bill will not be a big and sudden surprise.

Setting up the budgets

Setting the AWS Cloud budget

The first step is to set budgets and alerts. We usually set:

  • The Account wide budget,
  • The Most used service budget,
  • The Most important tag budget.

Every budget sends out an email notification when the budget cost threshold is reached, in these two cases:

  • The ACTUAL cost
    which will be triggered once that amount has already been spent,
  • The FORECASTED cost
    notification will be triggered earlier as the cost is based on prediction based on your past usage.

As we are a tech company the budgets are set using Terraform and in the next section, you can find diagrams and code samples that explain how budgets and notifications work.

Monthly Account Budget

Set your Monthly AWS Budget.

This budget tracks account-wide costs.

resource "aws_budgets_budget" "monthly_account_budget" {
name = "Monthly Budget for my Account"
budget_type = "COST"
limit_amount = "500"
limit_unit = "USD"
time_unit = "MONTHLY"

notification {
comparison_operator = "GREATER_THAN"
threshold = 90
threshold_type = "PERCENTAGE"
notification_type = "ACTUAL"
subscriber_email_addresses = ["email@my.team"]
}

notification {
comparison_operator = "GREATER_THAN"
threshold = 100
threshold_type = "PERCENTAGE"
notification_type = "FORECASTED"
subscriber_email_addresses = ["email@my.team"]
}
}

Budgets by Service

AWS Budgets illustration.

EC2 Monthly Budget

This budget will track costs for EC2 services only.

resource "aws_budgets_budget" "ec2_monthly_budget" {
name = "My EC2 Monthly Budget"
budget_type = "COST"
limit_amount = "400"
limit_unit = "USD"
time_unit = "MONTHLY"

cost_filter {
name = "Service"
values = [
"Amazon Elastic Compute Cloud - Compute",
]
}

notification {
comparison_operator = "GREATER_THAN"
threshold = 90
threshold_type = "PERCENTAGE"
notification_type = "ACTUAL"
subscriber_email_addresses = ["email@my.team"]
}

notification {
comparison_operator = "GREATER_THAN"
threshold = 100
threshold_type = "PERCENTAGE"
notification_type = "FORECASTED"
subscriber_email_addresses = ["email@my.team"]
}
}

S3 Monthly Budget

AWS S3 Monthly Budget.

This budget will track costs for S3 buckets only.

resource "aws_budgets_budget" "s3_monthly_budget" {
name = "My S3 Monthly Budget"
budget_type = "COST"
limit_amount = "100"
limit_unit = "USD"
time_unit = "MONTHLY"

cost_filter {
name = "Service"
values = [
"Amazon Simple Storage Service",
]
}

notification {
comparison_operator = "GREATER_THAN"
threshold = 90
threshold_type = "PERCENTAGE"
notification_type = "ACTUAL"
subscriber_email_addresses = ["email@my.team"]
}

notification {
comparison_operator = "GREATER_THAN"
threshold = 110
threshold_type = "PERCENTAGE"
notification_type = "FORECASTED"
subscriber_email_addresses = ["email@my.team"]
}
}

AppRunner Monthly Budget

This budget will track costs for AppRunner services only.

resource "aws_budgets_budget" "apprunner_monthly_budget" {
name = "My AppRunner Monthly Budget"
budget_type = "COST"
limit_amount = "100"
limit_unit = "USD"
time_unit = "MONTHLY"

cost_filter {
name = "Service"
values = [
"AWS App Runner",
]
}

notification {
comparison_operator = "GREATER_THAN"
threshold = 90
threshold_type = "PERCENTAGE"
notification_type = "ACTUAL"
subscriber_email_addresses = ["email@my.team"]
}

notification {
comparison_operator = "GREATER_THAN"
threshold = 110
threshold_type = "PERCENTAGE"
notification_type = "FORECASTED"
subscriber_email_addresses = ["email@my.team"]
}
}

Budgets based on tag filtering

Production Tag Monthly Budget

This budget will group multiple types of service based on the tag, in this case, the production tag is used.

Links:

Closing

This article explained the importance of setting a cloud budget and then demonstrated how to do it programmatically. Not only that, we went a step further and created nice diagrams for each budget so that it is easier for every reader to imagine the setup.

We hope this article finds its way and helps you avoid unnecessary cloud costs.

This article was written by Nedim Hadzimahmutovic, our Senior DevOps Engineer here at ClearView.

--

--

A remote-first, distributed software company with team members spread across the globe. We help startups and scaling companies to build products. clearview.team