<?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: mentalcaries</title>
    <description>The latest articles on DEV Community by mentalcaries (@mentalcaries).</description>
    <link>https://dev.to/mentalcaries</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%2F834071%2F29b341cf-fd93-44a7-acf8-bc0989d3d662.jpeg</url>
      <title>DEV Community: mentalcaries</title>
      <link>https://dev.to/mentalcaries</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mentalcaries"/>
    <language>en</language>
    <item>
      <title>Deploying a static site using AWS S3, Cloudfront and Domain.com</title>
      <dc:creator>mentalcaries</dc:creator>
      <pubDate>Sat, 08 Apr 2023 20:02:22 +0000</pubDate>
      <link>https://dev.to/mentalcaries/deploying-a-static-site-using-aws-s3-cloudfront-and-domaincom-lo3</link>
      <guid>https://dev.to/mentalcaries/deploying-a-static-site-using-aws-s3-cloudfront-and-domaincom-lo3</guid>
      <description>&lt;p&gt;As part of &lt;a href="https://cloudresumechallenge.dev/"&gt;The Cloud Resume Challenge&lt;/a&gt;, one of the tasks is pointing a custom domain to your static site in a CloudFront Distribution.&lt;/p&gt;

&lt;p&gt;Like most devs, I probably registered more domains for myself than necessary, so I figured I could use one that I had registered from &lt;a href="https://domain.com"&gt;Domain.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This guide assumes that you've already enabled Static website hosting for your S3 bucket, and that you also own a domain. Domains can be purchased through AWS Route53 or any third party provider. For this case, we'll focus on the config required for Domain.com.&lt;/em&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Why Cloudfront?
&lt;/h3&gt;

&lt;p&gt;Now, even though your static site is accessible via the bucket website endpoint, typically something like &lt;code&gt;http://some-bucket-name.s3-website-us-east-1.amazonaws.com&lt;/code&gt;, the problem list in the first four characters: your content is served over HTTP.&lt;/p&gt;

&lt;p&gt;For best practices, you want to use &lt;a href="https://www.cloudflare.com/learning/ssl/what-is-https/"&gt;HTTPS&lt;/a&gt;, which is the secure version of HTTP. It uses encryption, and that's particularly important for sensitive information. There's really no reason to &lt;em&gt;not&lt;/em&gt; use it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cloudfront enables this!!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;a href="https://aws.amazon.com/cloudfront/"&gt;Amazon CloudFront&lt;/a&gt; is a content delivery network (CDN) service built for high performance, security, and developer convenience.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Creating a Distribution
&lt;/h3&gt;

&lt;p&gt;In CloudFront, you'll need to click &lt;strong&gt;Create Distribution&lt;/strong&gt; and then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set your Origin domain to point to your S3 bucket.&lt;/li&gt;
&lt;li&gt;Enter a name for the origin. This can be anything really, but you can leave it as the default name.&lt;/li&gt;
&lt;li&gt;Select the recommended Origin Access Control Settings for &lt;strong&gt;Origin Access&lt;/strong&gt;. Once selected, you will need to:&lt;/li&gt;
&lt;li&gt;Create a control setting. Give it a name (or leave the default) and click Create. Once you do this, you will be warned that your bucket policy will need to be updated afterwards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OiCMYKQQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzkhdsq7bsoinv16n3mt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OiCMYKQQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzkhdsq7bsoinv16n3mt.png" alt="Setting Origin and OAC" width="800" height="710"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Head to the &lt;strong&gt;Default root object&lt;/strong&gt; and set your default object, which should generally be &lt;code&gt;index.html&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Hit &lt;strong&gt;Create Distribution&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You should receive a prompt to copy your bucket policy, so be sure to do that and paste in the permissions for the respective S3 bucket!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gI_Y5TCu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1zosx26il18nw9oz81c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gI_Y5TCu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1zosx26il18nw9oz81c.png" alt="Create Distribution" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point give your distribution some time to deploy. You can test if your website is accessible going to the &lt;em&gt;Distribution domain name&lt;/em&gt; under &lt;strong&gt;General&amp;gt;Details&lt;/strong&gt;. Notice the URL for this now has &lt;strong&gt;https&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Distribution Deployed
&lt;/h3&gt;

&lt;p&gt;Once your new or existing distribution is deployed, here's where we can set up the custom domain. In this case we'll be using the &lt;strong&gt;Alternate domain name (CNAME)&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A Canonical Name or CNAME record is a type of DNS record that maps an alias name to a true or canonical domain name.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;strong&gt;alias name&lt;/strong&gt; is the domain name that you registered, eg &lt;a href="https://iowntoomanydomains.com"&gt;https://iowntoomanydomains.com&lt;/a&gt;.&lt;br&gt;
The true/canonical domain name is that set up by Cloudfront, eg. &lt;a href="https://d2r7ncgogp998k.cloudfront.net"&gt;https://d2r7ncgogp998k.cloudfront.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For your custom domain name to work, you'll need to request an SSL certificate and add it to your distribution to verify that you are authorized to use the domain.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;An SSL certificate is a digital certificate that authenticates a website's identity and enables an encrypted connection. SSL stands for Secure Sockets Layer, a security protocol that creates an encrypted link between a web server and a web browser.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you've deployed to Vercel or Netlify before, this is automatically done for you. With AWS, it takes a little setting up:&lt;/p&gt;




