Lab · Task 4.3 · about 30 minutes
Expire Data with TTL and Switch Capacity Modes
Services in this lab
- Amazon DynamoDBin scope
- AWS CLIin scope
Lesson for this lab: Cost-Optimized Databases
Goal
Build a DynamoDB table with a time to live attribute, switch its capacity mode, and watch an expired item stay visible until DynamoDB removes it.
Before you start
- An AWS account you can experiment in. Use a sandbox account, never a production account. Create a monthly budget with an alert in AWS Budgets on that account before you start, so unexpected spend is caught early.
- Sign in as an IAM Identity Center user or an IAM role that can create and delete DynamoDB tables. Don't use the root user.
- AWS CLI v2 configured for that identity (
aws sts get-caller-identityreturns your role, not root). - One Region for every command. Replace
<region>with it.
The exam point of view: a table that holds expiring data should delete it with TTL, not with a job you write and run. The trap is assuming that an expired item disappears the moment it expires.
Resources you will create (all deleted in Clean up):
- One DynamoDB table,
lab-db-cost-sessions, with a string partition keysession_id.
Steps
0 of 5 steps done
Provisioned mode lets you see the capacity setting that the rest of the lab changes.
aws dynamodb create-table \ --table-name lab-db-cost-sessions \ --attribute-definitions AttributeName=session_id,AttributeType=S \ --key-schema AttributeName=session_id,KeyType=HASH \ --billing-mode PROVISIONED \ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \ --region <region> aws dynamodb wait table-exists --table-name lab-db-cost-sessions --region <region>TTL reads an attribute that holds an epoch time in seconds, stored as a number.
aws dynamodb update-time-to-live \ --table-name lab-db-cost-sessions \ --time-to-live-specification "Enabled=true,AttributeName=expires_at" \ --region <region>The first command sets
EXPIREDto the epoch-seconds time one day ago, so the first item's expiry is already past. The second item stores its expiry as a string, which TTL does not read.EXPIRED=$(( $(date +%s) - 86400 )) aws dynamodb put-item --table-name lab-db-cost-sessions --region <region> \ --item '{"session_id":{"S":"expired-1"},"expires_at":{"N":"'"$EXPIRED"'"}}' aws dynamodb put-item --table-name lab-db-cost-sessions --region <region> \ --item '{"session_id":{"S":"string-expiry-1"},"expires_at":{"S":"'"$EXPIRED"'"}}'Read the expired item straight after you write it, and read the table again a few minutes later.
aws dynamodb get-item --table-name lab-db-cost-sessions --region <region> \ --key '{"session_id":{"S":"expired-1"}}'You expect the item to be gone because its time has passed. It is still returned. DynamoDB deletes expired items in a background process, typically within a few days after their expiration time, and reads can still return them until then. Remember this for the exam: TTL saves storage cost over time, but it is not an immediate delete. The string-expiry item is also still there, and it will stay there, because TTL ignores an expiry that is not a number.
On-demand mode removes the capacity setting. The table keeps its data and its TTL setting.
aws dynamodb update-table \ --table-name lab-db-cost-sessions \ --billing-mode PAY_PER_REQUEST \ --region <region>
Verify
Run these checks and compare the output with the expected results.
aws dynamodb describe-table --table-name lab-db-cost-sessions --region <region> \
--query 'Table.BillingModeSummary.BillingMode' --output text
# Expected: PAY_PER_REQUEST
aws dynamodb describe-time-to-live --table-name lab-db-cost-sessions --region <region> \
--query 'TimeToLiveDescription.[TimeToLiveStatus,AttributeName]' --output text
# Expected: ENABLED expires_at (ENABLING expires_at for the first minute)
aws dynamodb get-item --table-name lab-db-cost-sessions --region <region> \
--key '{"session_id":{"S":"string-expiry-1"}}' --query 'Item.session_id.S' --output text
# Expected: string-expiry-1 (the string expiry never triggers deletion)
If expired-1 is gone when you run get-item again, the background process has already removed it. Either result is acceptable, as long as you can explain why the item could still be there at the time of the first read.
Clean up
This lab creates one resource: the DynamoDB table lab-db-cost-sessions. Nothing else is created, so deleting that table is the whole clean-up.
aws dynamodb delete-table --table-name lab-db-cost-sessions --region <region>
aws dynamodb wait table-not-exists --table-name lab-db-cost-sessions --region <region>
aws dynamodb list-tables --region <region> --query 'TableNames' --output text
# Expected: lab-db-cost-sessions does not appear in the list
Confirm in the DynamoDB console that no tables from this lab remain in your Region.
Sources
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/data-modeling-blocks.html
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-switching-capacity-modes.html
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/CostOptimization_TableCapacityMode.html
Last reviewed