DEV Community

Cover image for Serverless Student Management System (AWS)
Muhammad Awais Zahid
Muhammad Awais Zahid

Posted on

Serverless Student Management System (AWS)

Summary

  • Create DynamoDB Table
  • Create IAM Role for Lambda
  • Lambda Function 1 – Get Students
  • Lambda Function 2 – Insert Student Data
  • Create API Using API Gateway
  • Build Frontend on S3
  • Create CloudFront Distribution

Create DynamoDB Table

Create a Table named "studentData" and Partition Key "studentid" (String). Used to store all student records securely with high availability.

Image description1

Create IAM Role for Lambda

Created a custom IAM role with AmazonDynamoDBFullAccess permission. This role is attached to both Lambda functions to allow database read/write operations.

Lambda Function 1 – Get Students

Use Lambda Runtime "Python 3.12" and Execution role "Existing IAM role with DynamoDB full access". Added code from getstudents.py and deployed. Function retrieves student data from DynamoDB.

import json
import boto3

def lambda_handler(event, context):
    # Initialize a DynamoDB resource object for the specified region
    dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

    # Select the DynamoDB table named 'studentData'
    table = dynamodb.Table('studentData')

    # Scan the table to retrieve all items
    response = table.scan()
    data = response['Items']

    # If there are more items to scan, continue scanning until all items are retrieved
    while 'LastEvaluatedKey' in response:
        response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
        data.extend(response['Items'])

    # Return the retrieved data
    return data

Enter fullscreen mode Exit fullscreen mode

Lambda Function 2 – Insert Student Data

Use Lambda Runtime "Python 3.12" and the Execution role "same IAM role". Added code from insertstudentdata.py and deployed. Function inserts new student records into DynamoDB.

import json
import boto3

# Create a DynamoDB object using the AWS SDK
dynamodb = boto3.resource('dynamodb')
# Use the DynamoDB object to select our table
table = dynamodb.Table('studentData')

# Define the handler function that the Lambda service will use as an entry point
def lambda_handler(event, context):
    # Extract values from the event object we got from the Lambda service and store in variables
    student_id = event['studentid']
    name = event['name']
    student_class = event['class']
    age = event['age']

    # Write student data to the DynamoDB table and save the response in a variable
    response = table.put_item(
        Item={
            'studentid': student_id,
            'name': name,
            'class': student_class,
            'age': age
        }
    )

    # Return a properly formatted JSON object
    return {
        'statusCode': 200,
        'body': json.dumps('Student data saved successfully!')
    }

Enter fullscreen mode Exit fullscreen mode

Image description2

Create API Using API Gateway

Use API Type "REST API" (Edge Optimized)
Created two methods:
GET → Linked to Lambda Function 1
POST → Linked to Lambda Function 2
Enabled CORS to allow browser requests.
Deployed the API and copied the invoke URL for frontend integration.

Image description3

Build Frontend on S3

Created S3 bucket and uploaded index.html + script.js.
Public access blocked (ACL disabled).
Added bucket policy allowing GetObject for CloudFront.
Enabled Static Website Hosting.

[
  {
    "AllowedHeaders": ["*"],
    "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
    "AllowedOrigins": ["*"],
    "ExposeHeaders": ["ETag"],
    "MaxAgeSeconds": 3000
  }
]

Enter fullscreen mode Exit fullscreen mode

Create CloudFront Distribution

Set index.html as the default root object.
Made the S3 bucket private for better security.
Updated bucket policy to allow CloudFront access.
CloudFront provides global caching and faster content delivery.

Image description4

OUTPUT

Image description6

Image description7

Top comments (0)