All about CDN and AWS Cloudfront with EC2 Instance as quickly as possible
Website performance, security and availability under heavy load is always a challenge for any web developer. We try hard to optimize our code, database queries and assets but it is never sure whether the website will work fine in production environment against a lot of requests. Load Balancer can help to improve availability but certainly we need more than just load balancer for better security, caching and asset optimization. Here comes our popular thing called CDN(Content Delivery Network). I am sure some of you might have used it already if not you might be aware about this name. Let’s try to wrap it as quickly as possible.
What is a CDN?
Content Delivery Network can be considered as a group of distributed servers spread through multiple geographic locations. This works between the main website server and user requesting for website and stores the static data like assets as cache and distribute to all CDN servers. So if some other user requesting the same page then it is delivered from nearest CDN server hence leads to faster load time. It not only works for website but can also be used to deliver media content.
Benefits of CDN:
There are following benefits of using CDN for your website or media content:
1. Cached content delivery so faster loads:
All the website static content and assets will be cached and distributed so the request will not go to main server for every request unless it has to load some dynamic information which improves speed drastically
2. Works as a secure wall before your server so adding additional security:
All requests goes through the CDN so it works as an additional layer of security which will filter any attack as most of the CDN servers uses some sort of firewall which restrict direct access to your main server
3. Improved availability due to content delivery from nearest CDN server:
Suppose your website is hosted on a server located in NewYork city and someone is trying to view it from India then the requests will channel through a lot of mesh of network and the load time will be higher than someone viewing in New York city itself. CDN will deliver the content from nearest CDN server in India so increasing speed and availability
4. Better performance under heavy load as CDN server can handle a lot of traffic:
CDN uses distributed servers and can absorb a lot of load compared to normal main server so improving uptime and performance under high usage and tons of requests.
5. Immunity from DDOS attack and healthy main server:
The major concern for any low configuration server is DDOS attack which can choke any normal server with average configuration. If we use CDN, its servers can absorb a lot of load, blacklist IP and can also report you about the attack
6. Deliver media efficiently and securely:
CDN can also be used to deliver heavy media efficiently like videos streaming. It also helps to reduce latency and better load time which lead to improved user experience.
Integrating AWS Cloudfront with your website hosted on EC2 Instance:
There are many CDN providers like Max CDN, cloudflare but in this article we will see how to setup cloudfront with EC2. I will assume DNS is setup through AWS Route 53 for simpler guideline. Here are the steps to configure:
1. Create a S3 bucket with public access to store the Cloud front logs
2. Go to EC2 and select region where the website is running
3. In EC2, create a load balancer and attach the instance to it
4. Make sure security group assigned to load balancer have incoming port open for 80 and 443
5. Copy the load balancer assigned domain and see if it is loading website from EC2 instance
6. Go to cloudfront in AWS console and create new distribution
3. Select the loadbalancer from the list in Origin Domain Name
4. Chose behavior of request methods, cache as per your requirements
5. Enter your original domain name in alias domain name field and let other fields default
6. Select default SSL certificate or import from ACM
7. To create certificate, you should create it in N. Virginia region only
8. Verify the domain name in Route 53 using newly created certificate CNAME
9. Once domain verified in certificate then chose this certificate in custom certificate and chose S3 bucket(Created initially) to log any errors
10. Confirm all details and create distribution and wait until status show deployed
11. Copy the Public name of cloudfront distribution and add it as alias to your domain name in Route 53 as IPV4/IPV6
12. Now hit your domain and all request will go through CDN and you will notice the speed improvements after few requests
Troubleshooting for the problems:
You might face some issue but trust me, it is easy to fix if you focus on the problem. First of make sure below checklist is correct:
1. EC2 instance website is running fine and can be accessible by IP or public DNS in the browser
2. Connected Load balancer DNS name loading the correct website from attached EC2 in the browser
3. DNS name of cloudfront loading the website in the browser
4. Alias IPV4/6 in Route 53 have correct domain name and Cloudfront distribution DNS Name
If all above checklist seems fine then checkout the S3 log bucket and fix the problem accordingly.
This article might not have detailed information but it provides a general and steps in short to at-least know how AWS Cloudfront can be configured rest you can checkout AWS official documentation and checkout other options which you left default during your first step. I hope it will help you some way.