Skip to content

APIs with Lambda + API Gateway

Category Cloud & AWS Data & Serverless
Status โœ… Completed
Repo nextwork-apis-with-lambda-plus-api-gateway
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 APIs with Lambda + API Gateway 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 2 of the three-tier series is the logic tier. A Node.js Lambda handles GET /users?userId=โ€ฆ, and API Gateway exposes it as a REST API through a Lambda proxy integration. For the secret mission, the API is documented in JSON (OpenAPI 3).

Summary

Deployed the REST API (prod stage) โ†’ Lambda proxy โ†’ DynamoDB on real AWS and called the public invoke URL: 200 with the user, 404 for an unknown user, 400 with no userId. Everything was torn down afterwards.

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

Skills & tools

AWS, Serverless, Terraform, Infrastructure as Code, moto (AWS mock), AWS Lambda, Amazon API Gateway, REST APIs, Lambda proxy integration, OpenAPI, Node.js, CORS

What I built

  • lambda/index.mjs: handler returning 200, 404, or 400 with CORS headers.
  • main.tf: REST API UserRequestAPI, /users GET, AWS_PROXY integration, invoke permission, deployment, and prod stage, plus the Lambda and DynamoDB.
  • openapi.json: API documentation (parameters, response schemas, errors).
  • local-api.mjs: a tiny local API Gateway emulator for curl testing.

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

Local API: AWS_ENDPOINT_URL=<mock> node local-api.mjs then curl 'localhost:3000/users?userId=1'

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-apis-with-lambda-plus-api-gateway 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 8 CHECKS PASSED
$ ./real-extra.sh
$ curl 'https://om663t8ogb.execute-api.us-east-1.amazonaws.com/prod/users?userId=1'
{"email":"test@example.com","name":"Test User","userId":"1"}  [HTTP 200]
$ curl 'https://om663t8ogb.execute-api.us-east-1.amazonaws.com/prod/users?userId=999'
{"message":"No user data found"}  [HTTP 404]
$ curl 'https://om663t8ogb.execute-api.us-east-1.amazonaws.com/prod/users'
{"message":"userId query parameter is required"}  [HTTP 400]

Teardown

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

=== TEARDOWN 2026-12-02 20:11 CST ===
$ terraform destroy -auto-approve -input=false -no-color -var project_tag=nextwork-apis-with-lambda-plus-api-gateway
Destroy complete! Resources: 12 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-apis-with-lambda-plus-api-gateway
$ live EC2 instances / volumes / VPCs tagged Project=nextwork-apis-with-lambda-plus-api-gateway (should be empty)
=== teardown verified 20:11 CST ===

Cost

Estimated cost of the real run: $0.00 (a few API Gateway and Lambda requests, fractions of a cent). 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/2af91800-3f42-5fc1-b405-c5069180c0df ยท 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: 12 added, 0 changed, 0 destroyed.
api_id = "4h3icirzbj"
function_name = "RetrieveUserData"
invoke_url = "https://4h3icirzbj.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: OpenAPI doc describes GET /users with 200/400/404
PASS: curl through the local API -> Lambda -> DynamoDB ({"userId":"1","name":"Test User","email":"test@example.com"})
ALL 8 CHECKS PASSED

[exit code 0]