Home Managing Costs with AWS Budgets and CloudFormation
Post
Cancel

Managing Costs with AWS Budgets and CloudFormation

In today’s digital landscape, the cloud has revolutionized how we harness technology’s potential. It offers an extraordinary range of capabilities, allowing individuals and businesses to tap into expansive data centers seamlessly. With just a few clicks, you can effortlessly deploy servers, databases, and a myriad of other resources, harnessing the true power of the cloud. However, as with any powerful tool, understanding and managing costs is crucial.

Introducing AWS Budgets: Your Guide to Cost Control in the Cloud

Cloud computing provides incredible flexibility and numerous services, such as serverless functions, databases, and containers, available on platforms like AWS. These platforms empower users to create, innovate, and customize to their heart’s content. Nevertheless, with this immense power and flexibility comes the challenge of navigating the complex pricing and cost accumulation methods.

Whether you’re a seasoned cloud user or just beginning your journey, cost anxiety can be a common concern. The question of how to effectively manage and optimize expenses on your AWS account may loom over your head. Fortunately, there is a reliable solution to combat cost anxiety and regain peace of mind: AWS budgets. These invaluable tools help you track and receive alerts on your costs based on predefined thresholds, ensuring you stay within your desired financial boundaries.

This article will delve into AWS budgets, demystifying their purpose, functionality, and potential benefits. Additionally, we will guide you through deploying a simple budget into your AWS account using CloudFormation, empowering you to take complete control of your cloud costs. You will learn how to master cloud economics and leverage the cloud’s power while keeping your budget in check.

AWS Budgets

So, what exactly are AWS Budgets? Budgets give invaluable insights into your spending patterns and the ability to establish financial boundaries, allowing you to make informed financial decisions. They serve as your guardrails by keeping an eye on your expenses in the cloud and alerting you if things get out of the defined limits. Imagine this scenario: you set a monthly budget of $500 for your EC2 instances. AWS Budgets steps in to provide assistance, alerting you promptly when you approach or exceed your predetermined spending limit. Budgets give you important insights that help you to optimize your usage, resize instances, and explore cost-saving strategies.

Budgets give you the flexibility to choose your preferred method of receiving alerts. Whether you opt for email notifications, updates via Amazon SNS, or leverage CloudWatch Events, AWS Budgets ensures you stay well-informed, steering clear of budgetary deviations.

Let’s delve into the different types of budgets you can create. First, we have cost budgets, enabling you to set limits on overall expenses or specific cost dimensions. For instance, you can monitor your Amazon S3 bill or exercise control over expenses associated with other targeted services.

Next, there are usage budgets, offering insight into resource utilization. You can set thresholds based on usage dimensions such as the number of hours your EC2 instances operate or the volume of data transmitted via Amazon CloudFront.

Last but not least, reserved instance budgets provide a specialized avenue for managing costs related to AWS Reserved Instances. By setting budgets specific to reserved instances, you can optimize their utilization and eliminate unnecessary expenditures.

Budgets are a formidable tool in alleviating cost anxiety. By establishing budgets aligned with your key metrics and configuring alerts to activate when those limits are exceeded, you can liberate yourself to operate your account with greater freedom, free from the concern of unforeseen expenses.

CloudFormation

At its core, AWS CloudFormation is a powerful service that enables users to define and deploy their cloud infrastructure in a repeatable and automated way. By using CloudFormation, you can provision and manage a wide range of AWS resources and services effortlessly.

AWS CloudFormation offers a multitude of benefits. Firstly, it brings consistency to your infrastructure deployments. By defining your infrastructure as code using CloudFormation templates, you can ensure that the same resources and configurations are replicated accurately every time you deploy. This consistency eliminates manual errors and reduces the risk of configuration drift, resulting in a more reliable and stable infrastructure.

CloudFormation also offers scalability and flexibility. As your infrastructure requirements evolve, you can easily modify and update your CloudFormation templates to reflect those changes. This allows for seamless scaling of resources, whether you need to add additional servers, increase storage capacity, or configure load balancers. CloudFormation takes care of the underlying complexities, saving you time and effort.

Getting started with CloudFormation is relatively straightforward. You begin by creating a CloudFormation template, which is a JSON or YAML file that describes the desired AWS resources and their configurations. The template serves as a blueprint for your infrastructure. You can define resources such as EC2 instances, RDS databases, S3 buckets, security groups, and more.

Once your CloudFormation template is ready, you can deploy it using the CloudFormation service. This can be done through the AWS Management Console, AWS Command Line Interface (CLI), or by using AWS SDKs. During the deployment process, CloudFormation orchestrates the creation and configuration of the specified resources based on the template’s instructions. It automatically handles dependencies and ensures the correct order of resource provisioning.

CloudFormation is commonly used for various deployment scenarios, ranging from simple single-instance deployments to complex multi-tier architectures. It empowers users to automate the entire infrastructure lifecycle, including provisioning, updating, and even tearing down resources when they are no longer needed. With CloudFormation, you gain visibility and control over your infrastructure, making it easier to manage and maintain over time.

CloudFormation is a vital AWS service that supports deployments of all sizes. While it excels in complex scenarios, it also brings value to smaller-scale tasks like budget management. By utilizing CloudFormation for budget deployment, we achieve standardization and script reusability across multiple accounts, ensuring consistency. Furthermore, it minimizes manual intervention, streamlining the deployment process and saving precious time.

Deploy a Simple Cost Budget with CloudFormation

CloudFormation Script

