Deploying a serverless web application on S3, API gateway, lambda, DynamoDB

Introduction:
Deploying a serverless web application using AWS services such as S3, API Gateway, Lambda, and DynamoDB is a streamlined and cost-effective approach for building scalable applications without managing traditional server infrastructure. This setup leverages cloud-native services that handle scaling, security, and availability automatically, allowing developers to focus on application logic rather than infrastructure maintenance.

Serverless Architecture

Image description

Components of the Serverless Architecture

  1. AWS DynamoDB:

Fully managed NoSQL database service to store and retrieve data at scale.

  • Create a DynamoDB table (your-dynamodb-table-name) with a primary key (e.g., studentid).

Image description

  • Define table attributes and configure read/write capacity settings or use on-demand capacity mode.

Image description

Backend Deployment (Lambda, API Gateway, DynamoDB):
2.Lambda function

  • Write Lambda functions to handle CRUD operations (GET, PUT, POST, DELETE) on DynamoDB data
    create a function
    Image description

write code
Lambda function (Python) for handling GET requests.

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-2') # 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 (Python) for handling POST requests

# 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

Deploy the code
Image description

Create API gateway

-Create RESTful APIs to trigger Lambda functions and integrate with backend services.

  • Create API endpoints (GET, POST, PUT, DELETE) in API Gateway that trigger your Lambda functions.
    Set up CORS (Cross-Origin Resource Sharing) settings if your frontend is hosted on a different domain.

Image description
Deploy API:

Deploy your API to a stage (e.g., dev) and note down the Invoke URL provided by API Gateway.

Image description

Invoke URL will trigger the lambda

Image description

Setting Up AWS S3
Create an S3 Bucket:

Go to the AWS Management Console and navigate to S3.
Click on “Create bucket” and follow the wizard to create a bucket (e.g., your-bucket-name).

Upload Static Web Content:

Upload your web application files (e.g., index.html, style.css, script.js, images) to the S3 bucket.
Select the uploaded files and make them public by setting the permissions to allow public read access.

Image description

Enable Static Website Hosting:

In the bucket properties, navigate to “Static website hosting”.
Select “Use this bucket to host a website” and enter index.html as the Index document.

Image description
Accessing DynamoDB Data:

Image description

Our application is deployed!

Image description

Image description

Conclusion
Deploying a serverless web application using S3, API Gateway, Lambda, and DynamoDB offers scalability, cost-efficiency, and ease of maintenance. By leveraging these AWS services, developers can focus more on building application logic and less on managing infrastructure. This architecture is ideal for modern web applications that require flexibility, scalability, and seamless integration with backend services like DynamoDB


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top