<?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 Emiliano Gabrielli on Medium]]></title>
        <description><![CDATA[Stories by Emiliano Gabrielli on Medium]]></description>
        <link>https://medium.com/@emiliano.gabrielli?source=rss-47dbe1bd9686------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*07rV9qf1Qd_IoTFR958uEA.png</url>
            <title>Stories by Emiliano Gabrielli on Medium</title>
            <link>https://medium.com/@emiliano.gabrielli?source=rss-47dbe1bd9686------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:27:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@emiliano.gabrielli/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[Deploy a static web application on AWS, the right way]]></title>
            <link>https://medium.com/faktiva/deploy-a-static-website-on-aws-the-right-way-e83f47d60fdc?source=rss-47dbe1bd9686------2</link>
            <guid isPermaLink="false">https://medium.com/p/e83f47d60fdc</guid>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[static-websites]]></category>
            <category><![CDATA[cloudfront]]></category>
            <category><![CDATA[https]]></category>
            <dc:creator><![CDATA[Emiliano Gabrielli]]></dc:creator>
            <pubDate>Sun, 14 Apr 2019 00:39:02 GMT</pubDate>
            <atom:updated>2019-10-08T08:03:27.216Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Using CloudFront and S3 to deliver an HTTPS-only static web application, while avoiding misuse of the 403 response code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dUrdiG31cgNCwK-4" /><figcaption>Photo by <a href="https://unsplash.com/@adliwahid?utm_source=medium&amp;utm_medium=referral">Adli Wahid</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I stumbled for a while on tons of posts, articles and tutorials about deploying static web sites on Amazon S3.<br>Every resource I was able to consult uses a <strong>public S3 bucket</strong> and <strong>masks 403 errors as 404</strong>; which sounds so ugly to me and should not be necessary at all, especially when https is required too.</p><p>So, my goal was to have a static web application (for example a <em>React</em> application) having the following requirements all fulfilled:</p><ol><li>the application has to be only served <strong><em>via https</em></strong>, mandatory nowadays for a number of quite obvious reasons</li><li>the application have to take advantage of <strong><em>caching</em></strong> mechanisms to minimize the number of requests both on <em>client to shared-cache</em> and <em>shared-cache to origin</em> side</li><li>the application have to be able to handle users login, this means that nonexistent files on <em>S3</em> <strong>can’t</strong> be handled with custom error pages for the <em>403</em> HTTP code</li></ol><p>To fulfill the <em>point 1</em> we have to put an <em>SSL terminator</em> in front of the origin <em>S3</em> bucket (it could be a proxy or a CDN for example).</p><p>A common and reasonable choice to terminate SSL in front of <em>S3</em> is adding an <strong><em>AWS CloudFront distribution</em></strong> … and of course there’s nothing new in this approach.</p><p>I decided to write this article to expose the way we will use <em>CloudFront</em>, in combination with <em>S3</em>, to force the use of the <em>HTTPS protocol </em>to access the origin. Choosing <em>CloudFront</em> gives us the opportunity to satisfy the other points we required as well.</p><p>Let’s go ahead and see how to do the job, step by step.</p><h4>Deploy on S3</h4><p>Amazon Simple Storage Service (aka <em>S3</em>) is a fully managed AWS service implementing an <em>object storage service</em> that provides high scalability, data availability, security, and performance. It can be used to store and protect any amount of data at any scale and it is designed for <em>99.999999999%</em> (<em>eleven 9’s</em>) of durability.</p><p>So, <em>S3</em> buckets are a very good choice to store static assets in a simple, affordable, cost effective way. We’ll use it to store and provide HTML, CSS and JS files, as long as any other asset the application would require.</p><ul><li>create an account or sign in to the AWS Console at <a href="https://console.aws.amazon.com/">https://console.aws.amazon.com</a></li><li>navigate to the <a href="https://s3.console.aws.amazon.com/s3/home">S3 service</a>, click <strong>Create Bucket</strong> and pick up a globally unique name for your new bucket; then click <strong>Create</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qti5PIxhe-EHZU8mtWzrOw.png" /></figure><ul><li>configure to <strong>block and remove any public access</strong> attempt to the bucket</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a0Ov2i1a1QztPP_u4WkLWw.png" /></figure><ul><li><strong>don’t enable the </strong><strong>website hosting option</strong>, your bucket don’t need it and there is no reason to have it publicly accessible!</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jadxi9orFj1MnAHHvNSl5g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E401_arM6RjGBfD3eSwohg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sWHiePq3RRSmw5b25Ucbtw.png" /></figure><h4>Enforce HTTPS</h4><p>As said, we want to serve our application only via HTTPS. This is a security requirement and a good SEO practice. As <em>S3</em> only support HTTP protocol, and we are working on AWS, the natural choice to add HTTPS support is to implement a <strong><em>CloudFront</em></strong> distribution in front of the origin <em>S3</em> bucket.</p><p>This choice allow us to take advantage of a number of other desirable features, such as:</p><ul><li>automatic redirection of the requests from HTTP to HTTPS directly on the edge (on the CDN Point of Presence, which means very near to the user … globally)</li><li>latency minimization by means of the CDN global and highly distributed nature</li></ul><p>Let’s create a new CF distribution and follow these steps:</p><ul><li>navigate to the <a href="https://console.aws.amazon.com/cloudfront/home">CloudFront</a> service, click <strong>Create Distribution</strong> and select a <strong><em>WEB delivery method</em></strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C7HmOX0CzeWwXlmn_X017A.png" /></figure><ul><li>pick the <em>S3</em> bucket just created in the <strong>Origin Domain Name </strong>selection drop-down</li><li>flag the <strong>Restrict Bucket Access </strong>and create an <strong>Origin Access Identity</strong> or pick an existing one</li><li>flag the <strong>Yes, Update Bucket Policy</strong> option (we’ll modify the policy later anyway)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*blKJUBPEYwDD5yo8KcxQ5g.png" /></figure><ul><li>in the <strong>Default Cache Behavior</strong> select the <strong>Redirect HTTP to HTTPS</strong> option</li><li>pick <strong>Get, HEAD</strong> in the <strong>Allowed HTTP Methods</strong> selection</li><li>adjust settings to maximize caching: <strong>no</strong> <em>request headers forwarding</em>, <strong>no</strong> <em>cookies forwarding</em>, <strong>no</strong> <em>query string forwarding</em> and <em>automatic compression</em> of the response objects</li><li>it is important to leave flagged on the <strong>Use Origin Cache Headers</strong> to allow <em>S3</em> to manage caching on a per object basis (i.e. per file). This means we can (and should) use different caching strategies for different files types</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UaGBLDmoDnz4ZTu6td9hqw.png" /></figure><ul><li>set the <strong>Default Root Object</strong> to your application main file (for example index.html)</li><li>set the distribution <strong>CNAME</strong> to the domain name you want to use for your new shining static web site (or leave blank to use the distribution name)</li><li>to have a valid certificate you have to generate a <strong>Custom SSL Certificate</strong> here (otherwise just select the <em>CloudFront</em> default one)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hc_6382gbzs0R81KXuNPgA.png" /></figure><ul><li>click <strong>Create Distribution</strong></li></ul><h4>Configure application routing</h4><p>A static site could be make by only real static contents, such has HTML, CSS, and JS and in this case the job is already done.</p><p>But to allow a client side web application to work we need to enable its routing. This means that a “virtual path” (the route to the application action) has to be distinguished from a real server-side file or directory.<br>The only one to know this difference is the application itself, not the server and not the CDN. It has to properly manage existing routes and return a not found for stuff that isn’t handled. Client side!</p><p>To achieve this we need to define a <strong>Custom Error Response</strong> and point it to the application router (i.e. the /index.html).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9e8mewccoX5gI1enHGpqCw.png" /></figure><p>Here it comes the very original part of this article.</p><p>The default access policy in <em>S3</em> (the one we asked <em>CloudFront</em> to automatically created for us) doesn’t allow directory listing. And this is the right default, because the <em>S3</em>-only standard solution for a static website hosting requires to enable the <strong>website hosting</strong> option on the bucket but we don’t want to allow everybody to list our bucket contents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CYNhvz04EPTQwpWzaCGykA.png" /></figure><p>But, as we protected our bucket (it is not public and it is only accessible through the <em>CloudFront</em> distribution we just created), we can safely relax this configuration and modify the bucket policy, allowing the bucket listing:</p><pre>{<br>    &quot;Sid&quot;: &quot;AllowListBucketContents&quot;,<br>    &quot;Effect&quot;: &quot;<em>Allow</em>&quot;,<br>    &quot;Principal&quot;: {<br>        &quot;AWS&quot;: &quot;<em>arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity </em><strong><em>E2RM9I206MPVYE</em></strong>&quot;<br>    },<br>    &quot;Action&quot;: &quot;<strong><em>s3:ListBucket</em></strong>&quot;,<br>    &quot;Resource&quot;: &quot;<em>arn:aws:s3:::s3-cloudfront-https-only-howto</em>&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*Eg-VKFliH_oR0Yy-vIlsNw.png" /></figure><p><strong>And this is the Columbus egg!</strong> As this allows us to distinguish between the 403 and 404<em> S3</em> status codes. This is important, because it enables us to require an <strong><em>authorization in our application</em></strong>, if we need it. If we can’t distinguish between an “<em>403: forbidden</em>” and a “<em>404: not found</em>” then a failed login is mapped to a not found and then handled by the router instead of showing a login form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sfapgBdQyeqf2z8lDVdMyA.png" /></figure><p>Remember: to handle client-side application routing <strong>don’t create a custom error page for the </strong><strong>403 error code!</strong> This is not necessary and <strong>it is wrong</strong>.</p><p>You are done.</p><p>Enjoy your secure and performant new shiny <strong><em>https-only</em> static web application on AWS <em>S3</em> plus <em>CloudFront</em></strong> :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e83f47d60fdc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/faktiva/deploy-a-static-website-on-aws-the-right-way-e83f47d60fdc">Deploy a static web application on AWS, the right way</a> was originally published in <a href="https://medium.com/faktiva">faktiva</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>