Case studies

CDN

A media CDN for Magento: private S3 and CloudFront without moving the shop

A Magento shop with several domains kept product images, manuals and editor files on the application disk. The disk grew to hundreds of gigabytes and blocked scaling. We moved pub/media to a private S3 bucket and served media through CloudFront. The shop itself stayed on its existing server.

Illustration: online shop infrastructure and media delivery

1 host

Media host for every storefront

0

Public paths straight into the bucket

~30 dni

Media cache at the CloudFront edge

From this case study you will learn

  • which Magento directories move to the CDN and which stay on the server
  • how to set up a private S3 bucket with CloudFront and Origin Access Control
  • in what order to switch Magento so new files stop landing on the disk
  • what you pay for in such a rollout and what is free

The shop ran on Magento 2 and served several storefronts: B2C, B2B and country stores on separate domains. Product images, manuals and content editor files lived on the application disk, next to the PHP code and the database.

The starting point

The media directory grew with every import and took up hundreds of gigabytes. The application disk became a bottleneck: every image left the same server as PHP, and public files were mixed with private ones. The client planned to run several application instances, and a shared local media disk made that impossible.

The goal was not to move the whole shop to the cloud. It was to stop media from loading the application server.

What moved and what stayed

Phase one covered pub/media only. A single media domain, for example assets.sklep.pl, serves every storefront. Images in the HTML use an absolute CDN URL, while the shop keeps running on its own domains.

Moved to the CDN:

  • media/catalog/product, product images and thumbnails,
  • media/wysiwyg, content editor files,
  • manuals and other public assets from the media directory.

Stayed on the server:

  • HTML, because it depends on the domain and store view,
  • pub/static, because CSS and JS rely on versioning and nginx rules, a separate project,
  • the admin panel,
  • feeds, customer files and digital products, which must not be public.

Architecture

Diagram: HTML from the Magento server, media from private S3 through CloudFront, private prefixes return 403
HTML stays on the shop server, media comes from private S3 through CloudFront. Illustrative diagram.

HTML still comes from the shop server. Media goes through CloudFront from a private S3 bucket. The local disk is now used only for migration and a possible rollback.

Key decisions:

  • Private S3 bucket. Public access is blocked.
  • CloudFront with Origin Access Control. The bucket serves files to this one distribution only.
  • ACM certificate in us-east-1. CloudFront requires a certificate from that region.
  • DNS record without a proxy. A proxy in front of CloudFront would double the transfer and lengthen the path.
  • Private prefixes return 403. Customer files, downloadable products and imports are not reachable by a known URL.
  • Empty prefix in Remote Storage. Magento adds media/ to the object key itself. A media prefix would produce media/media/ paths.

If Cloudflare was already caching media, the speed gain is moderate. The origin of the file still changes: it is no longer the application disk.

Data migration

We copied files from production to S3 with sync without --delete, so nothing disappeared locally. The script skipped private directories the CDN would not serve anyway. After the first full copy, just before switching Magento, we ran a delta sync.

Three things need attention during migration:

  • Non-UTF-8 file names. AWS CLI rejects keys with invalid bytes, so we found and fixed such files before the copy.
  • Millions of small files. We tuned parallel requests so the copy would not slow down the live shop.
  • Thumbnails in catalog/product/cache. CloudFront will not generate a thumbnail the way local get.php does, so they must be copied or regenerated.

Cutover

Five steps to switch Magento to S3 and the CDN, plus the rollback plan
The media URL, S3 writes and imports are switched together. Illustrative diagram.

Changing base_media_url alone is not enough. The shop would show old files from S3 while new uploads and imports kept landing on the disk. That is why the cutover followed a fixed order:

  1. Media copy complete and verified under the CDN URL.
  2. Magento writes media to S3: remote_storage = aws-s3 with an empty prefix.
  3. base_media_url points to the CDN, one address for every store view.
  4. Configuration cache and full page cache flushed.
  5. Imports that wrote files by absolute path or through DirectoryList::PUB fixed. They moved to DirectoryList::MEDIA, otherwise crons and external imports would bypass S3.

The rollback plan was simple: clear base_media_url and return to the local driver, after first downloading from S3 the files created after the cutover.

What stays outside the CDN

Some assets were deliberately left out of phase one:

  • Product feeds are generated locally and exposed to partners at known URLs. A later step can publish them to S3.
  • Customer files and downloadable products can live in the same bucket, with no CloudFront access. Magento serves them through PHP.
  • Sessions, cache and queues do not belong on the application disk. They belong in Redis, Varnish and RabbitMQ.
  • Custom files in var/ are not covered by Remote Storage, which handles only media and import_export.

Costs

You pay for PUT requests during migration and ongoing uploads, S3 storage per GB per month, and transfer from CloudFront to users. Transfer from S3 to CloudFront and the ACM certificate itself are free. The largest one-off cost is usually the first migration: hundreds of gigabytes and millions of objects.

Result

Chart: one million media requests before and after the CDN at a 95 percent cache hit ratio
A model, not a measurement: at a 95% cache hit ratio the application server no longer serves media files.

Images no longer leave the same server as PHP and the database. CloudFront keeps files close to users, and with a cache of around 30 days repeat visits do not go back to the origin. B2C, B2B and country storefronts share one media host. Admin uploads, thumbnails and imports land in one place, which is the prerequisite for running several stateless application instances.

The bucket stays private. Only the paths CloudFront deliberately exposes are public.

Summary

The shop stayed where it was. Media is served from private S3 through CloudFront. What makes it work is switching the media URL and S3 writes at the same time, and fixing imports that bypass Magento's standard file API. DNS and CloudFront alone are only half the job.

Write to us
Other

Other case studies

An EKS cluster for tens of thousands of pods, upgraded without downtime

We built an Amazon EKS cluster where Karpenter matches EC2 nodes to the load and data lives on EFS and S3. We run tens of thousands of pods on hundreds of nodes there and upgrade the cluster regularly without interrupting the applications.

The S3 bill dropped from $20,000 to $1,500 a month

The client was overpaying for AWS: data in an expensive S3 class, oversized instances, an EKS cluster on paid Extended Support and data transfers for partners at its own cost. We went through the bill line by line, removed the causes and set up a daily cost view in Grafana.

A Magento shop lost its server together with its only backup

A large Magento shop served around one million customers. Backups ran every day, but they were written to the same server as the shop. When access to the server was lost, the shop and every backup became unavailable at the same time. We describe how we restored the shop and how backups now work in two independent places.

A 1 TB copy and a restore that took several days

The copy was 1 TB. After an outage the team restored the whole disk, while the service only needed a few files to run. Transfer, verification and bringing the machine up took several days. In this case study we describe how we split a start restore from the full image, so the service returns to traffic without waiting for the whole set.

A production database in a container without a persistent volume

The production database ran in a container. A restart, a node drain or a new image left an empty data directory. The application came up. The data did not. In this case study we show why database state cannot live in the image writable layer, and how we arranged a volume, a copy and a restore exercise.

AWS without a landing zone and without cost control

The AWS account grew without a landing zone, without a list of instance types and without a cost trail during the week. The bill arrived only with the invoice, at the end of the month. In this case study we describe how daily reports, threshold alerts and instance-type order went in, and what that did to the bill.