Deploying Ember Apps on Fastly
Deploying Ember Apps
on Fastly
James A Rosen
Slide 2
Slide 2 text
Deploying Ember Apps on Fastly
•
Lightning Deployment
•
Just enough Varnish & VCL to be
dangerous
•
The Fastly Content Delivery Network
•
Putting it together: GitHub, Travis,
Google Cloud Storage, & Fastly
What You’ll Learn
Slide 3
Slide 3 text
Deploying Ember Apps on Fastly
What You’ll Learn
Developer
GitHub Travis CI
Google
Cloud
Storage
Fastly
Edge
Server
Browser
Fastly
API
Pull
Push
index.html
*.css, *.js, …
Slide 4
Slide 4 text
Deploying Ember Apps on Fastly
Lightning Deployment
Slide 5
Slide 5 text
Deploying Ember Apps on Fastly
S3 + Redis Deployment
Developer
Amazon
S3
Reverse
Proxy
Browser
Pull
Push
Redis
*.js, *.css, ...
index.
html
Slide 6
Slide 6 text
Deploying Ember Apps on Fastly
•
Publish: push fingerprinted assets to S3
•
Activate: push index.html to Redis
•
samselikoff/ember-deploy
•
Merged into ember-cli-deploy
•
The Art of Ember Deployment
ember-deploy
Slide 7
Slide 7 text
Deploying Ember Apps on Fastly
Varnish
Slide 8
Slide 8 text
Deploying Ember Apps on Fastly
vcl_recv — receive and route the request
vcl_fetch — make request to origin on
cache miss
vcl_deliver — post-process response
before delivery to client
vcl_error — handle errors from origin
VCL: Flow
Slide 9
Slide 9 text
Deploying Ember Apps on Fastly
•
https://www.varnish-cache.org/
•
Caching Reverse Proxy
•
Reduce load, increase speed
•
You need to store your content somewhere
else
•
Fast purges
•
Varnish Configuration Language (VCL)
Varnish
Slide 10
Slide 10 text
Deploying Ember Apps on Fastly
•
Publish: push fingerprinted assets to S3
•
Activate: push index.html to Redis
•
Use Varnish as reverse proxy
S3 + Redis + Varnish Deployment
Slide 11
Slide 11 text
Deploying Ember Apps on Fastly
Routing Requirements
https://myapp.com/assets/app.js
s3://assets/app.js
https://myapp.com/robots.txt
s3://robots.txt
https://myapp.com/billing/
redis://index.html
https://myapp.com/
redis://index.html
Slide 12
Slide 12 text
Deploying Ember Apps on Fastly
sub vcl_recv {
if (req.http.Accept == "text/html") {
set req.http.url = "/index.html";
set req.backend = redis;
} else {
set req.backend = s3;
}
}
VCL: Routing
Slide 13
Slide 13 text
Deploying Ember Apps on Fastly
IE8:
image/gif,
image/jpeg,
image/pjpeg,
application/x-ms-application,
application/vnd.ms-xpsdocument,
application/xaml+xml,
application/x-ms-xbap,
*/*
VCL: Routing
Slide 14
Slide 14 text
Deploying Ember Apps on Fastly
sub vcl_recv {
if (req.http.url ~ "^/assets(/|$)" ||
req.http.url == "/robots.txt" ||
…) {
set req.backend = s3;
} else {
set req.http.url = "/index.html";
set req.backend = redis;
}
}
VCL: Routing
Slide 15
Slide 15 text
Deploying Ember Apps on Fastly
nginx: try_files
try_files $uri 404.html
# or for a single-page app:
try_files $uri /index.html
Slide 16
Slide 16 text
Deploying Ember Apps on Fastly
VCL: try_files
sub vcl_error {
if (resp.status == 404 &&
!req.http.tried-index-html) {
set req.url = "/index.html";
set req.http.tried-index-html = true;
return (restart);
}
}
Deploying Ember Apps on Fastly
•
Publish: push everything to S3
•
Activate: purge index.html
S3 + Varnish Deployment
Slide 20
Slide 20 text
Deploying Ember Apps on Fastly
VCL: Synthetics
• Responses directly from VCL
• Really fast
• Really really fast on Fastly
• Abuses errors for control flow
Slide 21
Slide 21 text
Deploying Ember Apps on Fastly
VCL: Synthetics
sub vcl_fetch {
if (req.http.url == "/index.html") {
error 900 "index.html";
}
…
}
sub vcl_error {
if (obj.status == 900) {
set obj.status = 200;
set obj.response = "OK";
synthetic {"…"};
}
…
}
Slide 22
Slide 22 text
Deploying Ember Apps on Fastly
Edge Dictionaries
(Fastly Varnish)
Slide 23
Slide 23 text
Deploying Ember Apps on Fastly
Before:
if (req.http.user-agent ~ "Bot 1") {
error 404 "Bot Detected";
} else if (req.http.user-agent ~ "Bot 2") {
error 404 "Bot Detected";
}
…
Fastly VCL: Edge Dictionaries
Slide 24
Slide 24 text
Deploying Ember Apps on Fastly
After:
table bots {
"Bot 1": "true",
"Bot 2": "true",
…
}
…
if (table.lookup(bots, req.http.User-Agent))
Fastly VCL: Edge Dictionaries
Slide 25
Slide 25 text
Deploying Ember Apps on Fastly
• Can update tables from API without uploading new VCL
• Update globally in seconds
• Limited storage space
Fastly VCL: Edge Dictionaries
Slide 26
Slide 26 text
Deploying Ember Apps on Fastly
Deploying on Fastly
Slide 27
Slide 27 text
Deploying Ember Apps on Fastly
table appAssets {
"index.html":
"…"
}
Deploying to Fastly
Slide 28
Slide 28 text
Deploying Ember Apps on Fastly
sub vcl_recv {
unset req.http.X-App-Content;
if (req.url.path == "/" ||
req.url.path == "/index.html") {
set req.http.X-App-Content =
table.lookup(appAssets,
"index.html");
error 900 "index";
}
}
Deploying to Fastly
Slide 29
Slide 29 text
Deploying Ember Apps on Fastly
sub vcl_error {
if (obj.status == 900) {
set obj.status = 200;
set obj.response = "OK";
synthetic X-App-Content;
}
}
Deploying to Fastly
Slide 30
Slide 30 text
Deploying Ember Apps on Fastly
What You’ve Learned
Developer
GitHub Travis CI
Google
Cloud
Storage
Fastly
Edge
Server
Browser
Fastly
API
Pull
Push
*.css, *.js, …
index.html
Slide 31
Slide 31 text
Deploying Ember Apps on Fastly
Slide 32
Slide 32 text
Deploying Ember Apps on Fastly
• https://www.varnish-cache.org/
• https://github.com/samselikoff/ember-deploy
• https://github.com/ember-cli/ember-cli-deploy
• https://www.youtube.com/watch?v=4EDetv_Rw5U
• http://blog.testdouble.com/posts/2015-11-03-deploying-
ember-to-aws-cloudfront-using-ember-cli-deploy.html
• https://docs.fastly.com
References