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.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay