9 min read
In this article you will learn to build an entire serverless project of an AWS online store, beginning with a React SPA frontend hosted on AWS followed by a serverless backend with API Gateway and Lambda functions.
This article is an excerpt taken from the book, ‘Building Serverless Web Applications‘ written by Diego Zanon. In this book, you will be introduced to the AWS services, and you’ll learn how to estimate costs, and how to set up and use the Serverless Framework.
The serverless architecture of AWS’ online store
We will build a real-world use case of a serverless solution. This sample application is an online store with the following requirements:
- List of available products
- Product details with user rating
- Add products to a shopping cart
- Create account and login pages
For a better understanding of the architecture, take a look at the following diagram which gives a general view of how different services are organized and how they interact:
In this section, we will estimate the costs of our sample application demo based on some usage assumptions and Amazon’s pricing model. All pricing values used here are from mid 2017 and considers the cheapest region, US East (Northern Virginia).
This section covers an example to illustrate how costs are calculated. Since the billing model and prices can change over time, always refer to the official sources to get updated prices before making your own estimations. You can use Amazon’s calculator, which is accessible at this link: http://calculator.s3.amazonaws.com/index.html. If you still have any doubts after reading the instructions, you can always contact Amazon’s support for free to get commercial guidance.
For our pricing example, we can assume that our online store will receive the following traffic per month:
- 100,000 page views
- 1,000 registered user accounts
- 200 GB of data transferred considering an average page size of 2 MB
- 5,000,000 code executions (Lambda functions) with an average of 200 milliseconds per request
Route 53 pricing
We need a hosted zone for our domain name and it costs US$ 0.50 per month. Also, we need to pay US$ 0.40 per million DNS queries to our domain. As this is a prorated cost, 100,000 page views will cost only US$ 0.04.
Total: US$ 0.54
Amazon S3 charges you US$ 0.023 per GB/month stored, US$ 0.004 per 10,000 requests to your files, and US$ 0.09 per GB transferred. However, as we are considering the CloudFront usage, transfer costs will be charged by CloudFront prices and will not be considered in S3 billing.
If our website occupies less than 1 GB of static files and has an average per page of 2 MB and 20 files, we can serve 100,000 page views for less than US$ 20. Considering CloudFront, S3 costs will go down to US$ 0.82 while you need to pay for CloudFront usage in another section. Real costs would be even lower because CloudFront caches files and it would not need to make 2,000,000 file requests to S3, but let’s skip this detail to reduce the complexity of this estimation.
On a side note, the cost would be much higher if you had to provision machines to handle this number of page views to a static website with the same availability and scalability.
Total: US$ 0.82
CloudFront is slightly more complicated to price since you need to guess how much traffic comes from each region, as they are priced differently. The following table shows an example of estimation:
RegionEstimated trafficCost per GB transferredCost per 10,000 HTTPS requestsNorth America70%US$ 0.085US$ 0.010Europe15%US$ 0.085US$ 0.012Asia10%US$ 0.140US$ 0.012South America5%US$ 0.250US$ 0.022
As we have estimated 200 GB of files transferred with 2,000,000 requests, the total will be US$ 21.97.
Total: US$ 21.97
Certificate Manager pricing
Certificate Manager provides SSL/TLS certificates for free. You only need to pay for the AWS resources you create to run your application.
There is no charge specifically for IAM usage. You will be charged only by what AWS resources your users are consuming.
Each user has an associated profile that costs US$ 0.0055 per month. However, there is a permanent free tier that allows 50,000 monthly active users without charges, which is more than enough for our use case.
Besides that, we are charged for Cognito Syncs of our user profiles. It costs US$ 0.15 for each 10,000 sync operations and US$ 0.15 per GB/month stored. If we estimate 1,000 active and registered users with less than 1 MB per profile, with less than 10 visits per month in average, we can estimate a charge of US$ 0.30.
Total: US$ 0.30
IoT charges starts at US$ 5 per million messages exchanged. As each page view will make at least 2 requests, one to connect and another to subscribe to a topic, we can estimate a minimum of 200,000 messages per month. We need to add 1,000 messages if we suppose that 1% of the users will rate the products and we can ignore other requests like disconnect and unsubscribed because they are excluded from billing. In this setting, the total cost would be of US$ 1.01.
Total: US$ 1.01
We will use SNS only for internal notifications, when CloudWatch triggers a warning about issues in our infrastructure. SNS charges US$ 2.00 per 100,000 e-mail messages, but it offers a permanent free tier of 1,000 e-mails. So, it will be free for us.
CloudWatch charges US$ 0.30 per metric/month and US$ 0.10 per alarm and offers a permanent free tier of 50 metrics and 10 alarms per month. If we create 20 metrics and expect 20 alarms in a month, we can estimate a cost of US$ 1.00.
Total: US$ 1.00
API Gateway pricing
API Gateway starts charging US$ 3.50 per million of API calls received and US$ 0.09 per GB transferred out to the Internet. If we assume 5 million requests per month with each response with an average of 1 KB, the total cost of this service will be US$ 17.93.
Total: US$ 17.93
When you create a Lambda function, you need to configure the amount of RAM memory that will be available for use. It ranges from 128 MB to 1.5 GB. Allocating more memory means additional costs. It breaks the philosophy of avoiding provision, but at least it’s the only thing you need to worry about. The good practice here is to estimate how much memory each function needs and make some tests before deploying to production. A bad provision may result in errors or higher costs.
Lambda has the following billing model:
- US$ 0.20 per 1 million requests
- US$ 0.00001667 GB-second
Running time is counted in fractions of seconds, rounding up to the nearest multiple of 100 milliseconds.
Furthermore, there is a permanent free tier that gives you 1 million requests and 400,000 GB-seconds per month without charges.
In our use case scenario, we have assumed 5 million requests per month with an average of 200 milliseconds per execution. We can also assume that the allocated RAM memory is 512 MB per function:
- Request charges: Since 1 million requests are free, you pay for 4 million that will cost US$ 0.80.
- Compute charges: Here, 5 million executions of 200 milliseconds each gives us 1 million seconds. As we are running with a 512 MB capacity, it results in 500,000 GB-seconds, where 400,000 GB-seconds of these are free, resulting in a charge of 100,000 GB-seconds that costs US$ 1.67.
- Total: US$ 2.47
Take a look at the following SimpleDB billing where the free tier is valid for new and existing users:
- US$ 0.14 per machine-hour (25 hours free)
- US$ 0.09 per GB transferred out to the internet (1 GB is free)
- US$ 0.25 per GB stored (1 GB is free)
Take a look at the following charges:
- Compute charges: Considering 5 million requests with an average of 200 milliseconds of execution time, where 50% of this time is waiting for the database engine to execute, we estimate 139 machine hours per month. Discounting 25 free hours, we have an execution cost of US$ 15.96.
- Transfer costs: Since we’ll transfer data between SimpleDB and AWS Lambda, there is no transfer cost.
- Storage charges: If we assume a 5 GB database, it results in US$ 1.00, since 1 GB is free.
- Total: US$ 16.96, but this will not be added in the final estimation since we will run our application using DynamoDB.
DynamoDB requires you to provision the throughput capacity that you expect your tables to offer. Instead of provisioning hardware, memory, CPU, and other factors, you need to say how many read and write operations you expect and AWS will handle the necessary machine resources to meet your throughput needs with consistent and low-latency performance.
One read capacity unit represents one strongly consistent read per second or two eventually consistent reads per second, where objects have a size up to 4 KB. Regarding the writing capacity, one unit means that you can write one object of size 1 KB per second. Considering these definitions, AWS offers in the permanent free tier 25 read units and 25 write units of throughput capacity, in addition to 25 GB of free storage. It charges as follows:
- US$ 0.47 per month for every Write Capacity Unit (WCU)
- US$ 0.09 per month for every Read Capacity Unit (RCU)
- US$ 0.25 per GB/month stored
- US$ 0.09 GB per GB transferred out to the Internet
Since our estimated database will have only 5 GB, we are on the free tier and we will not pay for transferred data because there is no transfer cost to AWS Lambda.
Regarding read/write capacities, we have estimated 5 million requests per month. If we evenly distribute them, we will get two requests per second. In this case, we will consider that it’s one read and one write operation per second.
We need to estimate now how many objects are affected by a read and a write operation. For a write operation, we can estimate that we will manipulate 10 items on average and a read operation will scan 100 objects. In this scenario, we would need to reserve 10 WCU and 100 RCU. As we have 25 WCU and 25 RCU for free, we only need to pay for 75 RCU per month, which costs US$ 6.75.
Total: US$ 6.75
Let’s summarize the cost of each service in the following table:
ServiceMonthly CostsRoute 53US$ 0.54S3US$ 0.82CloudFrontUS$ 21.97CognitoUS$ 0.30IoTUS$ 1.01CloudWatchUS$ 1.00API GatewayUS$ 17.93LambdaUS$ 2.47DynamoDBUS$ 6.75TotalUS$ 52.79
It results in a total cost of ~ US$ 50 per month in infrastructure to serve 100,000 page views. If you have a conversion rate of 1%, you can get 1,000 sales per month, which means that you pay US$ 0.05 in infrastructure for each product that you sell.
Thus, in this article you learned the serverless architecture of AWS online store also learned how to estimate its costs.
If you’ve enjoyed reading the excerpt, do check out, Building Serverless Web Applications to monitor the performance, efficiency and errors of your apps and also learn how to test and deploy your applications.