CloudServerless Architectures on AWS: Monitoring Costs

Serverless Architectures on AWS: Monitoring Costs

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

By Peter Sbarski with Sam Kroonenburg for Manning Publishing

This article was excerpted from the book Serverless Architectures on AWS.

CloudWatch is an AWS component for monitoring resources and services running on AWS, setting alarms based on a wide range of metrics, and viewing statistics on the performance of your resources. When you begin to build your serverless system, you are likely to use logging more than any other feature of CloudWatch. It will help to track and debug issues in Lambda functions, and it’s likely that you will rely it on for some time. Its other features, however, will become important as your system matures and goes to production. You will use CloudWatch to track metrics and set alarms for unexpected events.

Receiving an unpleasant surprise in the form of a large bill at the end of the month is disappointing and stressful. CloudWatch can create billing alarms that send notifications if total charges for the month exceed a predefined threshold. This is useful, not only to avoid unexpectedly large bills, but also to catch potential misconfigurations of your system.

For example, it is easy to misconfigure a Lambda function and inadvertently allocate 1.5GB of RAM to it. The function might not do anything useful except wait for 15 seconds to receive a response from a database. In a very heavy-duty environment, the system might perform 2 million invocations of the function a month costing a little over $743.00. The same function with 128MB of RAM would cost around $56.00 per month. If you perform cost calculations up front and have a sensible billing alarm, you will quickly realize that something is going on when billing alerts begin to come through.

Creating billing alerts

Follow these steps to create a billing alert:

  1. Enable billing alerts in the Billing and Cost Management console (figure 1).
    1. In the main AWS console, click your name (or the name of the IAM user that’s representing you) and then click Billing & Cost Management.
    2. Click Preferences in the navigation panel and then enable the checkbox next to Receive Billing Alerts.
    3. Click Save preferences.

Serv1
Figure 1: The preferences page allows you to manage how invoices and billing reports are received.

  1. Open CloudWatch console and select Billing in the navigation pane.
  2. In the Total Estimated Charges list, select the first checkbox next to the currency. Selecting this option captures estimated charges across all AWS services. You can, however, go granular and select specific services.
  3. Click the Create Alarm button in the lower right-hand corner to open the Create Alarm dialog.
  4. Set your spending threshold and select an SNS topic for the delivery of notifications. You can, optionally, click new list and enter an email address directly (figure 2).

Serv2
Figure 2: It’s good practice to create multiple billing alarms to keep you informed of ongoing costs.

Monitoring and optimizing costs

Services such as CloudCheckr can help to track costs, send alerts, and even suggest savings by analyzing services and resources in use. CloudCheckr comprises several different AWS services, including S3, CloudSearch, SES, SNS, and DynamoDB (figure 3). It is richer in features and easier to use than some of the standard AWS features. It is worth considering for its recommendations and daily notifications.

Serv3
Figure 3: CloudCheckr is useful for identifying improvements to your system but the good features are not free.

AWS also has a service called Trusted Advisor that suggests improvements to performance, fault tolerance, security, and cost optimization. Unfortunately, the free version of Trusted Advisor is limited, so if you want to explore all of the features and recommendations it has to offer, you must upgrade to a paid monthly plan or access it through an AWS enterprise account.

Cost Explorer (figure 4) is a useful, albeit high-level, reporting and analytics tool built in to AWS. You must activate it first by clicking your name (or the IAM user name) in the top right-hand corner of the AWS console, selecting Cost Explorer for the navigation pane, and then enabling it. Cost Explorer analyzes your costs for the current month and the past four months. It then creates a forecast for the next three months. Initially, you may not see any information, because it takes 24 hours for AWS to process data for the current month. Processing data for previous months make take even longer. More information about the Cost Explorer is available at http://amzn.to/1KvN0g2.

Serv4
Figure 4: The Cost Explorer tool allows you to review historical costs and estimate what future costs may be.

Using the Simple Monthly Calculator

The Simple Monthly Calculator is a web application developed by Amazon to help model costs for many of its services. This tool allows you to select a service on the left side of the console and then enter information related to the consumption of that particular resource to get an indicative cost. Figure 5 shows a snippet of the Simple Monthly Calculator with an estimated monthly cost of $650.00. That estimate is mainly of costs for S3, CloudFront, and the AWS support plan. It is a complex tool and it’s not without usability issues, but it can help with estimates.

You can click common customer samples on the right side of the console or enter your own values to see estimates. If you take the Media Application customer sample, something that could serve as a model for 24-Hour Video, it breaks down as follows:

  • The S3 estimated cost is $9.01. It includes 300GB storage, 200 PUT/COPY/POST/LIST requests, 100 GET and other requests, 2GB/Month data transfer out, and 10GB/Month data transfer in.
  • The CloudFront estimated cost is $549.96. It includes 5000GB/Month data transfer out with an average object size of 300KB. The edge location traffic distribution is 30% for US and Europe, 15% for Japan, and 25% for Hong Kong, Philippines, South Korea, Singapore, and Taiwan.
  • The AWS business support plan is $100.00.

Serv5
Figure 5: The Simple Monthly Calculator is a great tool to work out the estimated costs in advance. You can use these estimates to create billing alarms at a later stage.

Calculating Lambda and API Gateway Costs

The cost of running serverless architecture often can be a lot less than running traditional infrastructure. Naturally, the cost of each service you might use will be different, but we can have a look at what it takes to run a serverless system with Lambda and the API Gateway.

Amazon’s pricing for Lambda is based on the amount of requests, duration of execution, and the amount of memory allocated to the function. The first one million requests are free, with each subsequent million charged at $0.20. Duration is based on how long the function takes to execute, rounded up to the nearest 100ms. Amazon charges in 100ms increments while also taking into account the amount of memory reserved for the function.

A function created with 1GB of memory will cost $0.000001667 per 100ms of execution time, whereas a function created with 128MB of memory will cost $0.000000208 per 100ms. Note that Amazon prices may differ depending on the region and that they are subject to change at any time.

Amazon provides a perpetual free tier with 1 million free requests and 400,000 GB-seconds of compute time per month. This means that a user can perform a million requests and spend an equivalent of 400,000 seconds running a function created with 1GB of memory before they have to pay.

As an example, consider a scenario where you have to run a 256MB function five million times a month. The function executes for two seconds each time. The cost calculation follows:

Monthly Request Charge:

  • The free tier provides 1 million requests, which means that there are only 4 million billable requests (5M requests 1M free requests = 4M requests).
  • Each million is priced at $0.20, which that makes the request charge $0.80 (4M requests * $0.2/M = $0.80).

Monthly Compute Charge:

  • The compute price for a function per GB-second is $0.00001667. The free tier provides 400,000 GB-seconds free.
  • In this scenario, the function runs for 10 ms (5M * 2 seconds).
  • 10M seconds at 256MB of memory equates to 2,500,000 GB-seconds (10,000,000 * 256MB/1024 = 2,500,000).
  • The total billable amount of GB-seconds for the month is 2,100,000 (2,500,000 GB-seconds – 400,000 free tier GB-seconds = 2,100,000).
  • The compute charge is therefore $35.007 (2,100,000 GB-seconds * $0.00001667 = $35.007).

The total cost of running Lambda in the above example is $35.807. The API Gateway pricing is based on the number of API calls received and the amount of data transferred out of AWS. In US East, Amazon charges $3.50 for each million API calls received and $0.09/GB for the first 10TB transferred out. Given the above example and assuming that monthly outbound data transfer is 100GB a month, the API Gateway pricing is as follows:

Monthly API Charge:

  • The free tier includes one million API calls per month but it is valid for only 12 months. Given that it’s not a perpetual free tier, it will not be included in this calculation.
  • The total API cost is $17.50 (5M requests * $3.50/M = $17.50)

Monthly Data Charge:

  • The data charge is $9.00 (100GB * $0.09/GB = $9)

The API Gateway cost in this example is $26.50. The total cost of Lambda and the API Gateway is $62.307 per month. It’s worthwhile to attempt to model how many requests and operations you may have to handle on an ongoing basis. If you expect 2M invocations of a Lambda function that only uses 128MB of memory and runs for a second, you will pay approximately $0.20 month. If you expect 2M invocations of a function with 512MB of RAM that runs for five seconds, you will pay a little more than $75.00. With Lambda, you have an opportunity to assess costs, plan ahead, and pay for only what you actually use. Finally, don’t forget to factor in other services such as S3 or SNS, no matter how insignificant their cost may seem to be.

Serv6

Serverless Architectures on AWS: Monitoring Costs

This article was excerpted from the book Serverless Architectures on AWS.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories