Hosting a website in AWS - S3

From PheonixSolutions
Jump to navigation Jump to search

Hosting a website in AWS - S3

[edit]

Introduction

Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. It provides management features so that we can optimize, organize, and configure access to our data to meet our specific business, organizational, and compliance requirements

Prerequisites

  1. An AWS account

  2. Access and secret key of that AWS account

  3. A valid domain name

Implementation

Step 1: Sign in to the AWS account and choose S3 service

https://aws.amazon.com/

Step 2: Navigate buckets > create bucket

Step 3: Configure the bucket setting as mentioned

(i) Bucket name - Domain name

(ii) Choose the appropriate AWS region

(iii) Only tick the following under "Block Public Access settings for this bucket" section

  • Block public access to bucket and objects granted through new access control lists (ACLs)

  • Block public access to bucket and objects granted through any access control lists (ACLs)

(iv) Click on create bucket

Step 4: Click on Permission > Bucket Policy > edit option to edit the bucket policy.

Step 5: Add the below lines as mentioned and save the changes

Note: Replace example.com with the appropriate domain name

 {
 "Version": "2012-10-17",
 "Id": "Policy1548223592786",
 "Statement": [
 {
 "Sid": "Stmt1548223591553",
 "Effect": "Allow",
 "Principal": "*",
 "Action": "s3:GetObject",
 "Resource": "arn:aws:s3:::example.com/*"
 }
 ]
 }

Step 6: Click on Properties > Static website hosting > edit to enablethe option

Step 7: Configure the Static website hosting as mentioned below and click on save changes

(i) Enable option

(ii) Hosting type - Host a static website

(iii) Index document - index.html

(iii) Error document - index.html

Step 8: Remove A record for the domain (example.com) from DNS and add the CNAME record to point the domain to S3 bucket as mentioned

CNAME value: example.com.s3.<aws-region>.amazonaws.com

Step 9: To upload files in S3 bucket we can use two methods

(i) Using Upload option and add the files

(ii) AWS CLI option using Access key and Secret key

 aws configure
 <provide access key and secret key to configure S3 bucket in the remote server>
 aws s3 cp example.txt s3://bucketname/path/to/destination/

(iii) Java script using Access key and Secret key

  • Create a script as mentioned in test.js

  • Run the script as node test.js

 const AWS = require('aws-sdk');
 const fs = require('fs');
 // Configure AWS credentials
 AWS.config.update({
 accessKeyId: 'YOUR_ACCESS_KEY',
 secretAccessKey: 'YOUR_SECRET_KEY',
 });
 // Create an instance of the S3 service
 const s3 = new AWS.S3();
 // Define the bucket name and file details
 const bucketName = 'YOUR_BUCKET_NAME';
 const filePath = 'path/to/example.txt';
 const fileKey = 'example.txt'
 // Read the file from local disk
 const fileData = fs.readFileSync(filePath);
 // Set the parameters for S3 upload
 const uploadParams = {
 Bucket: bucketName,
 Key: fileKey,
 Body: fileData,
 };
 // Upload the file to S3
 s3.upload(uploadParams, (err, data) => {
 if (err) {
 console.error('Error uploading file:', err);
 } else {
 console.log('File uploaded successfully:', data.Location);
 }
 });

Note:

  • Replace YOUR_ACCESS_KEY, YOUR_SECRET_KEY, YOUR_BUCKET_NAME, path/to/example.txt, example.txt with the appropriate values

  • Ensure node, npm & "npm install aws-sdk" should be installed

Step 10: Access the domain in the browser to ensure the functionality

http://example.com/example.txt