Skip to content

Query Data with DynamoDB

Category Cloud & AWS Data & Serverless
Status โœ… Completed
Repo nextwork-query-data-with-dynamodb
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's Query Data with DynamoDB guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ Cloud & AWS Data & Serverless
Status: โœ… Completed

Overview

This project gets insights out of the data. You query by partition key, narrow with sort-key conditions, query a secondary index, filter, and then use a transaction to update two tables at once: add a comment and increment the forum's message counter, all-or-nothing.

Summary

On real DynamoDB I ran GetItem with a projection, partition-key queries, a GSI query, a FilterExpression, and a transaction, including a failed condition that cancels the whole transaction. 12/12 checks passed, then everything was torn down.

The checks behind this are in docs/verification.txt.

Skills & tools

AWS, Terraform, Infrastructure as Code, moto (AWS mock), Amazon DynamoDB, Query vs Scan, Global secondary indexes, Filter expressions, Transactions (ACID), boto3, Python

What I built

  • queries.py: get_project (projection + consistent read), comments_on_post (key query), comments_by_user_since (GSI + sort-key range), posts_in_forum_with_min_views (filter expression), and add_comment_transaction (TransactWriteItems across Comment + Forum with condition checks).
  • checks.py: asserts each query result, the transaction's effect, and that a failed condition rolls back the whole transaction.

How to run

pip install -r requirements-dev.txt   # moto (local AWS mock) + boto3
./local-test.sh                       # terraform apply against the mock, run checks.py, then destroy

# Real AWS (costs money; use your own IAM user/profile):
terraform init && terraform apply      # mock_endpoint defaults to "" = real AWS
terraform destroy                      # clean up when done

Verification

I ran the verification command on 2026-12-02. The full output is in docs/verification.txt.

PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH TF_PLUGIN_CACHE_DIR=/workspace/nextwork-portfolio/tf-plugin-cache ./local-test.sh

Real AWS deployment (2026-12-02)

I deployed this project to a real, dedicated AWS learning account (us-east-1, kept separate from everything else I run), checked it, captured the output, and tore it all down right after. Every resource carried the tag Project=nextwork-query-data-with-dynamodb so I could prove nothing was left behind.

How I ran it: ./real-aws-run.sh (in this repo) does terraform init/apply with my AWS profile, runs checks.py against the real account, runs real-extra.sh for the live tests, and then always runs terraform destroy, even if a step fails.

Evidence (excerpt; the full, sanitized transcript is in docs/aws-real-run.txt, with the account ID redacted):

$ python3 checks.py   (against the real account)
ALL 12 CHECKS PASSED
$ ./real-extra.sh
{
    "UnprocessedItems": {}
}
{
    "UnprocessedItems": {}
}
{
    "UnprocessedItems": {}
}
{
    "UnprocessedItems": {}
}
{
    "Item": {
        "Title": {
            "S": "Build a Virtual Private Cloud"
        "Price": {
            "N": "0"
        "ContentType": {
            "S": "Project"
        "Services": {
            "SS": [
                "Amazon VPC"
        "Published": {
            "BOOL": true
        "Id": {
            "N": "1"
        "Difficulty": {
            "S": "Easy peasy"
}
--- item counts per table
ContentCatalog: 5 items
Forum: 2 items
Post: 3 items
Comment: 4 items
--- query: all comments on one post (partition-key query)
2026-11-20T10:05:00Z    Query when you know the partition key.  User B
2026-11-20T11:00:00Z    Scan reads the whole table, avoid it.   User C

Teardown

Resources were up from 20:06 to 20:08 CST on 2026-12-02, then destroyed and verified gone:

=== TEARDOWN 2026-12-02 20:07 CST ===
$ terraform destroy -auto-approve -input=false -no-color -var project_tag=nextwork-query-data-with-dynamodb
Destroy complete! Resources: 4 destroyed.
$ terraform state list   (should be empty)
$ aws resourcegroupstaggingapi get-resources --tag-filters Key=Project,Values=nextwork-query-data-with-dynamodb
$ live EC2 instances / volumes / VPCs tagged Project=nextwork-query-data-with-dynamodb (should be empty)
=== teardown verified 20:08 CST ===

Cost

Estimated cost of the real run: $0.00 (on-demand tables, inside the always-free tier). The account has a $50/month budget with email alerts at $5, $20 and $40, and the whole AWS series is logged in a private spend ledger.


Original project: https://nextwork.ai/projects/0001cf4c-8256-52ae-b09c-6e99ed36462f ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH TF_PLUGIN_CACHE_DIR=/workspace/nextwork-portfolio/tf-plugin-cache ./local-test.sh

Success! The configuration is valid.

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
tables = [
  "ContentCatalog",
  "Forum",
  "Post",
  "Comment",
]
PASS: batch-write loaded all 4 tables ({'Comment': 3, 'ContentCatalog': 5, 'Forum': 2, 'Post': 3})
PASS: Comment scan returns 3 items
PASS: ContentCatalog scan returns 5 items
PASS: Forum scan returns 2 items
PASS: Post scan returns 3 items
PASS: item edit persisted (flexible schema: new attribute added)
PASS: GetItem with projection returns only requested attributes (['ContentType', 'Services', 'Title'])
PASS: Query by partition key returns the 2 comments in sort order
PASS: GSI query with sort-key condition
PASS: Query + FilterExpression (Views >= 100)
PASS: transaction wrote comment AND incremented Forum.Messages
PASS: failed condition cancels the WHOLE transaction (no orphan comment)
ALL 12 CHECKS PASSED

[exit code 0]