DEV Community

Cover image for Automating Cost Management: Creating AWS Budgets with Terraform ⚙️
awedis for AWS Heroes

Posted on

1

Automating Cost Management: Creating AWS Budgets with Terraform ⚙️

When it comes to monitoring, one of the most important things is to automate alarms, budget passes and always be aware of the price changes.

I will show you how to create a Budget using AWS Billing and Cost Management with Terraform in this article.

The main parts of this article:
1- About AWS Services
2- Architecture overview (Terraform)
3- Conclusion

About AWS Services

1- AWS Billing and Cost Management provides a suite of features to help you set up your billing, retrieve and pay invoices, and analyze, organize, plan, and optimize your costs.

To learn more about AWS Billing and Cost Management: Official Page

Architecture Overview

variable "region" {
  default     = "eu-west-1"
  description = "AWS Region to deploy to"
}

terraform {
  required_version = "1.5.1"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.84.0"
    }
  }
}

provider "aws" {
  region = var.region
}

resource "aws_budgets_budget" "ec2" {
  name              = "MonthlyBudget"
  budget_type       = "COST"
  limit_amount      = "10"
  limit_unit        = "USD"
  time_unit         = "MONTHLY"

  notification {
    comparison_operator        = "GREATER_THAN"
    threshold                  = 80
    threshold_type             = "PERCENTAGE"
    notification_type          = "FORECASTED"
    subscriber_email_addresses = ["<YOUR_EMAIL_ADDRESS>"]
  }

  tags = {
    Name = "MonthlyBudget"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now let's say you want to add a budget only for certain resources, you can add the following terraform code inside aws_budgets_budget.

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

Conclusion

Automating cost management and budget alerting is essential, especially as your team grows, activity on your account increases, and you scale up rapidly. Creating and maintaining budgets to stay on top of your costs is crucial. I hope this article was helpful and informative!

Happy coding 👨🏻‍💻

If you'd like more content like this, please connect with me on LinkedIn.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs