This project demonstrates a serverless web application built on AWS. It uses AWS Lambda, Amazon API Gateway, DynamoDB, Amazon S3, and CloudFront to create a dynamic student data website. Users can add new student records and view all stored student information—all without managing any underlying servers.
- Overview
- Architecture
- Prerequisites
- Deployment Steps
- Project Structure
- Usage
- Troubleshooting
- License
This repository contains the source code for a serverless student data web application. The application lets users:
- Add student data: Each record includes Student ID, Name, Class, and Age.
- View all student data: Retrieve stored records from DynamoDB via a GET API call.
The front-end (HTML, CSS, and JavaScript) is hosted on an S3 bucket configured for static website hosting, while secure delivery over HTTPS is achieved through CloudFront. The back-end is powered by AWS Lambda functions that interact with a DynamoDB table, and Amazon API Gateway is used to expose REST endpoints.
The application leverages AWS serverless services:
- Amazon S3: Hosts static files such as
index.htmlandscripts.js. - Amazon API Gateway: Provides RESTful endpoints that trigger Lambda functions.
- AWS Lambda:
getStudents.py: Retrieves student records from DynamoDB.
:contentReference[oaicite:0]{index=0}:contentReference[oaicite:1]{index=1}insertStudentData.py: Inserts new student records into DynamoDB.
:contentReference[oaicite:2]{index=2}:contentReference[oaicite:3]{index=3}
- Amazon DynamoDB: A NoSQL database that stores student data.
- CloudFront: Delivers the S3-hosted web content securely over HTTPS.
- An active AWS Account
- Basic knowledge of AWS services (S3, Lambda, API Gateway, DynamoDB, and CloudFront)
- AWS CLI or AWS Management Console access
- IAM roles with necessary permissions for Lambda functions and DynamoDB access
- Create a DynamoDB table named studentData.
- Define the partition key (e.g.,
studentid) and include any additional keys as required. - Adjust table settings if necessary.
Create two Lambda functions using the AWS Lambda console (or your preferred deployment tool):
-
Get Student Data Function
- Use the code from
getStudents.pywhich scans the studentData table to retrieve all records.
:contentReference[oaicite:4]{index=4}:contentReference[oaicite:5]{index=5}
- Use the code from
-
Insert Student Data Function
- Use the code from
insertStudentData.pywhich writes a new record to the studentData table.
:contentReference[oaicite:6]{index=6}:contentReference[oaicite:7]{index=7}
- Use the code from
Ensure both functions have the proper IAM roles to interact with DynamoDB.
- Create a new REST API in API Gateway.
- Define the following methods:
- GET Method: Integrate with the
getStudentsLambda function. - POST Method: Integrate with the
insertStudentDataLambda function.
- GET Method: Integrate with the
- Deploy the API to a stage (e.g.,
prod) and note the invoke URL. - Update the API endpoint in
scripts.jswith the deployed URL.
:contentReference[oaicite:8]{index=8}:contentReference[oaicite:9]{index=9}
- Create an S3 bucket to host your static web files.
- Upload
index.htmlandscripts.jsto the bucket.
:contentReference[oaicite:10]{index=10}:contentReference[oaicite:11]{index=11} :contentReference[oaicite:12]{index=12}:contentReference[oaicite:13]{index=13} - Enable static website hosting on the bucket and set the index document (e.g.,
index.html). - Update the bucket policy to allow public read access for the hosted files.
- Create a new CloudFront distribution with the S3 bucket as the origin.
- Configure CloudFront to serve content over HTTPS.
- Update the S3 bucket policy to allow CloudFront to access your objects.
- Once deployed, use the CloudFront distribution’s domain name to access your secure website.
This guide walks you through the process of deploying a serverless student data web application on AWS. The project leverages AWS DynamoDB, Lambda, API Gateway, S3, and CloudFront to create a scalable and secure solution.
- Part 1: Setting Up the Back-End
- Part 2: Creating the API Gateway and Hosting the Front-End
- Part 3: Securing Your Application with CloudFront
- Conclusion
- Log in to the AWS Management Console.
- Navigate to DynamoDB.
- Click Create table.
- Set the table name as
studentData. - Define the Partition key as
studentid(this key uniquely identifies each student record). - Leave the default settings (or adjust based on your requirements).
- Click Create and wait for the table status to become active.
-
Open the AWS Lambda console.
-
Click Create function and select Author from scratch.
-
Name the function
getStudentData. -
Choose Python 3.x as the runtime.
-
Under Permissions, use the default execution role or create one with DynamoDB access.
-
Replace the default code with the following (
getStudents.py):import json import boto3 def lambda_handler(event, context): dynamodb = boto3.resource('dynamodb', region_name='us-east-1') table = dynamodb.Table('studentData') response = table.scan() data = response['Items'] while 'LastEvaluatedKey' in response: response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey']) data.extend(response['Items']) return data
-
Click Deploy.
-
Create a new Lambda function named
insertStudentData. -
Choose Python 3.x and ensure the execution role has permission to write to DynamoDB.
-
Replace the default code with the following (
insertStudentData.py):import json import boto3 dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('studentData') def lambda_handler(event, context): student_id = event['studentid'] name = event['name'] student_class = event['class'] age = event['age'] response = table.put_item( Item={ 'studentid': student_id, 'name': name, 'class': student_class, 'age': age } ) return { 'statusCode': 200, 'body': json.dumps('Student data saved successfully!') }
-
Click Deploy.
- For
insertStudentData, create a test event with the following sample data:{ "studentid": "1", "name": "Sur", "class": "A", "age": "22" } - Run the test to confirm that the data is inserted into the DynamoDB table.
- Test
getStudentDatato verify it returns the expected list of student records.
- Open the Amazon API Gateway console.
- Create a new REST API (e.g., named
StudentAPI). - Create a new resource (or use the root resource).
- Add a GET method:
- Set the integration type to Lambda Function.
- Select the
getStudentDataLambda function.
- Add a POST method:
- Set the integration type to Lambda Function.
- Select the
insertStudentDataLambda function.
- Deploy the API:
- Click Deploy API.
- Create a new stage (e.g.,
prod). - Copy the Invoke URL for later use.
-
Open the
scripts.jsfile. -
Replace the placeholder API endpoint with the API Gateway Invoke URL:
var API_ENDPOINT = "https://<your-api-id>.execute-api.<region>.amazonaws.com/prod";
-
Save the changes.
- Navigate to the Amazon S3 console.
- Create a new S3 bucket (e.g.,
devopsMasterBucket). - Upload your static files (
index.htmlandscripts.js) to the bucket. - Enable Static Website Hosting:
- Set the index document to
index.html.
- Set the index document to
- Open the S3 website URL.
- Test form submission and data retrieval.
- Open the CloudFront console.
- Create a new distribution.
- Set the Origin Domain Name to your S3 bucket.
- Configure security settings.
- Modify the S3 bucket policy to restrict access via CloudFront.
- Access the site through CloudFront.
- Verify HTTPS and application functionality.
You have successfully deployed a serverless student data web application on AWS. The system is secure, scalable, and ready for use. 🚀
├── index.html # Static front-end webpage
├── scripts.js # JavaScript for AJAX API calls
├── getStudents.py # Lambda function for retrieving student data
└── insertStudentData.py # Lambda function for inserting student data
- index.html: Contains the HTML markup and basic styling for the student data interface.
:contentReference[oaicite:14]{index=14}:contentReference[oaicite:15]{index=15} - scripts.js: Implements AJAX calls to the API Gateway endpoints to save and retrieve student data.
:contentReference[oaicite:16]{index=16}:contentReference[oaicite:17]{index=17} - getStudents.py and insertStudentData.py: Python-based Lambda functions that interact with DynamoDB.
:contentReference[oaicite:18]{index=18}:contentReference[oaicite:19]{index=19} :contentReference[oaicite:20]{index=20}:contentReference[oaicite:21]{index=21}
-
Adding Student Data:
- Open the website hosted via CloudFront.
- Fill in the Student ID, Name, Class, and Age in the form.
- Click Save Student Data to submit. The data is sent via a POST request to API Gateway, triggering the Lambda function to insert the record.
-
Viewing Student Data:
- Click View all Students to fetch the records.
- The GET request retrieves the list of students from DynamoDB and displays it in a table.
- API Endpoint Issues: Ensure the API Gateway URL in
scripts.jsis updated after deployment. - Permission Errors: Verify that Lambda functions have the correct IAM roles to access DynamoDB.
- S3 Access: Check that the bucket policy allows public access (or access via CloudFront) and that static website hosting is properly enabled.
- CloudFront: Allow time for distribution changes to propagate. If the website is not loading over HTTPS, verify the CloudFront settings and S3 bucket policy.
This project is licensed under the MIT License. See the LICENSE file for details.
