Building a Full-Stack Serverless App on AWS: A Practical Walkthrough (with the Mistakes I’d Avoid)
A hands-on walkthrough of building a secure, full-stack serverless task tracker on AWS. You’ll learn how to design a DynamoDB table around access patterns, add authentication with Cognito, protect an HTTP API with a JWT authorizer, write a least-privilege Lambda backend, and host the front end on S3 and CloudFront. It also covers the CORS, IAM, and token errors that tripped me up, plus tips for keeping costs close to zero.
Why serverless?
If you’re a student, a freelancer, or a small team, you probably don’t want to patch servers or pay for a machine that sits idle at 3 a.m. Serverless lets you pay only for what you use, and for low-traffic apps that is often close to nothing.
In this article I’ll build a small task tracker with sign-up, login, and per-user tasks. We’ll use:
- Amazon Cognito for authentication
- Amazon API Gateway (HTTP API) as the front door
- AWS Lambda for the backend logic
- Amazon DynamoDB for storage
- Amazon S3 + CloudFront to host the front end
Architecture
Browser ──► CloudFront ──► S3 (static React/HTML app) │ └──► API Gateway (HTTP API, JWT authorizer = Cognito) │ └──► Lambda ──► DynamoDBThe flow:
- The user signs in through Cognito and gets a JWT.
- The front end sends the JWT in the
Authorizationheader.
- API Gateway validates the token before any Lambda runs, so unauthenticated requests never cost you compute.
- Lambda reads and writes DynamoDB, using the user’s ID from the token.
Step 1: Design the DynamoDB table first
This is the step people rush, and it’s the hardest to change later. Start from your access patterns:
- Get all tasks for a user
- Create, update, and delete a task
A single-table design works well here:
Attribute | Value |
|---|---|
PK (partition key) | USER#<userId> |
SK (sort key) | TASK#<taskId> |
title , status , createdAt | task data |
Fetching all of a user’s tasks becomes one efficient
Query on PK , with no table scans. Use on-demand capacity so you don’t have to guess throughput.Step 2: Set up authentication with Cognito
Create a User Pool and an App Client (without a client secret, since it’s a browser app). Enable email sign-up and verification.
You don’t write password hashing, token issuing, or email verification yourself. That’s a big security and time saving.
Step 3: Write the Lambda function
A minimal handler that lists the current user’s tasks (Node.js):
javascript
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
const db = DynamoDBDocumentClient.from(new DynamoDBClient({}));
export const handler = async (event) => { // API Gateway puts the verified JWT claims here const userId = event.requestContext.authorizer.jwt.claims.sub;
const result = await db.send(new QueryCommand({ TableName: process.env.TABLE_NAME, KeyConditionExpression: "PK = :pk AND begins_with(SK, :sk)", ExpressionAttributeValues: { ":pk": `USER#${userId}`, ":sk": "TASK#", }, }));
return { statusCode: 200, headers: { "Content-Type": "application/json" }, body: JSON.stringify(result.Items), };};Two things to note:
- The user ID comes from the verified token, never from the request body. This prevents one user from reading another’s data.
- Create the DynamoDB client outside the handler so warm invocations reuse it.
Step 4: Wire up API Gateway
Use an HTTP API rather than a REST API. It’s simpler and cheaper, and it has a built-in JWT authorizer. Point the authorizer at your Cognito User Pool, then create routes such as
GET /tasks , POST /tasks , and DELETE /tasks/{id} , each integrated with a Lambda function.Step 5: Host the front end
Build your front end and upload it to a private S3 bucket. Put CloudFront in front using Origin Access Control, so the bucket isn’t public but the site is served over HTTPS globally.
Step 6: Least-privilege IAM
Give each Lambda function only the DynamoDB actions it needs, scoped to your table’s ARN. For example, the “list tasks” function gets
dynamodb:Query only, not dynamodb:* . It takes five extra minutes and limits the damage if a function is ever compromised.What went wrong (the useful part)
1. CORS errors. The classic. The browser blocks calls to your API until you configure CORS on API Gateway: allowed origin (your CloudFront domain), headers (
Authorization , Content-Type ), and methods. Check this first when requests fail in the browser but work in curl.2. Using the wrong token. Cognito issues an ID token and an access token. Make sure your authorizer’s audience configuration matches the token you actually send.
3. IAM “AccessDenied” in Lambda. Almost always a missing permission or the wrong table ARN. Read the CloudWatch logs; the error names the exact action that was denied.
4. Cold starts. The first request after idle time is slower. For a task tracker it’s barely noticeable. Keep your deployment package small and avoid heavy dependencies to keep it low.
5. Designing the table like SQL. My first instinct was a table per entity. DynamoDB rewards designing around queries, not entities.
What does it cost?
For a low-traffic app, you’ll likely stay inside the free tier or pay very little. Lambda and DynamoDB both have always-free allowances, and HTTP API pricing is charged per million requests. S3 and CloudFront costs depend on your storage and traffic. Pricing changes, so plug your own numbers into the AWS Pricing Calculator and set up a billing alarm before you launch anything.
What I’d do next
- Define the infrastructure as code with AWS SAM or CDK so the whole stack is reproducible
- Add a CI/CD pipeline
- Add structured logging and a CloudWatch dashboard
- Add input validation and rate limiting
Key takeaways
- Design DynamoDB around access patterns before writing any code
- Let Cognito and API Gateway handle auth, so unauthorized requests never reach your code
- Read the user’s identity from the token, never from user input
- Apply least-privilege IAM from day one
- Set a billing alarm before you deploy
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article