Showing posts with label CloudFront. Show all posts
Showing posts with label CloudFront. Show all posts

Super Fast Dynamic Websites with CloudFront, ReactJS, and NodeJS - Part 1


CloudFront should be an essential component of any web based application deployment. It not only instantly provides super low-latency performance, it also dramatically reduces server costs while providing maximum server uptime.

Creating low latency static websites with CloudFront is a relatively simple process. You simply upload your site to S3 and create a CloudFront distribution for it. This is great for HTML5 websites and static blogs such as Jeckyl. But what about dynamic sites that need real time information presented to the end user? A different strategy is clearly required. Much has been published about different methods of caching dynamic websites but, I will present the most common sense and reliable technique to achieve this end.

Server Side v Browser Side Rendering

If you read any book on NodeJS you will no doubt find plenty of examples of rendering Jade templates with Express on the server. If you are still doing this, then you are wasting valuable server resources. A far better option is to render on the browser side. There are a number of frameworks specifically for this, the most popular being Facebook's ReactJS and Google's AngularJS (see State of JS report). I personally use ReactJS and the example will be in ReactJS, but either is fine.

Creating your site using ReactJS or AngularJS and uploading it to your NodeJS public directory will shift the rendering of your site from your server to the client's browser. Users of your app will no longer be waiting for rendered pages and will see pages appear with the click of a button.

You can now create a CloudFront distribution for your ReactJS or AngularJS site.

Although pages may be rendered instantly in the browser, any dynamic data required for the pages will be cached by CloudFront. We most probably do not want our dynamic data cached. We will still need a solution for delivering this data to the browser.


Handling Dynamic Data


Although there are many elaborate techniques published for handling dynamic data with CloudFront, the best way is to deliver this data without caching at all from CloudFront.

Not all HTTP methods are cached by CloudFront, only responses to GET and HEAD requests (although you can also configure CloudFront to cache responses to OPTIONS requests). If we use a different HTTP method, such as POST, PUT or DELETE  the request will not be cached by Cloudfront. CloudFront will simply proxy these requests back to our server.

Our EC2 NodeJS server can now be used to respond to requests for dynamic data by creating an API for our application that responds to POST requests from the client browser.




Some of you might be wondering why I haven't used serverless technology such as AWS Lambda or API Gateway. Rest assured I will be posting another series using this but, I consider EC2 as the preferred technology for most applications. First of all, costs are rarely mentioned in the serverless discussion. If you have an application that has significant traffic, the conventional EC2/ELB architecture will be the most cost effective. Secondly, many modern web applications are utilising websocket connections. Connections like this are possible with EC2 directly and also behind an ELB when utilizing proxy protocol. This is not possible with serverless technology as connections are short lived.

In the next post in this series we will set up our NodeJS server on EC2, create a CloudFront distribution and, create our API for handling dynamic data.

Be sure to subscribe to the blog so that you can get the latest updates.

For more AWS training and tutorials check out backspace.academy

New Video lab Low Latency HTML 5 Websites using S3, CloudFront and Route 53 - Part 2

We have added a new lab video to the Amazon CloudFront subject of the AWS Certified Solutions Architect Associate course.

Low Latency HTML 5 Websites using S3, CloudFront and Route 53 - Part 2
  • Types of origin servers
  • Types of CloudFront Distribution strategy
  • Creating a CloudFront distribution
  • Invalidating a CloudFront distribution

New Highly Available and Fault Tolerant VPC Architecture Tutorial

New Highly Available and Fault Tolerant VPC Architecture Tutorial Utilising ELB, ENI, Auto Scaling and the new S3 Endpoints and Multi-AZ Aurora Clusters We have just added a new tutorial to the AWS Certified Solutions Architect Associate Course that applies what you have learned in the lessons and quizzes.
These videos cover the essential hands knowledge in AWS architecture required for certification. Look for them in the AWS Architecture Design Patterns subject.




Amazon CloudFront Signed HTTP Cookies

Previously restricting access to CloudFront involved using signed URLs. Now another option is available through the use of signed HTTP cookies.This has the advantage of allowing restricted access to multiple objects or to a whole site. An example process for transcoded media files in an S3 bucket is as follows:


If you are sitting an AWS make sure you remember that there are now two ways of securing access to CloudFront objects; signed URLs and signed HTTP cookies.
More information at: Serving Private Content through CloudFront