Optimise your frontend APP in S3 by connecting with AWS Cloudfront [part 5]
Tags: tutorial, webdev, aws
Published: 2024-03-02 02:13:20

This post continues from my last post: [Deploy app to AWS by using Serverless Framework [part 4]](https://dev.to/laurenceho/deploy-app-to-aws-by-using-serverless-framework-2gen)

Once you can access your photo album app from S3 bucket, the next step is connecting AWS CloudFront with S3 bucket.

## What's CloudFront?

[AWS CloudFront](https://aws.amazon.com/cloudfront/) is a global content delivery network (CDN) that makes it easy to deliver websites, videos, apps, and APIs securely and at high speeds with low latency. You can use CloudFront to reduce latency by delivering data through 400+ globally dispersed Points of Presence (PoPs) and improve security with traffic encryption, access controls, and resiliency against DDoS attacks.

## Create distribution
### Origin
In this step, you only need to choose the S3 bucket you use for serving your photo album app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk5h8hgl52ssitnhm1oc.jpeg)

### Default cache behaviour
You can leave everything as default but I'd recommend using **HTTPS Only** for the viewer protocol policy. After all, we want to enforce a secure connection on our web app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm3of42d9jgigno4iav1.jpeg)

### Web Application Firewall (WAF)

You can enable it or not, it's up to you. Once it's enabled, you will have an extra charge.

### Settings

Leave everything as default unless you want to set up your domain name. If you don't use an [Alternate domain name (CNAME)](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html) with CloudFront, then choose **Create Distribution** to complete the process. If you use a CNAME, then follow these additional steps before you create the distribution:

1. For Alternate Domain Names (CNAMEs), choose Add item, and then enter your alternate domain name.
2. For Custom SSL Certificate, choose the custom SSL certificate from the dropdown list that covers your CNAME to assign it to the distribution.
Note: For more information on installing a certificate, see [How do I configure my CloudFront distribution to use an SSL/TLS certificate?](https://repost.aws/knowledge-center/install-ssl-cloudfront)
3. Choose Create distribution.
Note: After you choose **Create distribution**, it might take 20 or more minutes for your distribution to deploy.

Be sure to update the DNS for your domain to a CNAME record that points to the CloudFront distribution's provided domain. You can find your distribution's domain name in the [CloudFront console](https://console.aws.amazon.com/cloudfront/).

In my demo, I don't use any customer domain name so I will leave these 2 input fields empty.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zljbvobr2bqc5q15q7c1.jpeg)

Once your distribution is deployed, you will need to go back to your S3 bucket permission setting and edit the bucket CORS setting:

```json
[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "https://{YOUR_CLOUDFRONT_ID}.cloudfront.net",
            "http://demo-quasar-photo-albums.s3-website-us-east-1.amazonaws.com",
            "http://localhost:9000"
        ],
        "ExposeHeaders": []
    }
]
```
After saving the bucket policy, you should be able to access your web app via the CDN endpoint.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zot7lhjldk1818u2o9g.jpeg)

Here is the [demo](https://d3lfuof5usiolc.cloudfront.net).