Skip to main content

Command Palette

Search for a command to run...

AWS Serverless Project: Video Upload and Playback Application

Updated
โ€ข3 min read
AWS Serverless Project: Video Upload and Playback Application
P

PRAFUL PATEL โ˜๏ธ๐Ÿš€, Highly skilled and motivated Cloud Engineer with a proven track record of designing, implementing, and managing robust cloud infrastructure solutions. With years of hands-on experience, I am deeply passionate about creating scalable and resilient cloud architectures that drive innovation and deliver optimal business outcomes. ๐Ÿ›  Key Competencies:

Cloud Platforms: AWS, Azure, GCP, OCI Infrastructure as Code: Terraform, Ansible Containers & Orchestration: Docker, Kubernetes Scripting: Python, Bash/Shell CI/CD & Version Control: GitHub, Jenkins, CircleCI Monitoring & Analytics: Grafana, Prometheus, Datadog, New Relic Backup & Recovery: Veeam Operating Systems: Linux, Windows DevOps Tools: AWS Code Build, Code Pipeline, Azure DevOps

๐Ÿ“š Continuous Learning: Staying ahead in the rapidly evolving cloud landscape is my priority. I am committed to expanding my skill set and embracing emerging cloud technologies to drive efficiency and innovation. Passionate Cloud/DevOps enthusiast dedicated to designing, building, and deploying cutting-edge technology solutions. As a devoted YouTuber, I love sharing insights through informative videos and crafting technical blogs that delve into areas like โ˜๏ธ Cloud, ๐Ÿ› ๏ธ DevOps, ๐Ÿง Linux, and ๐Ÿ“ฆ Containers. ๐Ÿ’ป Open Source Advocate: Contributing to open-source projects is a vital part of my journey. I actively engage in projects centered around Cloud, DevOps, Linux, and Containers, fostering collaboration and innovation within the community. ๐Ÿ’Œ Let's Connect: I am enthusiastic about virtual collaborations and meeting fellow professionals. Let's explore how I can contribute to your organization's cloud goals. Feel free to connect or DM me.

๐ŸŒ Portfolio: Check out my portfolio ๐Ÿ”— LinkedIn: Connect on LinkedIn ๐Ÿ› ๏ธ GitHub: Explore my projects ๐ŸŽฅ YouTube: Watch my videos ๐Ÿ“ Medium: Read my articles ๐ŸŒ Dev.to: Check out my posts

Overview

This project showcases a fully serverless video upload and playback application built using AWS services. The application enables users to upload videos, store them in Amazon S3, and manage metadata in DynamoDB. Videos can then be fetched and played seamlessly through a modern web interface. The architecture is scalable, secure, and cost-effective.

๐Ÿ‘‰ Follow Project on GitHub Repo Link: https://github.com/prafulpatel16/video-app-aws-serverless/blob/master/README.md ๐Ÿš€โœจ


Architecture Diagram


Key Features

  1. Scalable serverless architecture.

  2. Upload videos directly from the frontend to S3.

  3. Store video metadata in DynamoDB.

  4. Fetch and play videos via a modern web interface.


Tech Stack

  • Frontend: HTML, CSS, JavaScript

  • Backend: AWS Lambda, API Gateway

  • Database: DynamoDB

  • Storage: S3

  • Monitoring: CloudWatch


Step-by-Step Implementation

1. Setting Up Amazon S3 for Video Storage

  1. Create an S3 Bucket:

     aws s3 mb s3://video-upload-bucket
    

  1. Configure CORS:

     [
         {
             "AllowedHeaders": ["*"],
             "AllowedMethods": ["GET", "PUT", "POST"],
             "AllowedOrigins": ["*"]
         }
     ]
    
  2. Enable Static Website Hosting:

    • Go to the Properties tab in the S3 console.

    • Set Index Document to index.html.

  1. Sync Frontend Files:

     aws s3 sync static-web/ s3://video-upload-bucket
    

2. Setting Up DynamoDB for Metadata Storage

  1. Create a DynamoDB Table:

     aws dynamodb create-table \
         --table-name video-metadata \
         --attribute-definitions AttributeName=videoId,AttributeType=S \
         --key-schema AttributeName=videoId,KeyType=HASH \
         --billing-mode PAY_PER_REQUEST
    

3. Creating Lambda Functions

Upload Handler

This function uploads videos to S3 and saves metadata in DynamoDB.

import boto3
import json
import os
import uuid

s3 = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')

BUCKET_NAME = os.environ['BUCKET_NAME']
TABLE_NAME = os.environ['TABLE_NAME']

def lambda_handler(event, context):
    try:
        file_content = event['body']
        file_name = f"{uuid.uuid4()}.mp4"

        # Upload video to S3
        s3.put_object(Bucket=BUCKET_NAME, Key=file_name, Body=file_content)

        # Save metadata to DynamoDB
        table = dynamodb.Table(TABLE_NAME)
        table.put_item(
            Item={
                'videoId': file_name,
                'url': f"https://{BUCKET_NAME}.s3.amazonaws.com/{file_name}"
            }
        )

        return {
            "statusCode": 200,
            "headers": {"Access-Control-Allow-Origin": "*"},
            "body": json.dumps({"message": "File uploaded successfully"})
        }
    except Exception as e:
        return {
            "statusCode": 500,
            "headers": {"Access-Control-Allow-Origin": "*"},
            "body": json.dumps({"error": str(e)})
        }

Fetch Handler

This function retrieves video metadata from DynamoDB.

import boto3
import json
import os

dynamodb = boto3.resource('dynamodb')
TABLE_NAME = os.environ['TABLE_NAME']

def lambda_handler(event, context):
    try:
        table = dynamodb.Table(TABLE_NAME)
        response = table.scan()

        return {
            "statusCode": 200,
            "headers": {"Access-Control-Allow-Origin": "*"},
            "body": json.dumps(response['Items'])
        }
    except Exception as e:
        return {
            "statusCode": 500,
            "headers": {"Access-Control-Allow-Origin": "*"},
            "body": json.dumps({"error": str(e)})
        }

4. Configuring API Gateway

  1. Create an API:

    • Go to API Gateway > Create API.

    • Choose HTTP API and name it video-app-api.

  2. Add Routes:

    • POST /upload โ†’ video-upload-handler.

    • GET /fetch โ†’ video-fetch-handler.

  3. Enable CORS:

    • Add headers:

      • Access-Control-Allow-Origin: *

      • Access-Control-Allow-Methods: GET, POST, OPTIONS


5. Hosting Frontend on S3

  1. Sync the frontend files:

     aws s3 sync static-web/ s3://video-upload-bucket
    

6. Testing the Application

  1. Open the static website URL in your browser.

  2. Upload videos and fetch metadata to test the functionality.


Monitoring and Optimization

  1. Enable CloudWatch Logs for Lambda functions to monitor errors and performance.

  2. Use CloudFront to distribute video content globally for faster playback.


Challenges and Solutions

  1. CORS Issues:

    • Ensure proper headers are configured in API Gateway and Lambda responses.
  2. Large File Uploads:

    • Use multipart uploads for better performance with large files.

Conclusion

This AWS serverless project demonstrates the power of building scalable, cost-efficient, and secure video upload and playback systems. By leveraging AWS services like S3, DynamoDB, Lambda, and API Gateway, developers can deliver high-quality user experiences without managing server infrastructure. This architecture is ideal for real-time video applications.

More from this blog

C

cloud/devops

29 posts

PRAFUL PATEL โ˜๏ธ๐Ÿš€, Highly skilled and motivated Cloud/DevOps Engineer with a proven track record of designing, implementing, and managing robust cloud infrastructure solutions.