Now that we have discussed Budgets and CloudFormation let’s use CloudFormation to create a simple cost budget in our AWS account. Copy the following code to a file on your computer and call it aws-simple-budget.yml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
AWSTemplateFormatVersion: "2010-09-09"

Description: "This stack creates a budget in an account that will email the specified users when 90% and 100% of the actual budget has been hit and when 100% of the budget has been forcasted."

Metadata: 
  AWS::CloudFormation::Interface: 
    ParameterGroups: 
      - 
        Label: 
          default: "Budget Configuration"
        Parameters: 
          - BudgetName
          - BudgetLimit
          - UserEmail

    ParameterLabels:
      # Naming Configuration
      BudgetName: 
        default: "What is the desired budget name?"
      BudgetLimit:
        default: "What is the desired limit for the budget?"
      UserEmail:
        default: "What is the email that should receive the budget alerts?"

Parameters:
#### Naming ####
  BudgetName:
    Description: Name of the budget
    Type: String

  BudgetLimit:
    Description: The budget amount
    Type: Number

  UserEmail:
    Description: The email that should receive budget alerts
    Type: String

Resources:

  Budget:
    Type: AWS::Budgets::Budget
    Properties: 
      Budget: 
        BudgetLimit: 
          Amount: !Ref BudgetLimit
          Unit: "USD"
        BudgetName: !Ref BudgetName
        BudgetType: "COST"
        CostTypes: 
          IncludeCredit: false
          IncludeDiscount: true
          IncludeOtherSubscription: true
          IncludeRecurring: true
          IncludeRefund: false
          IncludeSubscription: true
          IncludeSupport: true
          IncludeTax: true
          IncludeUpfront: true
          UseAmortized: false
          UseBlended: false
        TimeUnit: "MONTHLY"
      NotificationsWithSubscribers:
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: 90
          Subscribers:
            - SubscriptionType: EMAIL
              Address: !Ref UserEmail
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: 100
          Subscribers:
          - SubscriptionType: EMAIL
            Address: !Ref UserEmail
        - Notification:
            NotificationType: FORECASTED
            ComparisonOperator: GREATER_THAN
            Threshold: 100
          Subscribers:
          - SubscriptionType: EMAIL
            Address: !Ref UserEmail

The CloudFormation script provided establishes a monthly cost budget for the account. It prompts for a budget name, limit, and email address. After deployment, a single budget with three alerts is created: when actual costs reach 90%, 100%, and when the forecasted amount hits 100%. If triggered, an email notification will be sent to the specified address.

Upon receiving a budget alarm email, we recommend reviewing the cost explorer to identify the cause of unexpected cost spikes. Resolve any outstanding issues or terminate unnecessary resources as needed. To update the budget amount, simply update the CloudFormation stack and provide a new value for the budget limit parameter.

Deploying to CloudFormation

Deploying the CloudFormation script to your account is a straightforward process. The following steps will guide you through the precise process of deploying the script to your account using the AWS Management Console.

  1. Login to the AWS Management Console for the desired account in which you intend to create the budget console
  2. In the top right-hand corner of the console, locate the region drop-down menu and select “us-east-1”. It is recommended to create the CloudFormation script in this region as the Budget service is a global service. console
  3. Search for and select CloudFormation console
  4. Navigate to the CloudFormation landing page and choose “Stacks”. If you already have deployed stacks, you will be directed to the stacks list automatically, so you can skip to the next step. console
  5. On the stacks page, locate and click the “Create Stack” button. console
  6. On the Create Stack page, choose the “Upload a template file” option. Use the “Choose File” button to select the CloudFormation script file, aws-simple-budget.yml, that you previously saved to your local machine. After selecting the file, click “Next” to proceed. console
  7. On the “Specify stack details” page, enter the following parameters:
    1. Stack Name: Provide a suitable name for the CloudFormation Stack.
    2. Budget Name: Specify the desired name for the budget.
    3. Budget Limit: Enter the numeric value (in USD) to set as the budget limit. Please note that the value should only include the number and should not include a dollar sign.
    4. User Email: Input the email address that should receive budget alerts. console
  8. On the “Configure Stack Options” page, scroll down to the bottom and click the “Next” button to proceed. console
  9. On the “Review” page, carefully review the values you provided for the parameters to ensure their accuracy. Once you have verified the information, scroll to the bottom of the page and click the “Submit” button to initiate the stack creation process. console
  10. After clicking the “Submit” button, you will be redirected to the stack details page. To monitor the progress of stack creation, select the “Events” tab on this page. Here, you can track the step-by-step execution of CloudFormation as it creates the stack. Please wait until the stack creation process is completed. console
  11. Once the stack creation is finished, you can view the budget by navigating to the “Resources” tab and clicking on the link corresponding to the Budget’s Physical ID / budget name. This will take you to the Budget in the AWS Budgets service. console
  12. On the budget details screen, you will find comprehensive information about the budget history, current status, and alerts. Take your time to explore and familiarize yourself with the various sections. This budget serves as a valuable tool to monitor and track your costs in AWS. Feel free to utilize it whenever you need to check on the status of your expenses. console

To wrap it up, AWS Budgets and CloudFormation are essential tools for managing costs and deploying infrastructure effectively. With AWS Budgets, we gain control by setting thresholds and receiving alerts, while CloudFormation streamlines infrastructure provisioning with consistency and scalability. By deploying a simple budget using CloudFormation, we experienced the benefits of script reusability, streamlined deployment, and time saved. By leveraging AWS Budgets and CloudFormation, we can confidently manage costs and infrastructure, optimizing resource usage and embracing a path of cost optimization and operational excellence in the cloud.

This post is licensed under CC BY 4.0 by the author.
Contents