<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rob Kenis</title>
    <description>The latest articles on DEV Community by Rob Kenis (@robkenis).</description>
    <link>https://dev.to/robkenis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F170076%2F2d533723-b8df-40f7-842d-9a1888181c2c.jpg</url>
      <title>DEV Community: Rob Kenis</title>
      <link>https://dev.to/robkenis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robkenis"/>
    <language>en</language>
    <item>
      <title>Enable Hugo pretty URLs with AWS CloudFront Functions</title>
      <dc:creator>Rob Kenis</dc:creator>
      <pubDate>Fri, 19 Nov 2021 21:24:43 +0000</pubDate>
      <link>https://dev.to/robkenis/enable-hugo-pretty-urls-with-aws-cloudfront-functions-1mlf</link>
      <guid>https://dev.to/robkenis/enable-hugo-pretty-urls-with-aws-cloudfront-functions-1mlf</guid>
      <description>&lt;p&gt;There's nothing as pleasing as sending a clean URL to a friend or a colleague. No complicated path, no extensions, just a plain and simple URL. As I explained in &lt;a href="https://robkenis.com/posts/hosting_your_blog_on_aws/"&gt;this post&lt;/a&gt;, I hosted my statically linked website on AWS S3. This is great for cost savings and low maintenance, but it comes with a trade-off.&lt;/p&gt;

&lt;p&gt;When hosting websites on S3, you can configure your &lt;em&gt;index page&lt;/em&gt; and your &lt;em&gt;error page&lt;/em&gt;. When you configure &lt;code&gt;index.html&lt;/code&gt; as your index page, &lt;code&gt;https://robkenis.com/&lt;/code&gt; will lead to &lt;code&gt;https://robkenis.com/index.html&lt;/code&gt;. Wonderful, your single-page application works without further configuration. But what about &lt;code&gt;https://robkenis.com/posts/hosting_your_blog_on_aws/&lt;/code&gt; …there is no option to configure an index page for that path straight from S3.&lt;/p&gt;

&lt;p&gt;The work-around I used in the initial setup, was enabling &lt;em&gt;ugly URLs&lt;/em&gt; in Hugo. This would export the post as &lt;code&gt;/posts/hosting_your_blog_on_aws.html&lt;/code&gt; and create the links to the post accordingly. Although my site worked, I was unhappy with the need for extensions in my URLs.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS Lambda to the rescue
&lt;/h3&gt;

&lt;p&gt;There is another way to manipulate the URLs, a way in which the user won't even notice. Why don't we  intercept the request before it reaches S3 and append &lt;code&gt;/index.html&lt;/code&gt; to all URLs that need it? This would  mean clean URLs for the user that point to actual files in S3. &lt;/p&gt;

&lt;p&gt;Obviously I wasn't the first one to come up with this solution. Years ago, AWS introduced &lt;a href="https://aws.amazon.com/lambda/edge"&gt;Lambda @ Edge&lt;/a&gt;, a solution that enables you to execute serverless functions directly from your CloudFront Distribution. So no servers to maintain, you only need to write a Lambda function and configure CloudFront to call it when a request is received.&lt;/p&gt;

&lt;p&gt;Great solution, but can be improved on!&lt;/p&gt;

&lt;h3&gt;
  
  
  Introducing CloudFront Functions
&lt;/h3&gt;

&lt;p&gt;And here we are, a couple of months after Lambda @ Edge was possible, the process to execute functions at the edge was made simpler. Back in May 2021, AWS introduced &lt;a href="https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/"&gt;CloudFront Functions&lt;/a&gt;, a solution that provides the option to write small, fast functions that execute when a user arrives at your CDN.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating a function
&lt;/h4&gt;

&lt;p&gt;For those who have written a Lambda before, the next part will seem familiar. Your function has a handler method, which takes an event as input. The same applies to CloudFront Functions. I'll explain further with the code I used to make pretty URLs possible for my own website.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;endsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When an event of type &lt;a href="https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/aws-lambda/common/cloudfront.d.ts#L44"&gt;CloudFrontRequest&lt;/a&gt; is received, the URI is taken from the input. When the URI ends with a &lt;code&gt;/&lt;/code&gt; or has no extension, &lt;code&gt;/index.html&lt;/code&gt; is added to the URI, so it points to the correct file in S3.&lt;/p&gt;

&lt;p&gt;Now we have our logic inside a Lambda function. All that is left, is attaching it to our CloudFront. In the  same fashion as the previous post, I'll share the CloudFormation snippet below. The following CloudFormation resource will create a CloudFront Function. For now the only runtime available is &lt;code&gt;cloudfront-js-1.0&lt;/code&gt;. To me, this looks like very old JavaScript, so no &lt;em&gt;const&lt;/em&gt; and &lt;em&gt;let&lt;/em&gt; yet. To view your function, head over to the CloudFront console and find 'CloudFront Functions' in the menu on the left side.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;PrettyURLs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;AutoPublish&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
      &lt;span class="na"&gt;FunctionCode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;function handler(event) {&lt;/span&gt;
            &lt;span class="s"&gt;var request = event.request;&lt;/span&gt;
            &lt;span class="s"&gt;var uri = request.uri;&lt;/span&gt;
            &lt;span class="s"&gt;if (uri.endsWith('/')) {&lt;/span&gt;
                &lt;span class="s"&gt;request.uri += 'index.html';&lt;/span&gt;
            &lt;span class="s"&gt;}&lt;/span&gt;
            &lt;span class="s"&gt;else if (!uri.includes('.')) {&lt;/span&gt;
                &lt;span class="s"&gt;request.uri += '/index.html';&lt;/span&gt;
            &lt;span class="s"&gt;}&lt;/span&gt;
            &lt;span class="s"&gt;return request;&lt;/span&gt;
        &lt;span class="s"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;FunctionConfig&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;Comment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Enable pretty URLs for Hugo&lt;/span&gt;
        &lt;span class="na"&gt;Runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cloudfront-js-1.0&lt;/span&gt;
      &lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!Join&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-'&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;!Ref&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;AWS::StackName'&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;rewrite-pretty-urls&lt;/span&gt;
    &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::CloudFront::Function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will only create the CloudFront Function, but without configuring it, it will never be called. To attach it to a Distribution, we must configure it like any Lambda @ Edge function. We must pick an &lt;code&gt;EventType&lt;/code&gt;, and this is where CloudFront Functions are limited. Out of the 4 options, we can only pick &lt;code&gt;viewer-request&lt;/code&gt; and &lt;code&gt;viewer-response&lt;/code&gt;, so no modifying the request between the Distribution and the Origin during the &lt;br&gt;
&lt;code&gt;origin-request&lt;/code&gt; and &lt;code&gt;origin-response&lt;/code&gt; events. Next to the EventType, we must pass the reference to our CloudFront Function using the ARN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;CloudFront&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;DistributionConfig&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="s"&gt;...&lt;/span&gt;
        &lt;span class="s"&gt;DefaultCacheBehaviour&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
          &lt;span class="s"&gt;...&lt;/span&gt;
          &lt;span class="s"&gt;FunctionAssociations&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;EventType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;viewer-request&lt;/span&gt;
            &lt;span class="na"&gt;FunctionARN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;!GetAtt&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PrettyURLs.FunctionARN'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After attaching the Function to the Distribution, you're all set. When a user requests a page at &lt;code&gt;https://robkenis.com/posts/whatever-post-comes-next/&lt;/code&gt;, &lt;code&gt;index.html&lt;/code&gt; will be added so the user is served the correct page straight from S3, without managing any servers or adding noticeable costs.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>webdev</category>
      <category>cloudnative</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
