{"id":121,"date":"2024-02-15T13:38:57","date_gmt":"2024-02-15T12:38:57","guid":{"rendered":"https:\/\/virtutimilitari.click\/?p=121"},"modified":"2024-02-15T14:02:01","modified_gmt":"2024-02-15T13:02:01","slug":"the-correct-way-of-hosting-a-static-website-with-aws-s3-bucket-and-cloudfront-in-4-easy-steps","status":"publish","type":"post","link":"https:\/\/virtutimilitari.com\/index.php\/2024\/02\/15\/the-correct-way-of-hosting-a-static-website-with-aws-s3-bucket-and-cloudfront-in-4-easy-steps\/","title":{"rendered":"The correct way of hosting a static website with AWS S3 bucket and CloudFront in 4 easy steps"},"content":{"rendered":"<h1>The Problem<\/h1>\n<p>There&#8217;s a lot of articles and tutorials on the Internet on how to host a static website with AWS S3, but most of them include two very common problems:<\/p>\n<p><strong>Problem #1<\/strong>: Use AWS S3 static website hosting feature, but no Cloudfront. If you do that, you won&#8217;t have custom domain with SSL and therefore no https access to your website.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-124\" src=\"https:\/\/virtutimilitari.click\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap2.png\" alt=\"\" width=\"853\" height=\"351\" srcset=\"https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap2.png 853w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap2-300x123.png 300w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap2-768x316.png 768w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/p>\n<p><strong>Problem #2<\/strong>: Allow public access to the bucket (un-check &#8220;block all public access&#8221;). This is a bad practice because it may lead to security issues.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-123\" src=\"https:\/\/virtutimilitari.click\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap1.png\" alt=\"\" width=\"849\" height=\"564\" srcset=\"https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap1.png 849w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap1-300x199.png 300w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Webiste-Hosting-Trap1-768x510.png 768w\" sizes=\"auto, (max-width: 849px) 100vw, 849px\" \/><\/p>\n<h1>The solution.<\/h1>\n<p>In this article we&#8217;ll explain the right way of doing it. We&#8217;re assuming you&#8217;re already familiar with AWS Management Console and DNS management. L<span style=\"font-size: inherit;\">et&#8217;s get to it:<\/span><\/p>\n<p><strong>Step 1.<\/strong> Go to AWS Certificate Manager and issue a certificate for the domain you&#8217;d like to use for the website (e.g. mywebsite.com).<\/p>\n<ul>\n<li><strong>Important<\/strong>: make sure you&#8217;re in the context of us-east-1 (North Virginia).<\/li>\n<li>Select &#8220;Issue Public Certificate&#8221; and then choose &#8220;DNS validation&#8221; as validation method.<\/li>\n<li>Then pass the validation by adding correct entries in the DNS. Continue to Step 2 when the certificate Status is &#8220;Issued&#8221;<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-126\" src=\"https:\/\/virtutimilitari.click\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-Certs-1024x199.png\" alt=\"\" width=\"1024\" height=\"199\" srcset=\"https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-Certs-1024x199.png 1024w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-Certs-300x58.png 300w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-Certs-768x150.png 768w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-Certs-1536x299.png 1536w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-Certs.png 1541w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><strong>Step 2.<\/strong> Create an S3 bucket.<\/p>\n<ul>\n<li>Make sure &#8220;block all public access&#8221; is ON.<\/li>\n<li>Be sure to use &#8220;Server-side encryption with Amazon S3 managed keys (SSE-S3)&#8221;.<\/li>\n<li>After the bucket is created, do NOT enable &#8220;static website hosting&#8221; &#8211; this is NOT necessary at all, and in fact can only cause confusion.<\/li>\n<\/ul>\n<p><strong>Step 3.<\/strong> Create a CloudFront distribution<\/p>\n<ul>\n<li>Choose the bucket created in Step 2 as origin domain<\/li>\n<li>In the &#8220;origin access&#8221; select &#8220;Origin access control settings (recommended)&#8221;\n<ul>\n<li>Click &#8220;Create new OAC&#8221; button, in the pop-up window give it a name, and keep other settings at default.<\/li>\n<li>Select the newly created OAC<\/li>\n<\/ul>\n<\/li>\n<li><strong>Important<\/strong>: Scroll down to &#8220;Alternate domain name (CNAME)\u00a0<em>&#8211; optional&#8221;. <\/em>It is NOT optional, it is mandatory in our case. Enter the domain name you want to use for the app (the one you got the certificate for in the <strong>Step 1<\/strong>, e.g. mywebsite.com)<\/li>\n<li><strong>Important<\/strong>: Scroll down a bit more to &#8220;Custom SSL certificate\u00a0<em>&#8211; optional&#8221;\u00a0<\/em>and select the certificate created in <strong>Step 1<\/strong>. Again, this is NOT optional, it&#8217;s mandatory for the website to work with https.<\/li>\n<li><strong>Important<\/strong>: Scroll down to &#8220;Default root object\u00a0<i>&#8211; optional&#8221;.\u00a0<\/i>Enter index.html<\/li>\n<li>Pick other options per your own needs, or simply use defaults. Usually it&#8217;s best to redirect http to https and allow for the widest range of HTTP methods.<\/li>\n<\/ul>\n<p><strong>Step 4<\/strong>. Wait until the distribution is created (it can be in the &#8220;Deploying&#8230;&#8221; state for a few minutes). Then click on the Distribution, go to the Origins tab, select the Origin and click Edit. In the &#8220;Origin Access&#8221; section click on &#8220;Copy Policy&#8221; button, and leave without saving. Then go to S3 bucket created in <strong>Step 2<\/strong>, go to Permissions tab, scroll down to &#8220;Bucket Policy&#8221; section, and paste the policy JSON. Save.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-125\" src=\"https:\/\/virtutimilitari.click\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-AllowingCloudfront-To-Access-The-Bucket.png\" alt=\"\" width=\"865\" height=\"406\" srcset=\"https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-AllowingCloudfront-To-Access-The-Bucket.png 865w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-AllowingCloudfront-To-Access-The-Bucket-300x141.png 300w, https:\/\/virtutimilitari.com\/wp-content\/uploads\/2024\/02\/S3Bucket-Static-Website-Hosting-AllowingCloudfront-To-Access-The-Bucket-768x360.png 768w\" sizes=\"auto, (max-width: 865px) 100vw, 865px\" \/><\/p>\n<p>That&#8217;s all! You can now upload your website files to the S3 bucket (make sure index.html is in the bucket&#8217;s root directory), and it will be available under https and the domain you used.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Problem There&#8217;s a lot of articles and tutorials on the Internet on how to host a static website with AWS S3, but most of them include two very common problems: Problem #1: Use AWS S3 static website hosting feature, but no Cloudfront. If you do that, you won&#8217;t have custom domain with SSL and &#8230; <a title=\"The correct way of hosting a static website with AWS S3 bucket and CloudFront in 4 easy steps\" class=\"read-more\" href=\"https:\/\/virtutimilitari.com\/index.php\/2024\/02\/15\/the-correct-way-of-hosting-a-static-website-with-aws-s3-bucket-and-cloudfront-in-4-easy-steps\/\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-game-development"],"_links":{"self":[{"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":4,"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions\/129"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/media\/124"}],"wp:attachment":[{"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtutimilitari.com\/index.php\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}