aws

Hosting your static website in S3 by leveraging cloudfront

gathungu

Muchiri

Posted on January 27, 2024

Hosting your static website in S3 by leveraging cloudfront

S3 as a service can be linked from on-premise and other cloud services like Amazon Ec2 a computing services that allows to 'rent' servers in AWS. We will be hosting a static website in s3, this does not require compute

Before we setup the environment we need to understand that when adapting to cloud we should have a secure first approach.

We will leverage another AWS service know a Cloud Front. It is a web service that enhances and distributes your static and dynamic web content faster. It leverages its self from a worldwide network of data centers. It delivers the content on http and https adding
a secure and encrypted against a normal protecting against common cyber attacks.

From the s3 bucket we had created, upload your website by selecting all files and folders.

After a successful upload your console should provide a green success message at the top

Image description
in the buckets permission tab click on edit, disable the block public access

Image description

Deactivate the feature, this is temporary

Image description
Type confirm

Image description

navigate down to bucket policy and click edit

image...

paste this policy

`"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "PublicReadGetObject",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::bucket-name/*"
    }
]
Enter fullscreen mode Exit fullscreen mode

}`
Replace the bucket name with your bucket name, it should like follows

Image description

navigate down and save changes.

Click on properties in the bucket profile settings and scroll down to static website hosting

Image description

Image description

click on the edit button, navigate and enable static web hosting. in the index document add your index.html as the index document, for the error handling file type "error/index.html.

To retrieve the endpoint for your bucket, click the Properties tab, scroll to the bottom, and click the copy icon next to the Bucket website endpoint, copy it to a new tab and it will load your index.html page.

Scroll down and save changes.

Navigate to the search bar and search for CloudFront

Image description

create CloudFront a distribution

Image description
Do not click at the web endpoint and scroll down to origin access use the origin access control settings, this allows s3 to allow cloudfront to access it.

click on create control settings

Image description

Name it to mirror your s3 bucket-name you can use your abbreviation of CF to show its cloud-front then click on create.

Image description

scroll down to the Web Application Firewall do not enable protection

Image description

Default root object type index.html, leave the other default settings as they are, create the distribution.

Image description

There will be a splash screen suggesting you to update the policy in your s3 copy policy and navigate to s3 permissions and click on edit clear the previous policy you had pasted before and pste the new one, navigate below and save changes.

Image description

navigate to the public access settings, click on edit, activate the settings, click on save. Type confirm and click on confirm

Image description

Image description
Navigate back to CloudFront page and copy the distribution name and paste it to a new tab. your website will be all set up

Image description

Have a try, give me a feed back on how the exercise was

💖 💪 🙅 🚩
gathungu
Muchiri

Posted on January 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related