# non-97/cloudfront-s3-website architecture

> Architecture diagram of the GitHub repository non-97/cloudfront-s3-website, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/non-97/cloudfront-s3-website
- Repository: https://github.com/non-97/cloudfront-s3-website

## Overview

##### Project Type and Purpose
- This is an “Infrastructure-as-Code” project using AWS CDK in TypeScript.
- It defines a single CloudFormation stack (“WebsiteStack”) that:
- Provisions an S3 bucket for static assets
- Configures a CloudFront distribution with Lambda@Edge and CloudFront Functions (CF2)
- Attaches an ACM TLS certificate and a Route 53 hosted zone
- Applies a Web Application Firewall (WAF)
- Collects and processes access logs

##### File Structure
- Top-level:
- cdk.json, tsconfig.json, package.json – CDK app config
- bin/website.ts – CDK app entry
- lib/website-stack.ts – Stack definition
- lib/construct/*.ts – Reusable constructs: Bucket, Certificate, ContentDelivery, HostedZone, LogAnalytics, WAF
- lib/src/cf2/* – JavaScript code for CloudFront Functions (directory‐index, rewrite‐to‐webp, normalize keys)
- lib/src/lambda/* – TypeScript code for Lambda@Edge (same features) and a log‐mover function
- parameter/index.ts – Parameter definitions
- test/website.test.ts – Jest unit test

##### README
- Confirms it’s a blank CDK TypeScript project.
- Provides common CDK commands (build, deploy, synth, test).

##### Main Components & Their Interactions
a. Users (HTTPS clients)
b. Amazon Route 53 – DNS
c. AWS Certificate Manager (ACM) – TLS certificate
d. Amazon CloudFront Distribution
- Origin: Amazon S3 Static Website Bucket
- Edge Logic:
– CloudFront Functions (CF2) for directory indexing, WebP rewrites, cache-key normalization
– Lambda@Edge versions of the same logic for Lambda runtime support
- WAF WebACL attached
- Access Logging → S3 Logs Bucket
e. AWS Lambda (non-edge) “log mover” – processes/moves CloudFront access logs to analytics storage or pipeline
f. Optional: Log Analytics construct (could be CloudWatch Logs, Athena, etc., per construct implementation)

##### Architectural Patterns & Design Principles
- Modular CDK constructs—each AWS resource group is abstracted
- Edge-optimized delivery using CloudFront Functions and Lambda@Edge (low latency)
- Infrastructure as Code with a single source of truth
- Separation of static content, dynamic edge logic, and logging pipelines
- Parameterization for reusability across environments

## Diagram (Mermaid)

```mermaid
flowchart TB
    Users["Users\n(HTTPS Clients)"]:::external
    CDKApp["CDK App Entry"]:::internal
    click CDKApp "https://github.com/non-97/cloudfront-s3-website/blob/main/bin/website.ts"
    subgraph "WebsiteStack"
        click WebsiteStack "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/website-stack.ts"
        subgraph "Infrastructure"
            DNS["Route 53\n(DNS)"]:::network
            click DNS "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/construct/hosted-zone-construct.ts"
            Cert["ACM Certificate"]:::security
            click Cert "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/construct/certificate-construct.ts"
            WAF["WAF WebACL"]:::network
            click WAF "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/construct/waf-construct.ts"
        end
        subgraph "Content & Delivery"
            Bucket["S3 Static Assets Bucket"]:::storage
            click Bucket "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/construct/bucket-construct.ts"
            CF["CloudFront Distribution"]:::network
            click CF "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/construct/contents-delivery-construct.ts"
            subgraph "Edge Logic"
                CF2a["CF Function:\nDirectory Index"]:::compute
                click CF2a "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/src/cf2/directory-index/index.js"
                CF2b["CF Function:\nRewrite to WebP"]:::compute
                click CF2b "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/src/cf2/rewrite-to-webp/index.js"
                CF2c["CF Function:\nNormalize WebP Key"]:::compute
                click CF2c "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/src/cf2/normalize-webp-cache-key/index.js"
                LambdaEdgeA["Lambda@Edge:\nDirectory Index"]:::compute
                click LambdaEdgeA "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/src/lambda/directory-index/index.ts"
                LambdaEdgeB["Lambda@Edge:\nRewrite to WebP"]:::compute
                click LambdaEdgeB "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/src/lambda/rewrite-to-webp/index.ts"
            end
        end
        subgraph "Logging Pipeline"
            LogBucket["S3 Logs Bucket"]:::storage
            LogMover["Lambda: Log Mover"]:::compute
            click LogMover "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/src/lambda/move-cloudfront-access-log/index.ts"
            LogAnalytics["LogAnalytics Construct"]:::logging
            click LogAnalytics "https://github.com/non-97/cloudfront-s3-website/blob/main/lib/construct/log-analytics-construct.ts"
        end
    end

    Users -->|"HTTPS GET\n(full domain)"| DNS
    DNS -->|"DNS Validation"| Cert
    Cert --> CF
    DNS --> CF
    CF -->|"Viewer Request"| CF2a
    CF -->|"Viewer Request"| LambdaEdgeA
    CF -->|"Origin Request"| Bucket
    Bucket -->|"GET Objects"| CF
    CF -->|"Viewer Response"| Users
    WAF --> CF
    CF -->|"Access Logs"| LogBucket
    LogBucket -->|"PUT Event"| LogMover
    LogMover -->|"Analytics Data"| LogAnalytics
    CDKApp --> WebsiteStack

    classDef storage fill:#ffebcc,stroke:#e98c00
    classDef compute fill:#e0f7fa,stroke:#0288d1
    classDef network fill:#e8f5e9,stroke:#43a047
    classDef security fill:#ffebee,stroke:#e53935
    classDef logging fill:#f3e5f5,stroke:#8e24aa
    classDef external fill:#c8e6c9,stroke:#2e7d32
    classDef internal fill:#fff9c4,stroke:#f9a825
```
