Oscar Godson
Oscar Godson

Reputation: 32736

Deploy Node.js app to AWS elastic beanstalk that contains static assets

I'm having some trouble visualizing how I should handle static assets with my EB Node.js app. It only deploys whats committed in the git repo when you do eb deploy (correct?) but I don't want to commit all our static files. Currently we are uploading to S3 and the app references those (the.s3.url.com/ourbucket/built.js), but now that we are setting up dev, staging, and prod envs we can reference built.js since there can be up to 3 versions of it.

Also, there's a timespan where the files are uploaded and the app is rolling out and the static assets don't work with the two versions up on the servers (i.e. built.js works with app version 0.0.2 but server one is deploy 0.0.1 and server two is running version 0.0.2)

How do keep track of these mismatches or is there a way to just deploy a static assets to the EB instance directly.

Upvotes: 0

Views: 567

Answers (1)

Max
Max

Reputation: 8836

I recommend a deploy script that uploads relevant assets to S3 then perform the Elasticbeanstalk deploy. In that script, upload the S3 assets to a folder with the name of the env, so you'll have

the.s3.url.com/ourbucket/production/
the.s3.url.com/ourbucket/staging/
the.s3.url.com/ourbucket/dev/

Then you have the issue of old assets during deploy - in general, you should probably be CDNing these assets (I recommend CloudFront because its so easy to integrate once you're already on AWS) and you should be worrying about cache invalidation during deploy anyway. One strategy for dealing with that is to assign an ID to each deploy (either the first 7 letters of the git sha1 or a timestamp) and put all assets in a new folder with that name, then reference that on your HTML pages. So lets say you go with timestamp, and you deploy at 20150204-042501 (that's 4:25 and 1 second UTC on February 4, 2015) so you'd upload your assets to the.s3.url.com/ourbucket/production/20150204-042501/. Your HTML would say

<script src="//the.s3.url.com/ourbucket/production/20150204-042501/built.js" />

That solves both the "during deploy" problem and cache invalidation.

Upvotes: 1

Related Questions