Fetch Data with AWS Lambda¶
| Category | Cloud & AWS Data & Serverless |
| Status | โ Completed |
| Repo | nextwork-fetch-data-with-aws-lambda |
| 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 Fetch Data with AWS Lambda 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¶
Part 3 of the three-tier series is the data tier. A DynamoDB UserData table holds profiles, and a Node.js Lambda fetches a user by userId. You test it with a test event, then secure it. The guide's secret mission replaces the broad AmazonDynamoDBReadOnlyAccess with an inline policy allowing only GetItem on this one table.
Summary¶
Deployed the RetrieveUserData Lambda (Node.js 20) with a GetItem-only IAM policy and the UserData table on real AWS. aws lambda invoke returned the user for userId 1 and null for an unknown user. Everything was torn down, including the CloudWatch log group Lambda creates on its own.
The checks behind this are in docs/verification.txt.
Skills & tools¶
AWS, Serverless, Terraform, Infrastructure as Code, moto (AWS mock), AWS Lambda, Node.js, AWS SDK v3, Amazon DynamoDB, IAM inline policies, Least privilege
What I built¶
lambda/index.mjs: the handler (SDK v3GetCommand), with logging as in the guide.lambda/test.mjs: the guide's test event{"userId": "1"}plus a missing-user case.main.tf: the UserData table, a Lambda zipped byarchive_file, an execution role, and the inlineUserDataGetItemOnlypolicy.
How to run¶
(cd lambda && npm install) && 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.
(cd lambda && npm install --silent --no-audit --no-fund >/dev/null) && 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-fetch-data-with-aws-lambda 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. (In the transcript, the label "tests pass against the mocked DynamoDB" is just the name of the local test. In this run it pointed at real DynamoDB.)
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 3 CHECKS PASSED
$ ./real-extra.sh
$ aws lambda invoke --function-name RetrieveUserData --payload '{"userId":"1"}'
200
response: {"email":"test@example.com","name":"Test User","userId":"1"}
$ aws lambda invoke --function-name RetrieveUserData --payload '{"userId":"2"}'
200
response: null
Teardown¶
Resources were up from 20:08 to 20:11 CST on 2026-12-02, then destroyed and verified gone:
=== TEARDOWN 2026-12-02 20:09 CST ===
$ terraform destroy -auto-approve -input=false -no-color -var project_tag=nextwork-fetch-data-with-aws-lambda
Destroy complete! Resources: 5 destroyed.
$ terraform state list (should be empty)
$ aws resourcegroupstaggingapi get-resources --tag-filters Key=Project,Values=nextwork-fetch-data-with-aws-lambda
$ live EC2 instances / volumes / VPCs tagged Project=nextwork-fetch-data-with-aws-lambda (should be empty)
=== teardown verified 20:10 CST ===
$ aws logs delete-log-group --log-group-name /aws/lambda/RetrieveUserData (auto-created by Lambda on first invoke, not managed by Terraform; deleted 20:11 CST)
Cost¶
Estimated cost of the real run: $0.00 (a handful of Lambda invocations and DynamoDB requests, inside the always-free tiers). 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/465096ce-f136-53fb-8a99-ebab7c406cd9 ยท Part of my NextWork Learning portfolio (hub).
Verification output¶
From docs/verification.txt:
$ (cd lambda && npm install --silent --no-audit --no-fund >/dev/null) && 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: 5 added, 0 changed, 0 destroyed.
function_name = "RetrieveUserData"
role_name = "RetrieveUserData-role"
table_arn = "arn:aws:dynamodb:us-east-1:123456789012:table/UserData"
PASS: Lambda deployed with nodejs20.x runtime
PASS: inline policy: only dynamodb:GetItem on the UserData table
DynamoDB Response: {"Item":{"userId":"1","name":"Test User","email":"test@example.com"},"$metadata":{"httpStatusCode":200,"requestId":"HeziU2Wt91lymuZ5VEMl5Legg1dSdeigo9cqZ0pfdTAntl09fWSu","attempts":1,"totalRetryDelay":0}}
User data retrieved: { userId: '1', name: 'Test User', email: 'test@example.com' }
PASS: test event {userId: '1'} returned {"userId":"1","name":"Test User","email":"test@example.com"}
DynamoDB Response: {"$metadata":{"httpStatusCode":200,"requestId":"k9ky4Ut1ZwdR2x0zq0LjDri9VHxdzmNjyezb3RZUMYogPzmSfyHA","attempts":1,"totalRetryDelay":0}}
No user data found.
PASS: missing user returns null
PASS: Node.js handler tests pass against the mocked DynamoDB
ALL 3 CHECKS PASSED
[exit code 0]