Monitoring costs using CloudWatch

AWS CloudWatch can help you track and manage your cloud costs. This is especially useful where your AWS resource usage varies significantly and you need to log in to the AWS portal several times a day to track the usage. You can set appropriate alarms when the charges exceed a certain predefined threshold. To enable billing alerts for your AWS account, you have to log in as the account owner and edit the configuration settings. Hence, you can send notifications based on these metrics as e-mails and/or route them to other applications for further processing.

The estimated charges are calculated and sent several times a day to CloudWatch. These metrics include estimated total charges and charges by service. In addition, if you are using the consolidated billing option, then your CloudWatch metrics for estimated charges by linked account and estimated charges by linked account and service are also stored. This data is stored for 2 weeks, and it includes the estimated charges for every AWS service you use, as well as the total costs.

How to do it…

Enabling the monitoring of your estimated charges

You can enable the monitoring of your estimated charges in the AWS portal. To enable detail monitoring follow these steps:

  1. Open AWS billing console at https://console.aws.amazon.com/billing/home#/.
  2. Log in to the AWS console using the username and password.
  3. Click on Preferences.
  4. Select the Receive Billing Alerts checkbox:
    Enabling the monitoring of your estimated charges
  5. Create an SNS topic.

    Execute the following command to create the SNS topic called BillingTopic. Record TopicArn for further usage.

    $ aws sns create-topic 
    --name BillingTopic
    
  6. Subscribe to the SNS topic by running the following command. A user with e-mail ID is subscribed to an SNS topic called BillingTopic.
    $ aws sns subscribe 
    --topic-arn 
    arn:aws:sns:ap-southeast-1:968336292411:BillingTopic 
    --protocol email 
    --notification-endpoint [email protected]
    
  7. Create the billing alarm.

    Execute the following command to create an alarm that sends e-mail notifications to user when your estimated month-to-date charges for Amazon EC2 exceeds $50.

    $ aws cloudwatch put-metric-alarm 
    --alarm-name ec2billing 
    --comparison-operator GreaterThanOrEqualToThreshold 
    --evaluation-periods 1 
    --metric-name EstimatedCharges 
    --namespace AWS/Billing 
    --dimensions Name=Currency,Value=USD 
    --period 21600 
    --statistic Maximum 
    --threshold 50 
    --actions-enabled 
    --alarm-actions arn:aws:sns:ap-southeast-1:968336292411:BillingTopic
    

How it works…

First, we enable detail monitoring using the AWS console. Next, we create an SNS topic and subscribe to it. After the user subscribes to the topic, he/she will receive a confirmation mail from AWS. After the user confirms the subscription, he/she will start receiving the alerts as e-mail from AWS.

The protocol parameters let us specify the protocol from the supported protocols (HTTP, HTTPS, e-mail, email-JSON, SMS, SQS, etc). You can construct systems that react to the alarms via a HTTP(S) endpoint poke from these alerts. We also specify the endpoint for the notifications. The endpoints vary by the protocol selected. For example, for the in the e-mail protocol, the notification endpoint is the e-mail address.

We create the billing alarm based on the metric data. In our example, we send out an e-mail notification when the charges hit a certain amount. The alarm action here is the SNS topic with name BillingTopic, and the users subscribed to this topic will receive e-mails when the alarm is triggered. We assign a name to the alarm, the arithmetic operation to use when comparing the specified statistic and threshold, the number of periods over which data is compared to the specified threshold, the metric, namespace, period, statistic, and the threshold value.

Note

If you are new to the AWS environment, then a good starting point is to set up a billing alert for restricting your usage to remain within the free usage tier. Later on, you can set the limit to remain within your assigned or planned budgets.

The actions-enabled parameter also indicates whether or not actions should be executed during any changes to the alarm's state and the AlarmActions parameter lists the actions to execute when this alarm is triggered. Each action is specified as an Amazon Resource Name (ARN).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset