
{"id":29,"date":"2025-05-30T17:08:48","date_gmt":"2025-05-30T16:08:48","guid":{"rendered":"http:\/\/blog.mkcloudai.com\/?p=29"},"modified":"2025-05-30T17:09:46","modified_gmt":"2025-05-30T16:09:46","slug":"how-to-host-a-static-website-on-amazon-s3-using-aws-cli","status":"publish","type":"post","link":"https:\/\/blog.mkcloudai.com\/?p=29","title":{"rendered":"How to Host a Static Website on Amazon S3 Using AWS CLI"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AWS CLI installed and configured with your AWS credentials and default region.<\/li>\n\n\n\n<li>Your static website files (e.g., <code>index.html<\/code>, <code>style.css<\/code>) ready locally.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create an S3 Bucket<\/h3>\n\n\n\n<p>Run this command to create a new bucket. Replace <code>my-static-site-bucket<\/code> with a unique bucket name:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>aws s3 mb s3:\/\/my-static-site-bucket<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Enable Static Website Hosting on the Bucket<\/h3>\n\n\n\n<p>Use this command to configure your bucket for static website hosting:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>aws s3 website s3:\/\/my-static-site-bucket\/ --index-document index.html --error-document error.html<br><\/code><\/pre>\n\n\n\n<p><em>(You can skip <code>--error-document<\/code> or replace with your actual error page)<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Upload Your Website Files to the Bucket<\/h3>\n\n\n\n<p>Upload all your local files (e.g., from a folder <code>website-files\/<\/code>) to the bucket:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>aws s3 sync .\/website-files\/ s3:\/\/my-static-site-bucket\/<br><\/code><\/pre>\n\n\n\n<p>This command uploads everything in the local <code>website-files<\/code> directory to your bucket.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Set Bucket Policy to Make Files Public<\/h3>\n\n\n\n<p>To allow everyone to read your website files, create a JSON file <code>policy.json<\/code> with this content (replace <code>my-static-site-bucket<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>{<br>  \"Version\": \"2012-10-17\",<br>  \"Statement\": [{<br>    \"Sid\": \"PublicReadGetObject\",<br>    \"Effect\": \"Allow\",<br>    \"Principal\": \"*\",<br>    \"Action\": \"s3:GetObject\",<br>    \"Resource\": \"arn:aws:s3:::my-static-site-bucket\/*\"<br>  }]<br>}<br><\/code><\/pre>\n\n\n\n<p>Apply the policy with this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>aws s3api put-bucket-policy --bucket my-static-site-bucket --policy file:\/\/policy.json<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Access Your Website<\/h3>\n\n\n\n<p>Your static site will be accessible at this URL (adjust region as needed):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>http:\/\/my-static-site-bucket.s3-website-us-east-1.amazonaws.com<br><\/code><\/pre>\n\n\n\n<p>You can check the exact website endpoint URL by running:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>aws s3api get-bucket-website --bucket my-static-site-bucket<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Bonus: Delete the Bucket and Contents (If Needed)<\/h3>\n\n\n\n<p>To remove all files and delete the bucket:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>aws s3 rm s3:\/\/my-static-site-bucket --recursive<br>aws s3 rb s3:\/\/my-static-site-bucket<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Step<\/th><th>AWS CLI Command<\/th><\/tr><\/thead><tbody><tr><td>Create Bucket<\/td><td><code>aws s3 mb s3:\/\/my-static-site-bucket<\/code><\/td><\/tr><tr><td>Enable Website Hosting<\/td><td><code>aws s3 website s3:\/\/my-static-site-bucket\/ --index-document index.html --error-document error.html<\/code><\/td><\/tr><tr><td>Upload Website Files<\/td><td><code>aws s3 sync .\/website-files\/ s3:\/\/my-static-site-bucket\/<\/code><\/td><\/tr><tr><td>Apply Public Read Policy<\/td><td><code>aws s3api put-bucket-policy --bucket my-static-site-bucket --policy file:\/\/policy.json<\/code><\/td><\/tr><tr><td>Get Website Configuration<\/td><td><code>aws s3api get-bucket-website --bucket my-static-site-bucket<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites: Step 1: Create an S3 Bucket Run this command to create a new bucket. Replace my-static-site-bucket with a unique [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[6],"tags":[11,13,14,12],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-s3","tag-amazon-s3","tag-aws-s3","tag-static-website","tag-storage"],"_links":{"self":[{"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":1,"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mkcloudai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}