<?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: Jamshidbek Makhmudov</title>
    <description>The latest articles on DEV Community by Jamshidbek Makhmudov (@jamshidbekmakhmudov).</description>
    <link>https://dev.to/jamshidbekmakhmudov</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%2F1463162%2F58ce2d93-9bf8-4635-b867-39d3d939f96c.jpeg</url>
      <title>DEV Community: Jamshidbek Makhmudov</title>
      <link>https://dev.to/jamshidbekmakhmudov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamshidbekmakhmudov"/>
    <language>en</language>
    <item>
      <title>AWS CLI | Setup an S3 Static Website</title>
      <dc:creator>Jamshidbek Makhmudov</dc:creator>
      <pubDate>Thu, 02 May 2024 07:52:39 +0000</pubDate>
      <link>https://dev.to/jamshidbekmakhmudov/aws-cli-setup-an-s3-static-website-22h5</link>
      <guid>https://dev.to/jamshidbekmakhmudov/aws-cli-setup-an-s3-static-website-22h5</guid>
      <description>&lt;p&gt;&lt;strong&gt;In this article, we will take a look at how to setup an S3 bucket to host a static website using the aws cli. Skip to the bottom of the article if you just want the script.&lt;/strong&gt;&lt;br&gt;
AWS CLI&lt;br&gt;
The AWS Command Line Interface (CLI) lets us manage all of our AWS services from the command line, without having to use the web console. So instead of clicking a bunch of buttons to create a new EC2 instance, you could just run a command like this:&lt;br&gt;
aws ec2 run-instances  --region $region --image-id "ami-0d4504aaac331dc68" --count 1 --instance-type t2.micro --associate-public-ip-address&lt;br&gt;
Setup&lt;br&gt;
If you haven’t setup the AWS CLI already, you can do so using this link: &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html"&gt;https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html&lt;/a&gt;&lt;br&gt;
The reference for all of the commands used here is available at&lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/cli/latest/reference/"&gt;https://docs.aws.amazon.com/cli/latest/reference/&lt;/a&gt;&lt;br&gt;
S3 Static Website&lt;br&gt;
The steps for hosting a static website using s3 are pretty much the following:&lt;br&gt;
Create a new bucket with a unique name&lt;br&gt;
Enable public access to the bucket&lt;br&gt;
Update the bucket policy for public read access:&lt;br&gt;
{&lt;br&gt;
  "Version": "2012-10-17",&lt;br&gt;
  "Statement": [&lt;br&gt;
      {&lt;br&gt;
          "Sid": "PublicReadGetObject",&lt;br&gt;
          "Effect": "Allow",&lt;br&gt;
          "Principal": "&lt;em&gt;",&lt;br&gt;
          "Action": "s3:GetObject",&lt;br&gt;
          "Resource": "arn:aws:s3:::acit-3640-fall-2020-40126/&lt;/em&gt;"&lt;br&gt;
      }&lt;br&gt;
  ]&lt;br&gt;
}&lt;br&gt;
Enable the s3 bucket to host an index and error html page&lt;br&gt;
Upload your website&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new bucket with a unique name
aws s3 mb "s3://your-bucket-name"
aws s3 mb will create a new bucket. Make sure you change your-bucket-name to something better.&lt;/li&gt;
&lt;li&gt;Enable public access to the bucket
aws s3api put-public-access-block \
--bucket your-bucket-name \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"
aws s3api put-public-access-block allows you to configure the public access to the bucket. We’re setting all of the blocks to false to enable public access.&lt;/li&gt;
&lt;li&gt;Update the bucket policy for public read access:
aws s3api put-bucket-policy --bucket your-bucket-name --policy "{
\"Version\": \"2012-10-17\",
\"Statement\": [
  {
      \"Sid\": \"PublicReadGetObject\",
      \"Effect\": \"Allow\",
      \"Principal\": \"&lt;em&gt;\",
      \"Action\": \"s3:GetObject\",
      \"Resource\": \"arn:aws:s3:::your-bucket-name/&lt;/em&gt;\"
  }
]
}"
aws s3api put-bucket-policy allows us to specify a bucket policy which has to be written in JSON. This policy will allow anyone to get the objects ot of the bucket.&lt;/li&gt;
&lt;li&gt;Enable the s3 bucket to host an index and error html page
aws s3 website "s3://your-bucket-name" --index-document index.html --error-document index.html
aws s3 website configures the bucket as a website. We have to include an index and an error page. We could specify a single page for both of these. This is usually what we want for a single page application.&lt;/li&gt;
&lt;li&gt;Upload your static website
aws s3 sync directory-path "s3://your-bucket-name/"
aws s3 sync will update the buckets contents with that of the contents of the local directory.
If we want to just copy a single file, we can use aws s3 cp
# Copy a file to an s3 bucket
aws s3 cp path-to-file "s3://your-bucket-name/filename"
# Copy a file from an s3 bucket
aws s3 cp "s3://your-bucket-name/filename" path-to-file
s3 vs s3api
s3api gives you complete control of S3 buckets. s3 gives you a higher level of abstraction for some of the more common operations you want to perform on an S3 bucket.
Single Script
As a single bash script, this code would look like this. There are a few more variables to make the region and profile easier to configure.
#!/bin/bash
bucket_name='your-bucket-name'
website_directory='/path/to/website/'
region='us-east-1'
profile='default'
# 1. Create a new bucket with a unique name
aws s3 mb \
--profile $profile \
--region $region \
--region us-east-1 "s3://$bucket_name" 
# 2. Enable public access to the bucket
aws s3api put-public-access-block \
--profile $profile \
--region $region \
--bucket $bucket_name \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"
# 3. Update the bucket policy for public read access:
aws s3api put-bucket-policy \
--profile $profile \
--region $region \
--bucket $bucket_name \
--policy "{
\"Version\": \"2012-10-17\",
\"Statement\": [
  {
      \"Sid\": \"PublicReadGetObject\",
      \"Effect\": \"Allow\",
      \"Principal\": \"&lt;em&gt;\",
      \"Action\": \"s3:GetObject\",
      \"Resource\": \"arn:aws:s3:::$bucket_name/&lt;/em&gt;\"
  }
]
}"
# 4. Enable the s3 bucket to host an &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;error&lt;/code&gt; html page
aws s3 website "s3://$bucket_name" \
--profile $profile \
--region $region \
--index-document index.html \
--error-document index.html
# # 5. Upload you website
aws s3 sync \
--profile $profile \
--region $region \
$website_directory "s3://$bucket_name/"
Once the bucket is created, you only need to run the sync code to push new updates:
#!/bin/bash
bucket_name='your-bucket-name'
website_directory='/path/to/website/'
region='us-east-1'
profile='default'
aws s3 sync \
--profile $profile \
--region $region \
$website_directory "s3://$bucket_name/"
And if you ever want to completely destroy the bucket:
#!/bin/bash
bucket_name='your-bucket-name'
website_directory='/path/to/website/'
region='us-east-1'
profile='default'
aws s3 rm \
--profile $profile \
--region $region \
--recursive s3://$bucket_name&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;aws s3api delete-bucket \&lt;br&gt;
  --profile $profile \&lt;br&gt;
  --region $region \&lt;br&gt;
  --bucket $bucket_name&lt;/p&gt;

</description>
      <category>aws</category>
      <category>s3</category>
    </item>
  </channel>
</rss>
