S3 & Backend Integration

Amazon S3 (Simple Storage Service) is a highly scalable object storage service. InfraHub uses S3 to store all static assets including HTML templates, CSS files, images, and downloadable content. This allows the backend to remain lightweight, stateless, and easy to update.


Benefits of Using S3

  • Scalability: S3 automatically scales with your storage needs, no matter how many files or how much traffic your site receives.
  • High Availability & Durability: Data is redundantly stored across multiple availability zones, ensuring reliability.
  • Cost Efficiency: S3 charges primarily for storage and requests, making it economical for static content and infrequently updated files.
  • Security: Supports encryption at rest and in transit, IAM policies for fine-grained access, and integration with AWS KMS.
  • Performance: Files are served efficiently to CDNs and backend applications without needing persistent local storage.

Backend Integration with Flask & Boto3

InfraHub uses a Flask application to dynamically fetch and serve S3 content. The backend is modular, lightweight, and containerized with Docker.

Examples:


import boto3
from flask import Response

s3 = boto3.client('s3')
BUCKET_NAME = 'capstone-ajaimes'

# Fetch HTML template dynamically
def get_s3_text_file(key):
    obj = s3.get_object(Bucket=BUCKET_NAME, Key=key)
    return obj['Body'].read().decode('utf-8')

# Serve a CSS file
def serve_css(filename):
    css_key = f'frontend/static/{filename}'
    css_content = get_s3_text_file(css_key)
    return Response(css_content, mimetype='text/css')

# Serve PNG images
def serve_png(filename):
    s3_key = f'frontend/images/{filename}'
    obj = s3.get_object(Bucket=BUCKET_NAME, Key=s3_key)
    return Response(obj['Body'].read(), mimetype='image/png')
        

This approach keeps containers stateless and allows updates to templates, styles, or assets without rebuilding the Docker image. S3 works seamlessly with the CDNs to deliver content reliably and at low latency.


Additional Features

  • Secure Downloads: Files served from S3 can be streamed directly to the client with proper headers to force downloads.
  • Dynamic Template Serving: HTML and CSS are loaded from S3 using Flask’s render_template_string, allowing content updates without redeploying the container.
  • Stateless Backend: Containers remain lightweight, with no local asset storage, simplifying CI/CD and deployment processes.
  • Encryption: S3 supports server-side encryption (SSE-S3, SSE-KMS) ensuring that static content is stored securely.
  • Cost Awareness: Using S3 reduces EC2 storage costs and limits outbound bandwidth charges when combined with CloudFront and Fastly.