<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Štefan Pe on Medium]]></title>
        <description><![CDATA[Stories by Štefan Pe on Medium]]></description>
        <link>https://medium.com/@stefan.pe?source=rss-6917b05a0667------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TqqvKRA1ajlkRoNJyZzDtw.png</url>
            <title>Stories by Štefan Pe on Medium</title>
            <link>https://medium.com/@stefan.pe?source=rss-6917b05a0667------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:12:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@stefan.pe/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Speed-up DevOps with CloudFront Functions]]></title>
            <link>https://medium.com/slido-dev-blog/speed-up-devops-with-cloudfront-functions-7eaef36ac8a7?source=rss-6917b05a0667------2</link>
            <guid isPermaLink="false">https://medium.com/p/7eaef36ac8a7</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloudfront]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cloudfront-functions]]></category>
            <dc:creator><![CDATA[Štefan Pe]]></dc:creator>
            <pubDate>Thu, 11 Aug 2022 07:37:00 GMT</pubDate>
            <atom:updated>2022-08-11T08:39:47.509Z</atom:updated>
            <content:encoded><![CDATA[<p>A sneak peak into <em>a</em> <em>future of — </em>Infrastructure as a <em>dynamic</em> Code</p><blockquote>Disclaimer: All examples are shown as Terraform code</blockquote><p>In the last few months AWS introduced a lot of new features for their<em> edge network services</em>, such as <strong>TLSv1.3</strong>, <strong>Server Timing Headers</strong>, <strong>True IP connection port</strong>, <strong>ECDSA certificate for viewer connections</strong>, <strong>CloudFront Functions</strong> and many more. In this post, I would like to take you through an example of a pretty handy use-case of the last item in the list - <strong>CloudFront Functions</strong>.</p><blockquote><strong>We will be delivering </strong>multiple static app versions with <strong>CloudFront Functions.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2lwfGgc9bfh5kTfg6c2O8A.jpeg" /></figure><h3>CloudFront Functions</h3><p>If you happen to be here, I suspect you already know what CloudFront is, but for those that don’t - <strong>AWS CloudFront</strong> is <strong>a content delivery network</strong> (<strong>CDN</strong>) service built for high performance, security, and developer convenience — that’s what AWS says - in other words it is a network of servers sprinkled around the world to make the content of webpages you (or your customers) open in your browser load way faster, sometimes cutting thousands of kilometers (and mili-seconds) of unnecessary latency.</p><p>What Cloudfront does is that as soon as it receives a request the first time - it loads it from origin and saves it to its cache for a configurable amount of time - even though we are explaining a very complex mechanism in the simplest possible form - and that’s why when another user fetches the same page, it loads way faster, as it is served from CloudFront server’s endpoint cache.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NQDr3bRQVG0A3fzTbABupQ.png" /></figure><blockquote>CloudFront Functions are ideal for lightweight processing of web requests.</blockquote><p><strong>AWS CloudFront Functions</strong> adds some computing power to the edge, this means that instead of simply serving a request CloudFront can add some magic into the processing, too.</p><p>Here’s what <strong>CloudFront Functions</strong> can be used for:</p><ul><li>Cache-key manipulations and normalization</li><li>HTTP header manipulation</li><li>Access authorization</li><li>URL rewrites and redirects</li></ul><p>Let’s have a look into the latter, <strong>URL rewrites and redirects</strong> with minor <strong>manipulation of HTTP headers</strong> and let’s get the party started.</p><p>This means that by running:</p><pre>curl -i https://example.com/?feature=feature-name</pre><p>you can get a different response and HTTP headers than you would get by running:</p><pre>curl -i https://example.com/</pre><p><strong>CloudFront Functions</strong> will take the URL, and, thanks to the tiny piece of code will <em>adjust the URL accordingly </em>and <em>add/modify</em> <em>HTTP headers</em> - once this is done, it will pass the modified request to CloudFront which will serve it from cache or a particular origin.</p><h4>Real use-case scenario</h4><h4>Tame staging environments with CloudFront Functions</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMZu_0STNsTqZF-ULPVa7w.png" /><figcaption>Simplified architecture — to serve purpose of this example — to serve a static code.</figcaption></figure><p>Developers develop their features in a local environment, which many times is tightly connected with the company’s <em>dev</em> environment. Once they are done, the next step is to lean it on some functional <em>API</em> and pass it on to <em>testers - at this point without merging the code into the main development branch.</em></p><p>Let’s begin with <strong><em>staging environment number one</em></strong>. In case you have more developers working on the same project, developing several features at once, your staging environment can look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i6RRnzG0mJzumhOi7uFRtQ.png" /><figcaption>Developer and tester queue to use the same staging environment to prove/test their feature.</figcaption></figure><p>If you have a number of developers working on and trying their new features or bug fixes on <em>staging environments</em>, you would probably like to have as many <em>free</em> environments as possible. This a common scenario, particularly in the front-end world. You, of course, can serve them with as many environments as they request, but this will create additional overhead for your teams and will also take some precious resources from you (<em>time = money</em>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5hinophbzRqSJLajs_2p5g.png" /><figcaption>Many environments to serve developers and testers needs.</figcaption></figure><p><em>But what if you could let all your developers share a single environment?</em></p><p>…and differentiate between branches with <em>one query parameter</em>? Yes, one tiny query parameter can offload your team and solve one unpleasant bottleneck. And here is where the <em>AWS CloudFront Functions</em> can be used, as mentioned above I deeply believe that this pretty small and simple feature from AWS can have many more use-cases than one might think at first [<a href="https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/">1</a>].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4OJPI8XyNu0g2XgUK0-d6w.png" /><figcaption>One environment to rule them all — thanks to CloudFront Functions.</figcaption></figure><p>So let’s begin. <em>First, create your CloudFront distribution </em>[<a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution">2</a>]<em> and focus on </em><strong><em>function_association</em></strong> <em>and</em><strong><em> aws_cloudfront_response_headers_policy.this.id</em></strong> parts, since the rest is common configuration for <em>CloudFront distribution</em>:</p><pre>resource &quot;aws_cloudfront_distribution&quot; &quot;this&quot; {<br>  enabled             = true<br>  is_ipv6_enabled     = true<br>  aliases             = var.aliases<br>  price_class         = var.price_class<br>  wait_for_deployment = false<br>  tags                = var.tags</pre><pre>  origin {<br>    domain_name = &quot;foo.s3.amazonaws.com&quot;<br>    origin_id   = &quot;s3&quot;<br>    origin_path = &quot;/application&quot;</pre><pre>    s3_origin_config {<br>      origin_access_identity = &quot;origin-access-identity/cloudfront/${var.cf_oai_id}&quot;<br>    }<br>  }</pre><pre>  default_cache_behavior {<br>    target_origin_id           = &quot;s3&quot;<br>    allowed_methods            = [&quot;GET&quot;, &quot;HEAD&quot;]<br>    cached_methods             = [&quot;GET&quot;, &quot;HEAD&quot;]<br>    viewer_protocol_policy     = &quot;redirect-to-https&quot;<br>    compress                   = true<br>    cache_policy_id            = var.cache_policy.managed_caching_optimized<br>    realtime_log_config_arn     = var.realtime_log_config_arn<br>    response_headers_policy_id = aws_cloudfront_response_headers_policy.this.id</pre><pre>    function_association {<br>      event_type   = &quot;viewer-request&quot;<br>      function_arn = aws_cloudfront_function.this.arn<br>    }</pre><pre>  viewer_certificate {<br>    ssl_support_method       = &quot;sni-only&quot;<br>    minimum_protocol_version = var.min_protocol_version<br>    acm_certificate_arn       = var.certificate_arn<br>  }</pre><pre>  restrictions {<br>    geo_restriction {<br>      restriction_type = &quot;none&quot;<br>    }<br>  }</pre><pre>  logging_config {<br>    bucket = var.log_bucket<br>    prefix  = var.log_prefix<br>  }</pre><pre>}</pre><p><em>Then, create an </em><strong><em>aws_cloudfront_function </em></strong>[<a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_function">3</a>] resource within Terraform:</p><pre>resource &quot;aws_cloudfront_function&quot; &quot;this&quot; {<br>  name    = var.aliases[0]<br>  runtime = &quot;cloudfront-js-1.0&quot;<br>  code    = templatefile(&quot;${path.module}/request.js&quot;)<br>}</pre><p>and create <strong>the actual function </strong>(<em>request.js</em>), where we send requests without ?feature=<strong> query parameter</strong> into the <strong>/</strong>host/ folder and <strong>requests with the query parameter </strong>?feature= into a folder within the S3 bucket called <strong>/</strong>features/.</p><p>This means all requests will go through this logic and end up serving an /index.html file. For special cases, like .css, .png or similar we apply the endsWith(suffix) function and serve the actual uri of the original request:</p><pre>function handler(event) {<br>  var request   = event.request;<br>  var s3_folder = &#39;/host/&#39;+request.headers.host.value;</pre><pre><br>  if (&quot;feature&quot; in request.querystring) {<br>      var feature_name = decodeURIComponent(request.querystring.team.value);<br>      s3_folder        = &#39;/feature/&#39;+feature_name;<br>  }</pre><pre><br>  if ([&#39;.css&#39;, &#39;.js&#39;, &#39;.map&#39;, &#39;.png&#39;, &#39;.svg&#39;, &#39;.txt&#39;, &#39;config.html&#39;].some(suffix =&gt; request.uri.endsWith(suffix))) {<br>      request.uri = s3_folder+request.uri;<br>  } else {<br>      request.uri = s3_folder+&#39;/index.html&#39;;<br>  }</pre><pre>  return request;<br>}</pre><p><em>Then create an </em><strong><em>aws_cloudfront_response_headers_policy </em></strong>[<a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_response_headers_policy">4</a>]</p><pre>resource &quot;aws_cloudfront_response_headers_policy&quot; &quot;this&quot; {<br>  name = var.aliases[0]<br>  security_headers_config {<br>    strict_transport_security {<br>      access_control_max_age_sec = 31536000<br>      include_subdomains         = true<br>      override                   = false<br>      preload                    = true<br>    }<br>  }<br>}</pre><p>There are <strong><em>some</em></strong> <strong><em>prerequisites</em></strong> - you or your <em>CI/CD</em> should upload all the necessary static files (index.html, or so) onto S3. The file-tree of the actual S3 bucket should (in this case) looks like this:</p><pre>static-files-bucket (s3 bucket)</pre><pre>  └── application/<br>    ├── host/<br>    │   ├── example.com/<br>    │   └── ...    <br>    ├── feature/<br>    │   ├── green-feature/<br>    │   ├── blue-feature/<br>    │   ├── yellow-feature/<br>    │   └── ...<br>    └── static/<br>      ├── &lt;static-000&gt;.js<br>      ├── &lt;static-001&gt;.js<br>      └── &lt;static-002&gt;.js<br>      └── ...<br>  ├── application-2/<br>  ...</pre><p>The query parameter ?feature is arbitrary and can be replaced with anything that suits your needs, it can be ?team,?branch, ?date, etc.</p><p>Thanks to this you are able to scale your free environments without killing your infrastructure, DevOps or SRE teams, all your developers can play without ever running out of staging environments.</p><blockquote>In this way your Infrastructure can become an important and <strong>dynamic</strong> component in your toolset.</blockquote><p>We all know that there are many ways to deliver this and accommodate the needs of staging environments (some create one CloudFront distribution for each new build and destroy it once there is no need for it, some do it in a completely different way), this is just one of those.</p><blockquote><em>Fresh, simple and handy.</em></blockquote><p><em>Do you have more ideas what to use CloudFront Functions for?</em></p><p><em>Do you approach staging environments in a different manner? Share it with us!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7eaef36ac8a7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/slido-dev-blog/speed-up-devops-with-cloudfront-functions-7eaef36ac8a7">Speed-up DevOps with CloudFront Functions</a> was originally published in <a href="https://medium.com/slido-dev-blog">Slido developers blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>