Deploying a Static Website using Amazon S3 Bucket
Amazon Simple Storage Service (S3) is a highly scalable, secure, and durable object storage service that can be used to store and retrieve any type of data, including static website files like HTML, CSS, and JavaScript. In this tutorial, we’ll explore how to deploy a static website using S3 bucket.
1. Create an S3 Bucket
Before deploying a static website, we need to create an S3 bucket. To create an S3 bucket, follow these steps:
- Go to the AWS Management Console and navigate to the S3 service.
- Click the “Create bucket” button.
- Enter a unique name for your bucket. Note that the bucket name must be globally unique across all AWS accounts.
- Select the region where you want to create your bucket. It’s recommended to choose the region that’s closest to your target audience to reduce latency.
- Leave all other settings as default and click the “Create bucket” button.
2. Upload Website Files to the S3 Bucket
Once you have created the S3 bucket, it’s time to upload your website files to the bucket. To upload website files to the S3 bucket, follow these steps:
- Click on the newly created bucket to open it.
- Click the “Upload” button.
- Drag and drop your website files into the upload window or click the “Add files” button to select files from your computer.
- Once you have added all the files, click the “Upload” button to start uploading files to the bucket.
- Wait for all files to be uploaded.
3. Make the S3 Bucket Publicly Accessible
By default, objects stored in S3 buckets are private and can’t be accessed by the public. To make the S3 bucket publicly accessible, follow these steps:
- Select the files that you want to make public by clicking the checkbox next to the file name.
- Click the “Actions” button and select “Make public”.
- Click the “Confirm” button to make the selected files public.
- Repeat these steps for all the files that you want to make public.
4. Enable Static Website Hosting
To host a static website on an S3 bucket, we need to enable static website hosting. To enable static website hosting, follow these steps:
- Click on the bucket that you want to use for website hosting.
- Click the “Properties” tab and then click the “Static website hosting” card.
- Click the “Use this bucket to host a website” checkbox.
- Enter the name of the index document (e.g., index.html) and the error document (e.g., error.html).
- Click the “Save” button.
5. Configure Bucket Policy
To allow public access to the website hosted on the S3 bucket, we need to configure a bucket policy. A bucket policy is a JSON-based policy document that defines permissions for objects in a bucket. To configure the bucket policy, follow these steps:
- Click the “Permissions” tab and then click the “Bucket Policy” button.
- Paste the following policy document in the editor replacing
your-bucket-name
with your bucket name.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::your-bucket-name/*"
]
}
]
}
- Click the “Save” button to save the bucket policy.
6. Test the Website
Now that you have deployed your static website on the S3 bucket, it’s time to test it. To test the website, follow these steps:
- Copy the endpoint URL from the “Static website hosting” card on the “Properties” tab of your S3 bucket. The URL should look like this: `http://your-bucket-name.s3-website.your-region.amazonaws.com/`
- Open a web browser and paste the endpoint URL in the address bar.
- Press Enter to load the website.
- If the website loads successfully, congratulations! Your website has been successfully deployed on the S3 bucket.
Conclusion
Amazon S3 is a simple and cost-effective solution for deploying static websites. With S3, you can store and serve files, and it also provides a secure and scalable infrastructure. By following the steps outlined in this tutorial, you can easily deploy your static website on an S3 bucket.