S3 and CloudFront

Overview

This section describes the configuration steps for Amazon S3, Origin Access Control (OAC), and CloudFront to serve the static frontend of the system.

Deployment Steps

  1. Open Amazon S3 and create a new bucket for the LunchSync frontend.

S3 step 1

  1. Set the bucket prefix to lunchsync-project, choose region ap-southeast-1, allow S3 to generate the full name, and keep the bucket private.

S3 step 2

  1. Keep ACLs disabled, enable Block all public access, do not enable versioning, and continue with a private bucket.

S3 step 3

  1. Use default encryption SSE-S3, create the bucket, then upload frontend content into folders such as frontend/ and media/.

S3 step 4

  1. After attaching the bucket to CloudFront, check the bucket policy to ensure only cloudfront.amazonaws.com from your distribution can read objects.

S3 step 5

  1. Go to CloudFront > Origin access and create a new Origin Access Control (OAC) for the S3 bucket.

OAC step 1

  1. Name the OAC lunchsync-oac, add a clear description, and keep Sign requests enabled.

OAC step 2

  1. Verify that lunchsync-oac has been created successfully before proceeding.

OAC step 3

  1. Go to CloudFront > Distributions and start creating a new distribution.

CloudFront step 1

  1. Choose the distribution setup plan and continue the wizard.

CloudFront step 2

  1. Name the distribution S3-Frontend, select Single website or app, and skip Route 53 domain setup in the initial wizard.

CloudFront step 3

  1. In Specify origin, choose Amazon S3 as the origin type.

CloudFront step 4

  1. Select the project S3 bucket as origin, set Origin path = /frontend, and allow CloudFront to access the private bucket.

CloudFront step 5

  1. Keep AWS recommended origin and cache settings for static S3 content.

CloudFront step 6

  1. In Enable security, keep default CloudFront/WAF protections.

CloudFront step 7

  1. Review configuration: S3 origin, Origin path = /frontend, then create the distribution.

CloudFront step 8

  1. After creation, open Origins tab to verify correct origin path and OAC.

CloudFront step 9

  1. If needed, edit origin to attach lunchsync-oac and copy the generated policy into the S3 bucket policy.

CloudFront step 10

  1. Open Behaviors tab to configure default behavior.

CloudFront step 11

  1. Set Viewer protocol policy = Redirect HTTP to HTTPS.

CloudFront step 12

  1. Keep cache policy CachingOptimized and origin request policy CORS-S3Origin.

CloudFront step 13

  1. Open Error pages and configure custom error response for SPA.

CloudFront step 14

  1. Create a rule: when origin returns 403, respond with /index.html and status 200.

CloudFront step 15

  1. Verify SPA fallback rule is saved.

CloudFront step 16

  1. Go back to Origins and create a second origin for the backend ALB.

CloudFront step 17

  1. Enter ALB DNS name, use HTTPS only for origin communication.

CloudFront step 18

  1. Create a third origin for media using the same S3 bucket with Origin path = /media and attach OAC.

CloudFront step 19

  1. Verify there are now 3 origins: frontend, ALB backend, and media.

CloudFront step 20

  1. Create behavior for media/*, route to media origin, enforce HTTPS.

CloudFront step 21

  1. Create behavior for api/*, route to ALB, set HTTPS only, disable caching, use AllViewer policy.

  2. Verify final behavior list.

CloudFront step 23

  1. Go to Route 53, open hosted zone lunchsync.space.

CloudFront step 26

  1. Create an alias record at root domain pointing to CloudFront distribution.

CloudFront step 27

  1. Verify the alias record appears.

CloudFront step 28

  1. Return to CloudFront settings, add Alternate domain (CNAME) lunchsync.space and select ACM certificate in us-east-1.

CloudFront step 29 CloudFront step 30

  1. Set Default root object = index.html and enable HTTP versions (HTTP/2, HTTP/3).

CloudFront step 31

  1. Save settings and verify domain, certificate, and root object.

CloudFront step 32

  1. Open https://lunchsync.space to verify frontend is served via CloudFront.

CloudFront step 33

  1. Test a media asset like https://lunchsync.space/media/lunchsync.png to confirm media/* behavior works correctly.

CloudFront step 34