Setup automated alerts for AWS spending

AWS is a utility, you pay as you go. If you are new to AWS it can be easy to kick off services that run happily in the background costing you money. Although many services are free in the first 12 months of signing up with AWS (AWS free tier) there are also many chargeable services. It is your responsibility to know and understand what you are getting charged for.

Understanding where your AWS costs are coming from is important. You don’t want the only way of knowing what costs you have incurred being your AWS invoice! That is too late, and you may have incurred significant costs for services you weren’t using or hadn’t terminated when you were finished with them.

AWS has several tools to assist you with cost management, one of the easiest to setup is AWS Budgets. You can find more information on AWS Budgets at https://aws.amazon.com/aws-cost-management/aws-budgets/.

This is a quick article showing you how to setup a simple cost budget so you don’t exceed a certain cost. If you are new to AWS and have just setup your account, I suggest you have a read of this post (https://awsmadesimple.co.uk/300/enabling-cost-explorer/) covering how to setup access to your billing information from an IAM admin User (you shouldn’t be using your root account for day to day operations).

This article covers setting up an AWS Budget using both the AWS management console and the AWS CLI.

AWS Budgets Overview
AWS Budgets is part of AWS Cost Management and is a set of features that allow you to manage and budget your AWS costs. Cost Management also includes Cost Explorer (visualise and report on AWS costs incurred and forecast), Cost Anomaly Detection (Machine Learning based cost monitoring for unusual spends) , Rightsizing recommendations (i.e. downsizing an under-utilised EC2 instance) and Savings Plans (overall cost savings based upon a spend commitment over a 1 or 3 year period). More information can be found at https://docs.aws.amazon.com/cost-management/latest/userguide/what-is-costmanagement.html.

AWS Budgets setup using AWS Console
Type “budgets” into your AWS console and select AWS Budgets:

AWS Console – AWS Budgets Banner

If this is the first time you have been to this then select “Create Budgets”:

AWS Budgets – Create a budget

AWS has several types of Budget. As we want to be informed when we exceed a specified cost we can just use the recommended budget type – “Cost budget”:

AWS Budgets – Budget Type

Select “Next” and you will see the following screen:

AWS Budgets – Setup your budget

Give the budget a name, in this case we’ve given it the name of “cost-budget” and enter your budgeted amount. Accept all the other fields as default and click Next.

For further information on the options available on this page see https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-create.html?icmpid=docs_costmanagement_hp-setup for further information on each field.

AWS Budgets – Configure alerts

Our budget has now been setup based on an amount of $20.00, now we need to create an alert based on our budget to advise us when this amount has been exceed. We can accept the defaults with no alert thresholds or optionally create an alert threshold by clicking on the “Add an alert threshold”:

AWS Alerts – Alert Thresholds

This allows us to get an alert before our budget amount is reached i.e. send an alert threshold when we reach 80% of our budget. Just add the threshold amount and an email recipient and click Next. You can also send alerts via Amazon SNS or Amazon Chatbot.

The Alert details will be confirmed showing the alert details. Click Next to continue.

You will then get a review of all the previous steps:

AWS Budgets – Review settings

Click “Create Budget” and if successful you will see the following prompt:

AWS Budgets – Alert created prompt

Your budget will now be live and will alert you when your Budget Threshold is exceeded.

AWS Budgets setup using AWS CLI
An alternative method to creating an AWS Budget is to use the AWS CLI, Amazons Command Line Interface to AWS services. If you have a more experience and are comfortable using command line tools this is the preferred way of setting up your AWS Budget.. As well as being faster, you can also incorporate your AWS CLI commands into shell scripts to semi-automate tasks such as setting up a new AWS Budget.

AWS CLI supports Linux, Windows and Mac OS. For information on setting up the AWS CLI visit https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html for more information (Note that the current version of the AWS CLI is V2).

The basic AWS CLI command to create an AWS budget is:

aws budgets create-budget

The full command syntax together with examples can be found at https://docs.aws.amazon.com/cli/latest/reference/budgets/create-budget.html

To create an AWS Budget using the CLI (as per our budget example shown above using the AWS Console) we need to use the create-budget command. The items within the chevrons < > are your user specific information:

aws budgets create-budget \
    --account-id <account-id> \
    --budget file://<budget filename.json> \
    --notifications-with-subscribers file://<notifications with subscripers filename>

The create-budget command takes two files which are as follows:

  • Budget File – This contains the configuration data relating to our AWS cost budget
  • Notifications file (optional) – This contains the configuration data relating to our Budget Alert.

Our minimal budget file is:

{
    "BudgetLimit": {
        "Amount": "80",
        "Unit": "USD"
    },
    "BudgetName": "cost-budget",
    "BudgetType": "COST",
    
    "CostTypes": {
        "IncludeCredit": true,
        "IncludeDiscount": true,
        "IncludeOtherSubscription": true,
        "IncludeRecurring": true,
        "IncludeRefund": true,
        "IncludeSubscription": true,
        "IncludeSupport": true,
        "IncludeTax": true,
        "IncludeUpfront": true,
        "UseBlended": false
    },
    "TimeUnit": "MONTHLY"
}

Note – You can specify an optional start and an end date for the period of time that is covered by a budget. If you omit these two fields then Amazon Web Services sets the start date to 01/24/18 00:00 UTC and the end date to 06/15/87 00:00 UTC.

Our minimal notifications-with-subscribers file (a notification such as email or SNS that you want to associate with a budget) is:

[
    {
        "Notification": {
            "ComparisonOperator": "GREATER_THAN",
            "NotificationType": "ACTUAL",
            "Threshold": 80,
            "ThresholdType": "PERCENTAGE"
        },
        "Subscribers": [
            {
                "Address": "admin@awsmadesimple.co.uk",
                "SubscriptionType": "EMAIL"
            }
        ]
    }
]

Although it may look like more work to use the AWS CLI, once it is setup it is quick and convenient to use.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.