This content originally appeared on DEV Community and was authored by Rashmitha v
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
Components of the Serverless Architecture
- 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).
- Define table attributes and configure read/write capacity settings or use on-demand capacity mode.
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
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
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!')
}
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.
Deploy your API to a stage (e.g., dev) and note down the Invoke URL provided by API Gateway.
Invoke URL will trigger the lambda
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.
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.
Our application is deployed!
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
This content originally appeared on DEV Community and was authored by Rashmitha v
Rashmitha v | Sciencx (2024-07-08T21:11:26+00:00) Deploying a serverless web application on S3, API gateway, lambda, DynamoDB. Retrieved from https://www.scien.cx/2024/07/08/deploying-a-serverless-web-application-on-s3-api-gateway-lambda-dynamodb/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.