<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>AWS on smarx.com</title>
    <link>https://smarx.com/tags/aws/</link>
    <description>Recent content on smarx.com</description>
    <language>en-us</language>
    <managingEditor>smarx@smarx.com (Steve Marx)</managingEditor>
    <webMaster>smarx@smarx.com (Steve Marx)</webMaster>
    <lastBuildDate>Thu, 13 Aug 2020 00:00:00 +0000</lastBuildDate>
    
        <atom:link href="https://smarx.com/tags/aws/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>URL Rewriting With Lambda@Edge</title>
      <link>https://smarx.com/posts/2020/08/url-rewriting-with-lambda-at-edge/</link>
      <pubDate>Thu, 13 Aug 2020 00:00:00 +0000</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/08/url-rewriting-with-lambda-at-edge/</guid>
      <description>&lt;p&gt;I recently built &lt;a href=&#34;https://paschoolstats.com&#34;&gt;paschoolstats.com&lt;/a&gt;, a website to help Pennsylvania parents understand the statistics and guidance the state is providing schools about COVID-19.&lt;/p&gt;
&lt;p&gt;In the process of setting up hosting for the site, I learned a little about rewriting URLs with &lt;a href=&#34;https://aws.amazon.com/lambda/edge/&#34;&gt;Lambda@Edge&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The site is a single-page app (SPA) with just one file: &lt;code&gt;index.html&lt;/code&gt;.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; But it &lt;em&gt;looks&lt;/em&gt; like multiple pages because I wanted pretty URLs. Specifically, I wanted the URL when people viewed the statistics for Adams County to be &lt;a href=&#34;https://paschoolstats.com/adams&#34;&gt;paschoolstats.com/adams&lt;/a&gt;.&lt;/p&gt;





    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/08/url-rewriting-with-lambda-at-edge/paschoolstats.png&#34; data-lightbox=&#34;d0ea810d9f93529f964e6b03968be89f&#34; data-title=&#34;snapshot of https://paschoolstats.com/adams&#34; &gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/08/url-rewriting-with-lambda-at-edge/paschoolstats_hucf05ee60e1aba6ee29d9e241d596a9a3_99710_448x0_resize_lanczos_2.png&#34; width=&#34;448&#34; height=&#34;333&#34;&gt;
    
    &lt;/a&gt;
    
    
    &lt;figcaption&gt;
        &lt;p&gt;snapshot of &lt;a href=&#34;https://paschoolstats.com/adams&#34;&gt;https://paschoolstats.com/adams&lt;/a&gt;&lt;/p&gt;
    &lt;/figcaption&gt;
    
&lt;/figure&gt;

&lt;p&gt;This is a fairly typical requirement for SPAs, and it&amp;rsquo;s easily handled client-side with the HTML5 History API. I used &lt;a href=&#34;https://github.com/scssyworks/silkrouter&#34;&gt;SilkRouter&lt;/a&gt; to handle URL changes in the browser without requesting new pages from the server.&lt;/p&gt;
&lt;h2 id=&#34;the-challenge&#34;&gt;The challenge&lt;/h2&gt;
&lt;p&gt;A library like SilkRouter works great &lt;em&gt;on the client&lt;/em&gt;, but there&amp;rsquo;s actually a server-side component too. What if the user is looking at &lt;code&gt;paschoolstats.com/adams&lt;/code&gt; and decides to refresh the page? Or what if they bookmark that URL and visit it later directly?&lt;/p&gt;
&lt;p&gt;Despite the URL, I just want to serve up &lt;code&gt;index.html&lt;/code&gt; no matter what. (My client-side router will then show the right content based on the URL.) If you&amp;rsquo;re writing your own server code, this is pretty easy to do. Here&amp;rsquo;s an example in Go:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span class=&#34;kn&#34;&gt;package&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;main&lt;/span&gt;

&lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
	&lt;span class=&#34;s&#34;&gt;&amp;#34;net/http&amp;#34;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;main&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
	&lt;span class=&#34;nx&#34;&gt;http&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;HandleFunc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;/&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;w&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;http&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ResponseWriter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;r&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;http&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
		&lt;span class=&#34;nx&#34;&gt;http&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;ServeFile&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;w&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;index.html&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
	&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;

	&lt;span class=&#34;nx&#34;&gt;http&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;ListenAndServe&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;:8081&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But after recently setting up my new blog on AWS using S3 and CloudFront, I was hoping to be able to stick with that platform and not have to deploy a server anywhere.&lt;/p&gt;
