Monday, June 30, 2025

CloudFront with ALB or EC2 As VPC Origin | Overview.

CloudFront with ALB or EC2 As  VPC Origin - Overview.

Scope:

  • Intro,
  • Basics: CloudFront Origins,
  • But CloudFront is a public service, so,
  • Recommended setup,
  • Benefits,
  • Security tip,
  • Use Case,
  • Summary Table,
  • Best Practices.

Intro:

  •  twteh Uses Amazon CloudFront with ALB or EC2 as an origin, Especially with VPC-only resources,
  • The architecture requires some understanding of how CloudFront handles private resources (Subnet) and what architecture makes that possible.

 Basics: CloudFront Origins

CloudFront supports several types of origins, such as:

  •         S3 buckets
  •         Application Load Balancers (ALB)
  •         EC2 instances
  •         API Gateway, MediaPackage, etc.

But CloudFront is a public service, so:

  •         It cannot directly access private resources inside a VPC.
  •         twtech must make the resource publicly accessible or use AWS PrivateLink / Lambda@Edge / CloudFront Functions / ALB integration.

 Use Case 1: CloudFront + ALB Origin (with EC2 in private subnets)

Recommended setup:

     1.     ALB is public-facing (with an internet-facing scheme).
2.     ALB forwards traffic to EC2 instances in private subnets.
3.     CloudFront points to the public DNS name of the ALB.

Benefits:

  •         EC2 remains in private subnet (not directly exposed).
  •         CloudFront provides global caching, DDoS protection, and SSL termination.
  •         ALB handles dynamic content, session stickiness, health checks.

 Security tip:

Use CloudFront origin access control:

  •         Configure CloudFront with a custom header (e.g., X-Origin-Token) that only CloudFront sends.
  •         At ALB/EC2 level, verify this header to reject non-CloudFront traffic.

 Use Case 2: CloudFront + EC2 as Origin (No ALB)

Options:

  •         If EC2 instance has a public IP, CloudFront can directly access it.
  •         But it’s not recommended for scalability and fault tolerance.
  •         Better to put EC2 behind an ALB or API Gateway.

 Use Case 3: CloudFront + ALB/EC2 in Private VPC (VPC-only, no public IP)

  • This is trickier, but possible via AWS networking integrations.

 Problem:

  • CloudFront is external to twtech VPC and cannot access private-only endpoints.

 Solutions:

1. Use AWS Global Accelerator + ALB (Internal):

  •         Global Accelerator allows traffic from CloudFront to twtech internal ALB.
  •         Complex and usually overkill unless twtech needs private CloudFront origins.

2. Use CloudFront → Lambda@Edge → VPC endpoint (e.g., API Gateway Private Endpoint):

  •         twtech can build a flow like:
    •    CloudFront Lambda@Edge API Gateway private endpoint (which invokes VPC resource).
  •         Allows reaching private VPC resources securely.
  •         Adds latency and complexity.

3. Use AWS CloudFront Functions + S3/ALB Public Entry Point + VPC Backend

  •         CloudFront serves static content and invokes ALB that routes to private EC2.
  •         twtech limits public access to ALB via security group and CloudFront headers.

 Summary Table

Origin Type

VPC Access

CloudFront Support

Recommended

Public ALB

Yes

Native

Best practice

Private ALB

Yes

Not directly

🚫 Complex setup

Public EC2

No

Possible

🚫 Not scalable

Private EC2

Yes

Not directly

Not possible without proxy

S3

Yes (via endpoint)

(OAC preferred)

Recommended for static

Best Practices

  •         Use ALB over direct EC2 origin.
  •         Keep backend EC2 instances private, behind ALB.
  •         Secure your origin using custom headers or signed URLs.
  •         Use CloudFront Origin Access Control (OAC) if using S3.

Sunday, June 29, 2025

CloudFront Vs S3 Cross Region Replication | Overview


CloudFront Vs S3 Cross Region Replication - Overview.

