In the modern era of cloud computing, Serverless Architecture has become the gold standard for developers who want to build, deploy, and scale applications without the headache of managing servers.
Today, we are going to build a Serverless Quiz Application. This project isn't just a coding exercise; it's a deep dive into the "Big Three" of AWS serverless services: AWS Lambda (Compute), Amazon DynamoDB (Database), and Amazon API Gateway (Networking).
Why Serverless for a Quiz App?
Traditional hosting requires paying for idle server time. A quiz app, however, might have spikes in traffic (during a live event) and zero traffic at night.
- 💰Cost-Efficiency: You only pay when a user fetches a question or submits an answer (AWS Free Tier offers 1 million free Lambda requests per month).
- 📈Scalability: Whether 10 or 10,000 students take the quiz simultaneously, AWS handles the scaling automatically.
- 🔧Zero Maintenance: No OS patching, no server updates.
The Architecture
Before we touch the console, let's look at the flow:
- Frontend: A static website hosted on Amazon S3 and delivered via CloudFront.
- API Layer: API Gateway acts as the front door, routing HTTP requests.
- Logic Layer: AWS Lambda processes the logic (fetching questions/calculating scores).
- Data Layer: DynamoDB stores quiz questions, options, and results.
Flow: User → API Gateway → Lambda → DynamoDB
Step 1: Setting Up the Data Store (DynamoDB)
DynamoDB is a NoSQL database known for single-digit millisecond latency.
- Navigate to the DynamoDB Console.
- Click Create Table.
- Table Name: QuizQuestions
- Partition Key: questionId (String).
- Keep default settings and click Create.
Sample Data Item:
{
"questionId": "q1",
"questionText": "What is the capital of France?",
"options": ["London", "Berlin", "Paris", "Madrid"],
"correctAnswer": "Paris"
}📚 Resource:
Read more on DynamoDB Core Components in the AWS documentation.
Step 2: Creating the Backend Logic (AWS Lambda)
We will create a function that fetches a quiz question from our table.
- Go to the AWS Lambda Console.
- Click Create Function → Author from scratch.
- Runtime: Node.js 18.x or Python 3.9.
- Execution Role: Create a new role with basic Lambda permissions.
The Code (Node.js)
This function connects to DynamoDB and returns a random question.
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const params = {
TableName: "QuizQuestions",
Key: {
"questionId": event.queryStringParameters.id
}
};
try {
const data = await docClient.get(params).promise();
return {
statusCode: 200,
headers: { "Access-Control-Allow-Origin": "*" }, // Handle CORS
body: JSON.stringify(data.Item),
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify(err),
};
}
};⚠️ Crucial Step:
Go to the Configuration tab > Permissions and click on the Role Name. Attach the AmazonDynamoDBReadOnlyAccess policy so your Lambda can actually read from your table.
Step 3: Creating the REST API (API Gateway)
API Gateway transforms our Lambda function into a URL that our frontend can call.
- In the API Gateway Console, select HTTP API.
- Click Add Integration and select Lambda. Choose the function you just created.
- Configure Routes:
- Method: GET
- Resource Path: /get-question
- CORS Configuration: This is vital. Enable CORS so your frontend domain can communicate with the API.
- Deploy the API and copy the Invoke URL.
📚 Resource:
Understanding API Gateway Endpoint Types in the AWS documentation.
Step 4: Optional - Deploying the Frontend (S3 + CloudFront)
While you can test your API using Postman, a real project needs a UI.
- S3 Bucket: Create a bucket named
my-quiz-app-frontend. Upload your index.html and script.js. - Static Website Hosting: Enable this in S3 properties.
- CloudFront: Create a distribution and point the "Origin" to your S3 bucket. This provides:
- HTTPS: Secure your site with an SSL certificate.
- Speed: Cached content at Edge Locations worldwide.
Tips for Real-World Success
Security First
Never hardcode credentials. Use IAM Roles for service-to-service communication. For user authentication (making sure only logged-in users can take the quiz), integrate Amazon Cognito.
Monitoring
Check AWS CloudWatch Logs if your Lambda fails. It's the first place to look for "Access Denied" errors or syntax bugs.
Environment Variables
Keep your DynamoDB table name in an environment variable within Lambda. This makes it easier to switch between Dev, Test, and Prod environments.
Conclusion
You've just built a fully functional, production-ready serverless application. You've mastered data storage with DynamoDB, compute logic with Lambda, and networking with API Gateway.
This architecture isn't just for quizzes; it's the foundation for everything from e-commerce checkouts to IoT data processing.
🚀 Ready to take the next step?
Try adding a second Lambda function to POST user answers and store their final scores in a new DynamoDB table!