Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 70 ยท Storage with S3

Static Websites and S3 Events

Host a static website from S3, configure CORS, trigger Lambda, SQS or EventBridge on uploads, and replicate buckets across Regions, with runnable event and CORS emulators.

S3 does more than keep files. It can serve a website, answer cross-origin browser requests, tell other services the moment an object arrives, and copy data to another bucket or Region. These four features turn a bucket from a passive container into the front door and the trigger of many architectures: a marketing site, a user-upload pipeline, a disaster-recovery copy.

None of this can run on the site, so the configuration and commands below are read-only. The runnable examples are Python stand-ins: an event dispatcher that matches notification rules the way S3 does, and a CORS matcher that answers browser preflight requests. They are simplified models, built to make the rules visible.

Static website hosting

A bucket can act as a web server for static files: HTML, CSS, JavaScript and images. You enable static website hosting, name an index document (served for the root and for paths ending in /) and optionally an error document. S3 then offers a website endpoint such as acme-site.s3-website-us-east-1.amazonaws.com (some Regions write it with a dot, as s3-website.eu-central-1).

Terminal
aws s3 website s3://acme-site/ --index-document index.html --error-document 404.html
aws s3 sync ./dist s3://acme-site/

The website endpoint has two important limits. It speaks HTTP only, with no HTTPS and no custom domain certificate. And it needs the content to be publicly readable, which means turning off the Block Public Access settings you learned to keep on in S3 Security.

The better production design keeps the bucket private and puts CloudFront in front of it, with Origin Access Control so only the distribution can read the bucket. You get HTTPS with an ACM certificate, a global cache and your own domain through Route 53. The trade-off is that CloudFront talks to the REST endpoint, which has no automatic "index.html for subfolders" behavior; you add a small CloudFront Function that rewrites /docs/ to /docs/index.html, as CloudFront CDN shows. The complete build is the project Project: Static Site on S3 and CloudFront.

flowchart LR
    U["Browser"] -->|"HTTPS"| CF["CloudFront<br/>cache + certificate"]
    CF -->|"Origin Access Control"| B["Private S3 bucket<br/>index.html, assets"]
    U -.->|"HTTP only, public bucket"| W["S3 website endpoint"]
    W -.-> B

The solid path is the recommended design; the dotted path is the simple but limited website endpoint.

CORS

Browsers block a web page from reading responses of another origin unless that origin opts in with CORS (cross-origin resource sharing) headers. If a site on https://app.acme.example fetches images or JSON straight from an S3 bucket, or uploads with a presigned URL from JavaScript, the bucket needs a CORS configuration:

JSON
[
  {
    "AllowedOrigins": ["https://app.acme.example"],
    "AllowedMethods": ["GET", "PUT"],
    "AllowedHeaders": ["content-type", "x-amz-*"],
    "ExposeHeaders": ["ETag"],
    "MaxAgeSeconds": 3000
  }
]

For anything beyond a simple GET, the browser first sends a preflight OPTIONS request with Origin, Access-Control-Request-Method and Access-Control-Request-Headers. S3 checks the rules in order and the first rule that matches the origin, method and every requested header wins. If none matches, S3 answers 403 and the browser refuses the real request. CORS is a browser protection only; curl and servers ignore it, and it is not authorization. A frequent mistake in upload flows is forgetting to expose ETag, which a script needs to complete a multipart upload.

Python A CORS preflight matcher

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second case fails because the first rule matched the origin and method but not the x-custom header, and the second rule does not match the method either. Real S3 behaves the same way: a rule must satisfy all three conditions at once.

Event notifications

S3 can emit an event whenever something happens in a bucket, so work starts without any polling. Typical event types are s3:ObjectCreated:* (put, post, copy, completed multipart upload), s3:ObjectRemoved:*, lifecycle expiration and transition events, restore completion and replication events. You can narrow a rule with a key prefix and suffix, for example only uploads/ and .jpg.

Destinations:

  • AWS Lambda: run a function for each event. The function needs a resource-based permission that lets s3.amazonaws.com invoke it (Lambda Functions).
  • Amazon SQS: queue the events for a worker to process at its own pace (SQS, SNS and Fan-Out). The queue policy must allow S3 to send messages.
  • Amazon SNS: fan out to many subscribers.
  • Amazon EventBridge: turn it on once per bucket and every event goes to the default event bus, where rules can filter on far more fields (object size, tags, source IP) and route to many targets, with archive and replay (EventBridge and Step Functions).