Scope:

  • Intro, 
  • Comparison table between CloudFront & S3 Cross-Region Overview,
  • Key Differences,
  • When to Use CloudFront,
  • When to Use S3 CRR,
  • Does twtech Use  Both approaches Together.

Intro:

  • Here's twtech comparison between CloudFront and S3 Cross-Region Replication (CRR), highlighting their purpose, use cases, and differences.

 Comparison table between CloudFront & S3 Cross-Region Overview.

Feature

Amazon CloudFront

S3 Cross-Region Replication (CRR)

Type

Content Delivery Network (CDN)

S3 Feature

Primary Goal

Reduce latency for content delivery

Replicate objects across AWS regions for redundancy

Data Location

Edge locations near users

Destination S3 bucket in a different AWS region

Trigger Mechanism

On-demand caching (user request)

Automatic replication upon object creation

 Key Differences

Aspect

CloudFront

S3 CRR

Latency Improvement

Yes — delivers cached content from nearest edge location

No — not intended for performance enhancement

Redundancy & DR

No — caches, not durable storage

Yes — used for backup and disaster recovery

Data Persistence

Cached temporarily

Fully stored in destination region

Cost Model

Pay for data transfer and requests to edge locations

Pay for replication bandwidth + storage in destination region

Security

Integrated with WAF, SSL, signed URLs

Uses IAM, bucket policies, and encryption (SSE, KMS, etc.)

Version Replication

Not applicable

Can replicate new versions if versioning is enabled

Content Types

Ideal for static and dynamic web content

Works with any S3 object

Use with Public Web

Optimized for web content delivery

Not intended for direct access from users

 When to Use CloudFront

  • twtech wants faster content delivery to users globally.
  • twtech serves static/dynamic web assets (images, videos, scripts).
  • twtech wants to cache frequently accessed content close to users.
  • twtech needs DDoS protection and geo-restriction.

  When to Use S3 CRR 

  • twtech needs cross-region backup or data residency compliance.
  • twtech requires high availability and disaster recovery.
  • twtech needs replication of S3 data for processing in another region.
  • twtech wants automatic object replication, not caching.

 Does twtech Use  Both approaches Together

Yes.

 Many real-world setups combine both:

  • S3 CRR replicates data between regions for durability.
  • CloudFront delivers that data to users globally with low latency.

CloudFront Origins | Overview.

CloudFront Origins - Overview.

Scope:

  • Intro,
  • Types of Origins,
  • Key Origin Settings,
  • Sample Use Case,
  • Multiple Origins & Behaviors,
  • Sample Use Case.

Intro:

  • In Amazon CloudFront, an origin is the source location where CloudFront fetches twtech content from when it's not already cached at an edge location.
  • twtech can configure one or more origins for a CloudFront distribution.

 Types of Origins

CloudFront supports multiple types of origins:

  1. Amazon S3 bucket
    • Used to serve static content (e.g., images, CSS, JS, HTML).
    • Can be configured as:
      • Public bucket
      • With Origin Access Control (OAC) for private access
  2. HTTP/HTTPS Server (Custom origin)
    • For dynamic content from:
      • EC2 instances
      • Load balancers (ALB, NLB)
      • On-premises servers
    • CloudFront connects to the origin over HTTP or HTTPS.
  3. AWS Media Services
    • Used for video streaming (e.g., AWS MediaPackage, MediaStore).
  4. Other AWS Services
    • API Gateway
    • AWS Elemental MediaPackage
    • Lambda (indirectly through API Gateway)

 Key Origin Settings

