Skip to content

Build a Three-Tier Web App

Category Cloud & AWS Data & Serverless
Status ๐ŸŸก Partially completed
Repo nextwork-build-a-three-tier-web-app
Original NextWork guide nextwork.ai
Proof Official NextWork write-up: web ยท PDF

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 Build a Three-Tier Web App guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ Cloud & AWS Data & Serverless
Status: ๐ŸŸก Partial

Proof: Official NextWork write-up: web ยท PDF

Overview

The capstone of the series wires all three tiers together. A static site (S3 + CloudFront) calls a REST API (API Gateway + Lambda) that reads user data from DynamoDB. The guide's big debugging lesson is CORS: the browser blocks the API response unless Lambda returns Access-Control-Allow-Origin. Here that's handled in the handler, with ALLOWED_ORIGIN configurable.

Summary

The logic and data tiers ran on real AWS: API Gateway (prod) โ†’ Lambda โ†’ DynamoDB answered 200, 404 and 400 correctly, and the frontend's own fetchUser() worked against the live URL. The presentation tier's files were uploaded to a private S3 bucket. AWS won't create the CloudFront distribution until the new account is verified through AWS Support, so CloudFront was skipped (-target excludes it). Everything was torn down.

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

Skills & tools

AWS, Serverless, Terraform, Infrastructure as Code, moto (AWS mock), Three-tier architecture, Amazon S3, Amazon CloudFront, Amazon API Gateway, AWS Lambda, Amazon DynamoDB, CORS, JavaScript, Node.js

What I built

  • site/: the frontend (form โ†’ fetchUser() โ†’ renders JSON or the error).
  • lambda/index.mjs: the logic tier with CORS headers.
  • main.tf: one stack with all tiers (bucket, OAC, distribution, REST API, Lambda, least-privilege role, UserData table).
  • e2e.mjs: drives the real frontend function against the local API emulator and the mocked DynamoDB.

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

After a real deploy, set site/config.js โ†’ window.API_URL = "<invoke_url>" and re-upload.

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-build-a-three-tier-web-app 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. This run used -target to leave out the CloudFront distribution and its bucket policy. My first targeted run missed the IAM policy and the table, so the Lambda returned 500. The second was torn down before the stage had propagated. The third (shown here) is clean. The ledger records all three.

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 8 CHECKS PASSED
$ ./real-extra.sh
$ curl 'https://g2sx8zv8fh.execute-api.us-east-1.amazonaws.com/prod/users?userId=1'
{"email":"test@example.com","name":"Test User","userId":"1"}  [HTTP 200]
$ curl 'https://g2sx8zv8fh.execute-api.us-east-1.amazonaws.com/prod/users?userId=404'
{"message":"No user data found"}  [HTTP 404]
$ curl 'https://g2sx8zv8fh.execute-api.us-east-1.amazonaws.com/prod/users'
{"message":"userId query parameter is required"}  [HTTP 400]
--- frontend script.js fetchUser() against the LIVE API
fetchUser("1") -> {"email":"test@example.com","name":"Test User","userId":"1"}
fetchUser("404") -> error: No user data found
--- presentation tier files in the (private) S3 bucket
2026-12-02 20:16:06        149 config.js
2026-12-02 20:16:06        739 index.html
2026-12-02 20:16:06        758 script.js
2026-12-02 20:16:06        554 style.css

Teardown

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

=== TEARDOWN 2026-12-02 20:16 CST ===
$ terraform destroy -auto-approve -input=false -no-color -var project_tag=nextwork-build-a-three-tier-web-app -target=aws_dynamodb_table.users -target=aws_iam_role.fn -target=aws_iam_role_policy_attachment.logs -target=aws_iam_role_policy.ddb -target=aws_lambda_function.fn -target=aws_api_gateway_rest_api.api -target=aws_api_gateway_resource.users -target=aws_api_gateway_method.get -target=aws_api_gateway_integration.lambda -target=aws_lambda_permission.apigw -target=aws_api_gateway_deployment.d -target=aws_api_gateway_stage.prod -target=random_id.suffix -target=aws_s3_bucket.site -target=aws_s3_bucket_public_access_block.site -target=aws_s3_object.site
Destroy complete! Resources: 19 destroyed.
$ aws logs delete-log-group --log-group-name /aws/lambda/RetrieveUserData   (auto-created by Lambda, not managed by Terraform)
$ terraform state list   (should be empty)
$ aws resourcegroupstaggingapi get-resources --tag-filters Key=Project,Values=nextwork-build-a-three-tier-web-app
$ live EC2 instances / volumes / VPCs tagged Project=nextwork-build-a-three-tier-web-app (should be empty)
=== teardown verified 20:17 CST ===

Cost

Estimated cost of the real run: $0.00 (a few API Gateway, Lambda and DynamoDB requests; S3 storage for minutes). 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.

What isn't done, and why

The CloudFront distribution in front of the S3 bucket. AWS requires new accounts to be verified through an AWS Support case before they can create CloudFront resources, and only the account owner can open that case. After verification, ./real-aws-run.sh . with no -target flags deploys all three tiers, and the CloudFront checks in checks.py run automatically.


Original project: https://nextwork.ai/projects/f10f5983-0a15-5628-94b2-1cc37b19cb8c ยท 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: 22 added, 0 changed, 0 destroyed.
api_id = "sfmq8ajlnk"
bucket = "nextwork-three-tier-3506c715"
cdn_domain = "tl1feytqudzvm.cloudfront.net"
distribution_arn = "arn:aws:cloudfront::123456789012:distribution/G3I5EPRGO7IVL"
distribution_id = "G3I5EPRGO7IVL"
function_name = "RetrieveUserData"
invoke_url = "https://sfmq8ajlnk.execute-api.us-east-1.amazonaws.com/prod/users"
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
PASS: GET /users?userId=1 -> 200 {"userId":"1","name":"Test User","email":"test@example.com"}
PASS: unknown user -> 404
PASS: missing userId -> 400

PASS: Node.js handler tests pass against the mocked DynamoDB
PASS: API has /users resource with GET
PASS: GET /users uses Lambda proxy integration
PASS: deployed to the prod stage
PASS: CloudFront serves index.html by default
PASS: viewers are redirected to HTTPS
PASS: origin is the S3 bucket via OAC
PASS: bucket blocks all public access
PASS: bucket policy: only this distribution may GetObject
PASS: website files uploaded (['config.js', 'index.html', 'script.js', 'style.css'])
local API on http://localhost:3918/users
PASS: frontend fetchUser('1') -> {"userId":"1","name":"Test User","email":"test@example.com"}
PASS: frontend surfaces the API's 404 message

PASS: end-to-end: frontend -> API -> Lambda -> DynamoDB
ALL 13 CHECKS PASSED

[exit code 0]