Web hosting on Amazon

In this post we will show you how you can host your static web page or web application on AWS (Amazon Web Services) with S3 (Scalable Srorage in Cloud). You can obtain simple maintenable and cheap web hosting.

Login to AWS web console click Services and type to search field S3 and then select S3 Scalable Storage in Cloud. This brings you to your S3 dashboard.

S3 creates for each project so called bucket which is a place for your files. So let’s click Create bucket button.

In Create bucket window specify name and region and click Next.

So far skip all other pages by clicking Next and on the last page Review click Create bucket button.

Now you can see your new bucket in the list of all buckets. Simply click its name. This shows bucket detail page. Here you can upload files directly from your computer oven drag&drop or you can set properties and permissions of your bucket.

Click Properties button and then select Static website hosting card.

In next window you can configure the hosting. Select Use this bucket to host a website, then type index.html and click Save button. You can also notice a web address of your bucket, so called Endpoint. Please store it for later use.

Then back in bucket screen click Permissions and Edit button.

Then uncheck Block all public access item and click Save. You will also be asked to type confirmation of your chose.

Now the basic setup of your bucket is done. Go to Overview and upload your files by Upload button or by drag&drop your files from your computer. Please be aware that in the root of your bucket must be index.html file. This one will be loaded when anybody departs on your bucket web address.

Set bucket policy

Now in Permissions tab click Bucket Policy button and enter following policy:

 {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::YourBucket/*"
        }
    ]
}

Do not forget to replace “YourBucket” with the real name of your bucket. Then click Save button.

Load bucket files by script

You might an AWS Command Line Interfaces on your computer. If not then I warmly suggest to do it, because it can help a lot even with many other AWS tasks. The link is here: aws.amazon.com/cli/

Then you can upload a content of any folder of your computer very easilly. The command is:

aws s3 sync YourFolder s3://YourBucket --region us-west-2 --cache-control "no-cache, max-age=1"

Do not forget to replace “YourFolder” by your real folder including path and “YourBucket” by a name of your S3 bucket.

For loading from your aws-cli you should extend bucket policy this way:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:List*",
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::YourBucket",
                "arn:aws:s3:::YourBucket/*"
            ]
        }
    ]
}

Be careful with your Aws-cli profile. If you have more AWS profiles you can very easily switch between them via login to Aws portal. But your aws command line has to be always switched by adding profile switch to command this way:

aws s3 sync YourFolder s3://YourBucket --region us-west-2 --cache-control "no-cache, max-age=1" --profile YourNextProfile

This also means that you have to have more profiles in your local desktop Aws-cli configuration. How t add multiple profiles is described here: https://medium.com/@fullsour/how-to-switch-user-on-the-aws-cli-77c2b314e12d

Test your website

Anyway even manually or by Aws-cli you files should be displayed in your bucket.

Now open your internet browser and type public web address of your bucket. If you do not remember the address simply go to Properties, then click Static website hosting and copy url at Endpoint.

Happy browsing!