When twtech defines an origin, it specify:

  • Origin domain name: URL of the origin (e.g., example.s3.amazonaws.com)
  • Origin path (optional): Appended to the origin domain for requests
  • Origin ID: A unique identifier within your distribution
  • Protocol policy: HTTP only, HTTPS only, or match viewer
  • Custom headers (optional): Sent with origin requests

 Multiple Origins & Behaviors

  • twtech can associate different cache behaviors with different origins.
  • Sample:
    • /static/* S3 bucket
    • /api/* → ALB or API Gateway

 Sample Use Case

Path Pattern

Origin Type

Description

/images/*

S3 bucket

Static images

/app/*

ALB (EC2 backend)

Dynamic web app content

/api/*

API Gateway

REST API endpoint



CloudFront vs. Global Accelerator | Overview & Hands-On.

Here is twtech comparative Overview of Amazon CloudFront and AWS Global Accelerator. 

Scope:

  • Intro,
  • The concept: Amazon CloudFront,
  • The concept: AWS Global Accelerator,
  • Key Features,
  • Use Cases,
  • CloudFront vs. Global Accelerator,
  • When to Use Each,
  • Does twtech Needs both,
  • Project: Hands-on

Intro:

 The concept: Amazon CloudFront

A Content Delivery Network (CDN) that caches and distributes static and dynamic web content (like HTML, CSS, JS, images, video) to users around the globe.

Key Features:

  • Caching at edge locations to reduce latency.
  • Supports HTTPS, custom domains, and origin failover.
  • Works closely with S3, EC2, Elastic Load Balancers, and Lambda@Edge.
  • Can serve dynamic content, not just static.

Use Cases:

  • Accelerating websites and APIs.
  • Serving static content from S3.
  • Streaming video.
  • Securing content delivery with signed URLs or headers.

 The concept: AWS Global Accelerator

A network layer accelerator that uses AWS’s global network to improve the availability and performance of twtech global applications (does not cache content).

Key Features:

  • Assigns static anycast IP addresses to twtech app.
  • Automatically routes traffic through the optimal AWS edge location to the nearest healthy regional endpoint.
  • Improves performance (especially for TCP/UDP apps) and resiliency.

Use Cases:

  • Improving latency and availability of non-HTTP applications (e.g., gaming, VoIP, financial apps).
  • Routing traffic to nearest application endpoint in multi-region deployments.
  • Replacing Route 53 + Load Balancer setups for global apps.

 CloudFront vs. Global Accelerator

Feature

CloudFront

Global Accelerator

Type

CDN

Network traffic accelerator

Content Caching

✅ Yes

❌ No

Optimized for

HTTP/HTTPS traffic

TCP/UDP traffic

Latency Improvement

✅ Yes (via caching)

✅ Yes (via network optimization)

Static IP Addresses

❌ No

✅ Yes (Anycast IPs)

Ideal for

Websites, APIs, video, static content

Multiplayer games, VoIP, real-time apps

 When to Use Each:

  • ✅ Use CloudFront if  serving web content, APIs, or static files and want to reduce latency with caching.
  • ✅ Use Global Accelerator if running a global application that requires low-latency access to regional endpoints (especially for non-HTTP traffic or needing static IPs).

Does twtech Needs both

Yes:

 In some advanced architectures, twtech may use Global Accelerator to route users to the nearest CloudFront distribution or Application Load Balancer for maximum performance and availability.


Project: Hands-on

  • How twtech creates s3 bucket to hold files for distribution via:  CloudFront distribution.
  • Create a bucket: twtech-cloudfront-s3





  • Slelect and Upload some objects (files) into the bucket: twtech-cloudfront-s3

  • Objects don’t have permission to be seen publicly: Access Denied

  • twtech used CloudFront to make objects in private bucket accessible without making  accessible in the public.
  • Search aws services: CloudFront 


NB:

  • CloudFront is a global service.
  • Create a CloudFront distribution: twtech-cloudfront-distribution

  • Assign a name: twtech-cloudfront-distribution

  • Select Custom domain (optional)  or insert a custom domain and  check domain
Domain: twtechapp.com
  • Use twtech custom domain with free HTTPS to provide a secure, friendly URL for twtech app. 
  • twtech can add a custom domain later if twtech does not have a Route 53 zone in this account.



  • Specify origin

Origin

  • S3 origin
  • Choose an AWS origin, or enter your origin's domain name

From:


To: 
  • select bucket location

  • S3 origin from cloudfront: 
      twtech-cloudfront-s3.s3.us-east-2.amazonaws.com

  • Proceed with configuration of cloudfront distribution: twtech-cloudfront-distributiion


  • CloudFront can automatically create a certificate for the following domains: enabled

  *.twtech.click

  • Create a wildcard certificate This certificate will be valid for *.twtech.clcik
  • Create a wildcat certificate for domaintwtech.clcik
  • If custom domain names have been registered, select one: twtech.click

  • View certificate created for domain: twtechapp.com

  • TLS certificate is issued by us-east-1 region: N. Virginia


  • Review and create CloudFront distribution: twtech-cloudfront-distribution


  • It takes a couple of minutes to be fully deployed:


  • twtech updates the s3 bucket policy (twtech-cloudfront-s3):  so that CloudFront distriburion can access the bucket.
  • Select the bucket and click open: twtech-cloudfront-s3

  • In the bucket (twtech-cloudfront-s3), navigate to permissions tab.


  • Scroll down to Bucket policy to: edit if policy is not auto generated.

From:



To:

  • Delete the entire script and use that policy generated by aws cloudfront: origin

Edit: origin


  • Copy the policy genetated by aws and then cancel the page:


# twtech-sample-cloudfront-s3-policy

#  json

{

        "Version": "2008-10-17",

        "Id": "PolicyForCloudFrontPrivateContent",

        "Statement": [

            {

                "Sid": "twtechAllowCloudFrontServicePrincipal",

                "Effect": "Allow",

                "Principal": {

                    "Service": "cloudfront.amazonaws.com"

                },

                "Action": "s3:GetObject",

                "Resource": "arn:aws:s3:::twtech-cloudfront-s3/*",

                "Condition": {

                    "StringEquals": {

                      "AWS:SourceArn": "arn:aws:cloudfront::accountID:distribution/E3H6YAxxxxx"

                    }

                }

            }

        ]

      }

twtech accesses the bucket policy generated by aws cloudfront under: orgin 

  • Navigate to bucket permission tab to edit the bucket policy: so that cloudfront can have access to its objects.

  • Scroll down and edit Bucket policy:

From:

To: 

  • Copy and paste the policy genetated by aws under: cloudfront orgin

  • Paste policy and Save changes:



  • twtech accesses objects globally in the bucket from cloudfron distribution: twtech-cloudfront-distribution

  • twtech accesses the object be adding the path of the object to the Distribution domain name:
https://dnhouzyyxxxx.cloudfront.net/papa.jpg


https://dnhouzyyxxxx.cloudfront.net/pat-atem.jpg

https://dnhouzyyxxxx.cloudfront.net/Patpaddy3.jpg

NB:

  • twtech-Objects are secured with the TLS ceritificate issued by aws CMA(certificate management authority) for security (governance and compliance)
  • Connection is secured with a TLS certificated attached to a domain name:  it is issued by aws to encrypt traffic.


NB:

  • twtech may choose to create a distribution in the test environment without the TLS cerficate.
  • However, aws redirects traffic to https: secured connection

 From:


To: 

  • How twtech uses the distribution domain name with the object context path to access the ojects in bucket.

NB: 

AWS redirects traffic to https: secured connection

https://d1c1iqjz6xxx.cloudfront.net/twtech-object-name

https://d1c1iqjz6sxxx.cloudfront.net/papa.jpg


https://d1c1iqjz6sxxxx.cloudfront.net/pat-atem.jpg


https://d1c1iqjz6sxxx.cloudfront.net/Patpaddy3.jpg


Databases Explained & Use Cases with (Flash Card) | Overview.

Databases Explained  & Use Cases ( Flash Cards)   - Overview. A database is a structured collection of digital information designed f...