Hands-On Lab: Designing Cost-Optimized Database Solutions on AWS
Design cost-optimized database solutions
Hands-On Lab: Designing Cost-Optimized Database Solutions on AWS
Welcome to this Hands-On Lab focused on Domain 4 of the AWS Certified Solutions Architect - Associate (SAA-C03) exam. In this lab, we will explore database capacity planning by provisioning an Amazon DynamoDB table, adding cost-allocation tags for the AWS Cost Explorer, and migrating the table to a serverless, pay-per-request billing model to optimize costs for unpredictable workloads.
Prerequisites
Before starting this lab, ensure you have the following:
- AWS Account: An active AWS account with Administrator access.
- AWS CLI: Installed and configured (
aws configure) with valid access keys. - IAM Permissions: Policies allowing
dynamodb:*actions. - Prior Knowledge: Basic understanding of NoSQL databases and AWS pricing models (Provisioned vs. Serverless/On-Demand).
Learning Objectives
Upon completing this lab, you will be able to:
- Provision an Amazon DynamoDB table with defined Read/Write Capacity Units (RCUs and WCUs).
- Apply Cost Allocation Tags to AWS database resources for detailed billing visibility.
- Transition a database from provisioned capacity to On-Demand (Serverless) to optimize costs for spiky or unpredictable workloads.
Architecture Overview
The following flowchart illustrates the resource lifecycle we will build and modify in this lab:
Mathematical Concept: Capacity Cost Formula
When using Provisioned Capacity, your monthly cost is fixed regardless of usage. The baseline cost is calculated using this formula:
[!NOTE] In contrast, On-Demand pricing is calculated strictly as:
Below is a visual representation of how cost scales between the two models:
Step-by-Step Instructions
Step 1: Provision a DynamoDB Table with Provisioned Capacity
First, we will create a DynamoDB table named brainybee-lab-orders using a provisioned capacity model. This represents a traditional, fixed-cost database architecture.
aws dynamodb create-table \
--table-name brainybee-lab-orders \
--attribute-definitions AttributeName=OrderID,AttributeType=S \
--key-schema AttributeName=OrderID,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--region us-east-1[!TIP] Provisioned throughput is measured in Capacity Units. 5 RCUs and 5 WCUs fit well within the AWS Free Tier, ensuring this step does not incur immediate charges.
▶Console alternative
- Navigate to the DynamoDB Console.
- Click Create table.
- Enter Table name:
brainybee-lab-orders. - Enter Partition key:
OrderID(Type: String). - Under Table settings, select Customize settings.
- Under Read/write capacity settings, select Provisioned and set Read and Write capacity to
5. - Click Create table.
📸 Screenshot: Customize settings showing 5 RCU and 5 WCU.
Step 2: Apply Cost Allocation Tags
To effectively monitor cost optimization (a critical skill for Task 4.3 of the SAA-C03 exam), resources must be tagged so they can be filtered in AWS Cost Explorer and AWS Budgets.
Note: Retrieve your table's ARN from the output of Step 1, or replace <YOUR_ACCOUNT_ID> appropriately below.
aws dynamodb tag-resource \
--resource-arn arn:aws:dynamodb:us-east-1:<YOUR_ACCOUNT_ID>:table/brainybee-lab-orders \
--tags Key=Environment,Value=Lab Key=CostCenter,Value=DB-Optimization \
--region us-east-1[!IMPORTANT] For tags to appear in AWS Billing, an Administrator must explicitly activate them in the Cost Allocation Tags section of the Billing Console.
▶Console alternative
- In the DynamoDB Console, select the
brainybee-lab-orderstable. - Go to the Additional settings tab.
- Scroll down to Tags and click Manage tags.
- Click Add new tag.
- Enter Key:
Environment, Value:Lab. - Enter Key:
CostCenter, Value:DB-Optimization. - Click Save changes.
Step 3: Optimize Costs by Switching to On-Demand Capacity
Assume the brainybee-lab-orders database experiences highly unpredictable, spiky traffic. Paying a flat hourly rate for provisioned capacity wastes money during idle hours. We will migrate the database to On-Demand (Serverless) mode to optimize costs.
aws dynamodb update-table \
--table-name brainybee-lab-orders \
--billing-mode PAY_PER_REQUEST \
--region us-east-1[!TIP] Changing the billing mode to
PAY_PER_REQUESTinvolves zero downtime. AWS handles the seamless transition of the underlying partition management.
▶Console alternative
- In the DynamoDB Console, select the
brainybee-lab-orderstable. - Navigate to the Capacity tab (or Additional settings depending on console version).
- In the Capacity mode section, click Edit.
- Change the selection from Provisioned to On-demand.
- Click Save changes.
📸 Screenshot: Radio button toggled from Provisioned to On-demand.
Checkpoints
Let's verify that our table successfully transitioned to On-Demand capacity and that our cost tags are applied.
1. Verify Billing Mode:
aws dynamodb describe-table --table-name brainybee-lab-orders --query "Table.BillingModeSummary.BillingMode" --output textExpected Output: PAY_PER_REQUEST
2. Verify Tags:
aws dynamodb list-tags-of-resource --resource-arn arn:aws:dynamodb:us-east-1:<YOUR_ACCOUNT_ID>:table/brainybee-lab-ordersExpected Output: A JSON array containing your Environment and CostCenter tags.
Clean-Up / Teardown
[!WARNING] Remember to run the teardown commands to avoid ongoing charges. Even though On-Demand charges per request, the table consumes storage space which incurs a tiny monthly cost.
To clean up the resources deployed in this lab, run the following command to delete the table:
aws dynamodb delete-table \
--table-name brainybee-lab-orders \
--region us-east-1▶Console alternative
- In the DynamoDB Console, select the
brainybee-lab-orderstable. - Click the Delete button at the top right.
- Type
confirmin the text box to validate the deletion. - Click Delete table.
Troubleshooting
| Common Error | Cause | Solution |
|---|---|---|
ResourceNotFoundException | The specified table name or ARN does not exist. | Verify your spelling of brainybee-lab-orders, ensure you are in the correct --region, and verify your <YOUR_ACCOUNT_ID> in the ARN. |
ValidationException | Trying to change billing mode too frequently. | AWS limits how often you can switch between Provisioned and On-Demand (once every 24 hours). Wait if you recently switched it. |
UnrecognizedClientException | Invalid or expired AWS credentials. | Run aws configure to provide valid Access Key ID and Secret Access Key. |
AccessDeniedException | The IAM user lacks permissions for DynamoDB. | Ensure your IAM user has AmazonDynamoDBFullAccess or equivalent inline policy. |