&lt;h2 id=&#34;rewriting-urls-with-lambdaedge&#34;&gt;Rewriting URLs with Lambda@Edge&lt;/h2&gt;





    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/08/url-rewriting-with-lambda-at-edge/url-rewriting-featured.png&#34; data-lightbox=&#34;c3d591671024f80b62ea5c4ba13933ea&#34; &gt;
    
    &lt;img class=&#34;mx-auto p-6&#34; src=&#34;https://smarx.com/posts/2020/08/url-rewriting-with-lambda-at-edge/url-rewriting-featured_hu29fc7d7e26d7f94fe96bd19dbd665e94_106218_384x0_resize_lanczos_2.png&#34; width=&#34;384&#34; height=&#34;336&#34;&gt;
    
    &lt;/a&gt;
    
    
&lt;/figure&gt;

&lt;p&gt;&lt;a href=&#34;https://aws.amazon.com/lambda/edge/&#34;&gt;Lambda@Edge&lt;/a&gt; is a CloudFront feature that lets you run code out at &amp;ldquo;the edge&amp;rdquo;, meaning wherever CloudFront&amp;rsquo;s CDN is caching your content globally.&lt;/p&gt;
&lt;p&gt;Within your Lambda functions, you can manipulate requests and responses. This is perfect for doing URL rewriting for a single-page app.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the Lambda function I wrote to handle the rewrite:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;use strict&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;

&lt;span class=&#34;nx&#34;&gt;exports&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;handler&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;evt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;cb&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;request&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;evt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Records&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;].&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;cf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;

  &lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;uriParts&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;uri&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;split&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;/&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;

  &lt;span class=&#34;c1&#34;&gt;// Leave URLs like /static/favicon.png alone.
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;uriParts&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;!==&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;static&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;uri&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;/index.html&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;

  &lt;span class=&#34;nx&#34;&gt;cb&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;null&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;triggering-the-lambda-function&#34;&gt;Triggering the Lambda function&lt;/h2&gt;
&lt;p&gt;Functions you write with Lambda@Edge can be run in response to four different CloudFront events that happen in this order:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Viewer request&lt;/em&gt; &amp;ndash; runs for every request, before checking the cache&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Origin request&lt;/em&gt; &amp;ndash; runs on a cache miss, right before fetching from the origin&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Origin response&lt;/em&gt; &amp;ndash; runs after fetching from the origin but before caching&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Viewer response&lt;/em&gt; &amp;ndash; runs right before delivering the content&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Two of these events stand out as being relevant: viewer request and origin request. In both cases, you have a request URL that you can rewrite, but the performance characteristics of the two are different.&lt;/p&gt;
&lt;h3 id=&#34;viewer-request&#34;&gt;Viewer request&lt;/h3&gt;
&lt;p&gt;The viewer request event happens right away, before CloudFront even checks its cache. If you rewrite a URL here, things happen like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A request comes in for &lt;code&gt;/adams&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Lambda function rewrites the URL to &lt;code&gt;/index.html&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;CloudFront checks the cache. On a cache hit, the file is returned.&lt;/li&gt;
&lt;li&gt;On a cache miss, CloudFront fetches the file from S3, caches it for subsequent requests, and returns it to the viewer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is great because once someone&amp;rsquo;s loaded the home page, every other page will already be cached.&lt;/p&gt;
&lt;p&gt;The downside of this approach is that your Lambda function has to be invoked on every request no matter what.&lt;/p&gt;
&lt;h3 id=&#34;origin-request&#34;&gt;Origin request&lt;/h3&gt;
&lt;p&gt;The origin request event happens right before a file is fetched from the origin. If you rewrite a URL here, things happen like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A request comes in for &lt;code&gt;/adams&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;CloudFront checks the cache. On a cache hit, the file is returned.&lt;/li&gt;
&lt;li&gt;On a cache miss, the Lambda function rewrites the URL to &lt;code&gt;/index.html&lt;/code&gt; and CloudFront fetches that file from S3, caches it for subsequent requests, and returns it to the viewer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is great because cache hits are nice and fast, without having to execute the Lambda function at all.&lt;/p&gt;
&lt;p&gt;The downside of this approach is that there are more cache misses. &lt;code&gt;/adams&lt;/code&gt; has to be cached separately from &lt;code&gt;/allegheny&lt;/code&gt;, etc.&lt;/p&gt;
&lt;h3 id=&#34;which-is-better&#34;&gt;Which is better?&lt;/h3&gt;
&lt;p&gt;As I researched the options in the usual places (mostly StackOverflow), I found that recommendations varied. I was unable to find any discussion of the tradeoffs involved.&lt;/p&gt;
&lt;p&gt;I chose the origin request event simply because it was the first solution I saw. Performance wasn&amp;rsquo;t a big concern for me, but this is probably the faster option for an app like mine where the number of possible URLs is small. They&amp;rsquo;ll all get cached fairly quickly, and then my Lambda function never has to run again.&lt;/p&gt;
&lt;section class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;Okay, there&amp;rsquo;s also a favicon.png. You got me. &lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
  </channel>
</rss>