&lt;h3&gt;
  
  
  Requesting a Certificate
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Find your static site distribution in CloudFront and open it up.&lt;/li&gt;
&lt;li&gt;On the &lt;strong&gt;General&lt;/strong&gt; tab, click on &lt;em&gt;Edit&lt;/em&gt; under Settings. &lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;Alternate domain name (CNAME)&lt;/strong&gt;, click &lt;em&gt;Add item&lt;/em&gt; and add your domain name, eg &lt;code&gt;https://mynewdomain.com&lt;/code&gt; and you can also add a second &lt;code&gt;https://www.mynewdomain.com&lt;/code&gt; to ensure that your site is accessible with the &lt;code&gt;www.&lt;/code&gt; in front of it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S3yG8YvT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmpo0bvqfwhxh6df5sld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S3yG8YvT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmpo0bvqfwhxh6df5sld.png" alt="Adding domains" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next up, you'll need to click &lt;em&gt;Request Certificate&lt;/em&gt; which should take to you to the AWS Certificate Manager. &lt;/li&gt;
&lt;li&gt;Select &lt;em&gt;Request a public certificate&lt;/em&gt; and click Next. &lt;/li&gt;
&lt;li&gt;For fully qualified domain name(without any https), enter your domain as well as a version with www, eg. &lt;code&gt;mynewdomain.com&lt;/code&gt; and &lt;code&gt;www.mynewdomain.com&lt;/code&gt; so your site is accessible by either name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Orda6FST--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n3l10j5tfg9mewii2ho.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Orda6FST--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n3l10j5tfg9mewii2ho.png" alt="Image description" width="800" height="750"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the recommended &lt;em&gt;DNS validation&lt;/em&gt; and leave the key algorithm as is.&lt;/li&gt;
&lt;li&gt;Request the certificate!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Open up your new certificate and you'll notice that the Status says &lt;em&gt;Pending Validation&lt;/em&gt;. Here's where you'll need to head over to your Domain.com account to validate your ownership. Keep the Certificate open because you're going to need the &lt;strong&gt;CNAME name&lt;/strong&gt; and the &lt;strong&gt;CNAME value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In your Domain.com account, select your domain and head over to &lt;em&gt;DNS &amp;amp; Nameservers &amp;gt; DNS Records&lt;/em&gt;. Here you are going to click + to add a new DNS record.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GdCdW1RM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhpn6dps3j414pv9y5hm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GdCdW1RM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhpn6dps3j414pv9y5hm.png" alt="Certificate Manager" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Copy the &lt;strong&gt;CNAME name&lt;/strong&gt; and paste it into the &lt;em&gt;Name&lt;/em&gt; field. The form should automatically truncate it to the hexadecimal string starting with an underscore, eg &lt;code&gt;40c74073be244c16356efbfd43a6461e&lt;/code&gt;.
&lt;em&gt;At the time of writing, there was some weird behaviour with the form in that I would have to hit spacebar and then backspace for proper input validation. Hopefully this gets sorted out.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Copy the &lt;strong&gt;CNAME value&lt;/strong&gt; and paste it into the &lt;em&gt;Content&lt;/em&gt; field.&lt;/li&gt;
&lt;li&gt;Set the &lt;em&gt;Type&lt;/em&gt; to &lt;strong&gt;CNAME&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Set the &lt;em&gt;TTL&lt;/em&gt; to 1/2 Hour (or any value really). &lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Add DNS&lt;/em&gt; and repeat for your second domain startng with &lt;code&gt;www&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q-xOM17_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfi4r5cw4w1b9bx4ab9f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q-xOM17_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfi4r5cw4w1b9bx4ab9f.png" alt="Domain.com config" width="800" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now we wait while AWS and Domain.com validate your ownership. This can take a few minutes to hours, but once it's successful, the Status should update to &lt;strong&gt;Issued&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U4fakctL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgn5oes2afzrfrzuynqg.png" alt="Certificate Issued" width="800" height="524"&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Almost There!!
&lt;/h3&gt;

&lt;p&gt;Yeah, this is a bit of a process. We need to create two more DNS records in Domain.com. The first records were primarily to validate ownership. This time, we're actually going to direct the custom domain name to the CloudFront domain, and this will not work till the certificates are issued.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Head back over the CloudFront and grab the distribution Domain.&lt;/li&gt;
&lt;li&gt;Get back to Domain.com to Add a DNS record&lt;/li&gt;
&lt;li&gt;This time give it the name &lt;code&gt;@&lt;/code&gt; and set the content to your distribution domain without the https, eg &lt;code&gt;d2r7ncgogp998k.cloudfront.net&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set the type to *&lt;em&gt;CNAME&lt;/em&gt; and once more a TTL to 1/2 hour.&lt;/li&gt;
&lt;li&gt;Do this once more using the same content, but instead enter the name as &lt;code&gt;www&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q08OGAQK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wurgtrdj89x2rnacb87l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q08OGAQK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wurgtrdj89x2rnacb87l.png" alt="Domain.com config" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give it some time (minutes to hours) and with that, your static site should be available via your custom domain.&lt;/p&gt;

&lt;p&gt;For your hard work, give yourself a pat on the back, and take a break from the screen!!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloudfront</category>
      <category>staticsite</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
