STATIC WEBSITE WITH AWS S3 AND CLOUD FRONT CDN

                                 STATIC WEBSITE WITH AWS S3 AND CDN


As the title says let us see how to setup static site with S3 and CDN for high delivery performance.

S3:

First login to your AWS console and navigate to S3 console,

  • Create a S3 bucket in the same name as your website ex: cdn.example.com
  • In the permission tab edit public access and make the bucket as public as below,
  • Update the bucket policy as below which makes the bucket public,

{
    "Version": "2008-10-17",
    "Id": "PolicyForPublicWebsiteContent",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::cdn.example.com/*"
        }
    ]
}


  • Update the ACl as below,

You can ignore the S3 log delivery group if you don,t want S3 to log for this bucket on a separate bucket.

  • Since we are setting up a site with CDN we may have any CORS contents need to be delivered so update the CORS policy as below as your needs otherwise you may face issues,

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "GET"
        ],
        "AllowedOrigins": [
            "https://sandbox.example.com"
        ],
        "ExposeHeaders": []
    }
]



  • Now it is time to build the site,
  • Just upload the static contents inside the folder
  • If you want the contents to be downloaded then you can change those contents metadata as "binary/octet-stream"
  • If you want the contents to be served in web page then leave it as it is as when it was uploaded, the metadata will be assigned by S3 based on the file type.

Now into CDN:

  • Switch to Cloudfront console in AWS
  • Select create distribution
    • Give origin domain path as your S3 bucket created
    • Select protocol option like http/https or redirection
    • Then select the cache policy based on your need of how long should CDN cache your content in their system for this create a cache policy based on your need and select it
    • Then create and select CORS policy as below,


  • In the CNAME give your site name as cdn.example.com so that you can access with this name instead of CDN provided domain name
  • For SSL certificate select the certificate you have imported to ACM. If you dont have any certificate in ACM then you need to import or create a certificate in ACM for this.
  • We are done select create distribution
  • The last thing is to create a sub-domain for cdn.example.com in your DNS provider and point it to your CDN domain name you just created.
  • Now you have a static website hosted in S3 which has a high content delivery with Cloudfront CDN.

Comments

Popular posts from this blog

Azure AD login for linux and windows(on premise)

Ubuntu 18.04 no audio error

How to fix Permission denied (publickey). fatal: Could not read from remote repository