Jonathan Soifer
Jonathan Soifer

Reputation: 2837

Storing Phoenix static assets on Amazon S3

I need to provide some context before asking the question:

CONTEXT

I have a Phoenix application that is being deployed to Heroku. As default, Brunch is being used to compile the Static Assets like .js,.css and images.

The compilation process generates a cache_manifest.json, after the assets are digested using MD5 fingerprinting.

It maybe important to notice I'm using CloudFlare's free version as a CDN.

I'm not concerned about user uploaded assets, I'm talking about the app's assets

Relevant part of the apps config/prod.exs

config :bespoke_work, BespokeWork.Web.Endpoint, 
  on_init: {BespokeWork.Web.Endpoint, :load_from_system_env, []},
  http: [port: {:system, "PORT"}],
  url: [scheme: "https", host: System.get_env("HEROKU_HOST"), port: System.get_env("HEROKU_PORT")],
  static_url: [scheme: "https", host: System.get_env("STATIC_ASSETS"), port: 443],
  force_ssl: [rewrite_on: [:x_forwarded_proto]],
  cache_static_manifest: "priv/static/cache_manifest.json",
  secret_key_base: System.get_env("SECRET_KEY_BASE")```

QUESTION

  1. How can I prevent Heroku from building the assets and, instead, during deploy, automatically upload the digested assets to an Amazon S3 Bucket?

  2. Will that make Heroku's slug smaller?


POSSIBLE SOLUTION

  1. Reducing Heroku's Slug Size:

    • On the Procfile redirect mix phx.digest to output digested items to /dev/null.

    or

    • Redefine mix deps.compile for Prod, not generating the assets.


  1. Generate the assets locally.

  1. Either Manually upload them or use a Shell Script to upload them to S3. (In my case probably using Codeship's Amazing Amazon S3 deployment)

  1. Use static_url to generate paths "pointing" to the S3 Bucket.

Is there any simpler way to accomplish this?

Upvotes: 0

Views: 504

Answers (1)

Jonathan Soifer
Jonathan Soifer

Reputation: 2837

This is not really a good idea... it might even be considered an anti-pattern.

But, if for some reason, that is absolutely necessary, this is one way to do it:

Reducing the Slug Size on Heroku:


Automating Amazon S3 Uploads

• This should be done through a shell script.

Upvotes: 0

Related Questions