Remember three properties. Delivery is usually within seconds but is at least once, so a consumer must tolerate duplicates. Ordering is not guaranteed across keys. And the object key arrives URL-encoded in the event body, so a space shows up as +; decode it before use. Another classic trap is a Lambda function that writes into the same prefix it listens to, which triggers itself in an endless loop and a surprise bill. Use separate prefixes or buckets for input and output.

Sending S3 events to SNS, and checking that it works

To email yourself when files arrive, the path is: S3 event, SNS topic, email subscription. The steps matter because each one has its own permission.

  1. Create an SNS topic in the same Region as the bucket and add a topic access policy that lets s3.amazonaws.com publish, limited to your bucket with an aws:SourceArn condition and to your account with aws:SourceAccount.
  2. Subscribe an email address and confirm the subscription from the message AWS sends. An unconfirmed subscription receives nothing.
  3. On the bucket, add an event notification for the event types you want (optionally with a prefix or suffix filter such as uploads/ or .jpg) with the topic as destination. S3 sends a test event when you save, and rejects the configuration if the topic policy is wrong.
  4. Upload a matching file and check the inbox.

If nothing arrives, check in this order: the subscription is confirmed, the Region of topic and bucket is the same, the filter really matches the key (a .JPG upload does not match a .jpg suffix), and the message is not in the spam folder. Notifications for objects that do not match your filters are simply never sent, by design.

Direct notifications have a rule: configurations for the same event type cannot have overlapping prefix and suffix filters, because S3 could not decide which destination should receive an event. S3 rejects such a configuration when you save it.

Python Dispatching S3 events to destinations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The text file and the object under exports/ reach no destination, because no rule matches them. With EventBridge enabled, they would still appear on the event bus, where you could route them with a pattern.

Replication

Replication copies new objects automatically and asynchronously from a source bucket to a destination bucket. Cross-Region Replication (CRR) places the copy in another Region for lower latency for distant users, compliance or disaster recovery. Same-Region Replication (SRR) copies within a Region, for example to aggregate logs or keep a copy in a separate account. Key facts:

  • Versioning must be enabled on both buckets, and S3 needs an IAM role to read the source and write the destination.
  • A rule applies only to objects written after it is created. S3 Batch Replication copies existing objects.
  • Replication does not chain: a replica is not replicated again unless it is set up that way separately.
  • Delete markers can be replicated optionally; permanent deletes of specific versions are not, which protects the replica from malicious deletions.
  • Metadata, tags and encryption settings can be carried over, and SSE-KMS objects need extra KMS permissions on both sides. The destination can use a different storage class, such as Glacier.
  • Replication Time Control adds a 15-minute service level objective for most objects and metrics to monitor lag.

Replication is not a backup against bad writes, since overwrites replicate as well. Combine it with versioning and Object Lock where recovery matters, and see High Availability and Disaster Recovery for how it fits a recovery plan.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a limitation of the S3 website endpoint?

    The website endpoint does not support HTTPS. Putting CloudFront in front gives you TLS, caching and a custom domain, with the bucket kept private.
  2. 2. A browser's preflight request to your bucket fails with 403. What is the most likely cause?

    S3 answers a preflight with 403 when no CORS rule matches the origin, the requested method and all requested headers.
  3. 3. Your Lambda function triggered by uploads writes its result to the same prefix. What can happen?

    Each result creates a new object and a new event. Use different prefixes or buckets for input and output.
  4. 4. Which is required to enable replication between two buckets?

    Replication needs versioning on source and destination plus an IAM role S3 assumes to copy the objects.

Summary

  • Website hosting serves static files over HTTP only; production sites keep the bucket private behind CloudFront.
  • CORS rules are checked in order; the first rule matching origin, method and headers answers the preflight.
  • Event notifications go to Lambda, SQS, SNS or EventBridge, are delivered at least once and use URL-encoded keys.
  • Avoid overlapping filters and trigger loops.
  • Replication needs versioning, applies to new objects and is not a substitute for backups.

Next: RDS and Aurora.