Securely Host a Static Website with Amazon S3 and CloudFront
In today's digital landscape, having a fast and secure website is paramount. This tutorial will guide you through the process of securely hosting a static website using Amazon S3 for storage and Amazon CloudFront for content delivery. By leveraging CloudFront, you can ensure secure and accelerated access to your website without making the S3 objects public.
Step 1: Create an S3 Bucket
Instructions:
Log in to the AWS Management Console.
Navigate to the S3 service.
Click the "Create bucket" button.
Provide a unique and meaningful name for your bucket, and leave all other settings as default.
Click "Create bucket."
Explanation:
Creating an S3 bucket is the first step to host your static website files. Ensure the bucket name is unique and identifiable.
Step 2: Upload Your Files to S3
Instructions:
Select your newly created bucket.
Click the "Upload" button.
Choose the HTML file and any images you want to upload.
Click "Upload" to upload the files.
Explanation:
Upload your website's HTML and image files to the S3 bucket. These files will be stored securely and accessed through CloudFront.
Step 3: Verify Access Denied
Instructions:
Try to access the HTML file directly from the S3 object URL.
You should receive an access denied error since the objects are not public.
Explanation:
This step confirms that the S3 bucket is private, ensuring that your files are not publicly accessible, enhancing security.
Step 4: Create a CloudFront Distribution
Instructions:
Navigate to the CloudFront service in the AWS Management Console.
Click the "Create Distribution" button.
In "Origin Domain," choose the S3 bucket created in Step 1.
In "Origin Access," select "Origin access control settings," and create a control setting.
In WAF, choose "Do not enable security protections."
In "Default Root Object," enter
index.html
.Click "Create Distribution." This process may take some time.
Explanation:
CloudFront serves as a content delivery network (CDN) that distributes your content to various edge locations, improving access speed and security.
Step 5: Set Up Origin Access Control
Instructions:
While the CloudFront distribution is being created, click on 'Copy Policy'.
Go back to your S3 bucket in the AWS Management Console.
Navigate to the "Permissions" tab and click on "Bucket Policy."
Edit the bucket policy and paste the policy that you copied to grant CloudFront access to the S3 objects.
Save the bucket policy.
Explanation:
This step ensures that CloudFront has the necessary permissions to access your S3 bucket securely, without exposing your S3 objects to the public.
Step 6: Access Your Website Using CloudFront
Instructions:
Once the CloudFront distribution is created (check the status in the CloudFront console), copy the provided Distribution Domain Name.
Paste the Distribution Domain Name into a browser to access your website securely.
Explanation:
Access your website through the CloudFront URL. You'll notice faster loading times for images due to CloudFront's edge location caching.
Conclusion
Congratulations! You have successfully set up a secure and efficient static website using Amazon S3 and CloudFront. By utilizing CloudFront's edge locations, you enhance the loading speed of your website's images and other content, providing a better user experience. This setup not only ensures security but also leverages AWS's global infrastructure to deliver your content swiftly and reliably.
Happy Learning!
Follow me on LinkedIn.
Subscribe to my newsletter
Read articles from Sandhya Deotare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by