Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudfront and EC2

How do you setup Cloudfront in front of an EC2 instance? I'm interested in having users hit the Cloudfront url rather than the EC2 origin.

So instead of hitting ec2-52-64-xxx-xxx.ap-southeast-2.compute.amazonaws.com users would hit d111111abcdef8.cloudfront.net.

My intention is to save money on hosting by reducing the traffic and CPU load on the EC2 instance, while providing overseas users with faster load times.

Would I just point my DNS to the Cloudfront url instead of the EC2 origin?

like image 890
ChrisRich Avatar asked Oct 15 '15 00:10

ChrisRich


People also ask

Can we use CloudFront with EC2?

We also show you how you can use CloudFront, Application Load Balancer (ALB) and EC2 instances behind it to speed access to your dynamic and static content. If playback doesn't begin shortly, try restarting your device.

What does CloudFront do in AWS?

CloudFront speeds up the distribution of your content by routing each user request through the AWS backbone network to the edge location that can best serve your content. Typically, this is a CloudFront edge server that provides the fastest delivery to the viewer.

How does CloudFront work with elastic load balancer?

For a web application or other content that's served by an Application Load Balancer in Elastic Load Balancing, CloudFront can cache objects and serve them directly to users (viewers), reducing the load on your Application Load Balancer.


3 Answers

Would I just point my DNS to the Cloudfront url instead of the EC2 origin?

Pretty much.

Technically, you don't point DNS to a URL, you point it to a hostname or to an IP address, which is done a little differently with CloudFront.

First, though... in the CloudFront distribution settings, you need to configure any hostnames that CloudFront should expect to see sent by the browser, in the "alternate domain names" box.

For the origin, enter the ec2-...-compute.amazonaws.com hostname.

At this point, after 10-15 minutes, visiting the CloudFront-assigned URL should take you to your server.

Next... if your DNS is hosted in Route 53, then you'd go to the hosted zone and create new "A" records for those hostnames, setting "Alias" to "Yes," which should cause the targets box to be populated with the CloudFront distribution, which you would then select, and save the record.

If your DNS isn't hosted by Route 53, you would instead create CNAME records in DNS, pointing to the hostname assigned to the CloudFront distribution, e.g. jozxyqkexample.cloudfront.net. The catch, here, is that you can't do this with the apex/root of your domain, because a CNAME isn't valid there... so www.example.com would work, but example.com will not. This is a limitation of DNS, and the only workaround is to use Route 53 for your DNS, because it has internal integration with CloudFront (hence the "Alias" records, mentioned above, which use internal lookups rather than external referrals, like a CNAME).

You may also want to configure CloudFront to forward the Host header back to the origin server; otherwise when the request arrives, the hostname presented to your server in the HTTP requests will not be your domain name, and instead will be the hostname you configured as the origin host.

Be sure, when you connect through CloudFront, that the server doesn't redirect you back to the EC2 hostname or IP (the address bar in the browser will change, if it does, and you'll want to fix your web server's config if that happens).

like image 173
Michael - sqlbot Avatar answered Oct 19 '22 03:10

Michael - sqlbot


Use the below step to route the ec2-52-64-xxx-xxx.ap-southeast-2.compute.amazonaws.com to d111111abcdef8.cloudfront.net.

Follow below steps:

  1. Login into your aws console and search CloudFront

  2. Go to CloudFront: Create Distribution(please select Web distribution)

  3. Under Origin Settings : Select the Origin Domain Name (you can assign the Public DNS of your instance, e.g. ec2-52-64-xxx-xxx.ap-southeast-2.compute.amazonaws.com)
  4. Under Default Cache Behavior Settings : Select Viewer Protocol Policy: Redirect HTTP to HTTPS
  5. Under Object Caching: opt customize (Remain default setting in Minimum TTL , Maximum TTL and Default TTL)
  6. Forward Cookies : All
  7. Query String Forwarding and Caching : All
  8. Distribution Settings : Select Price Class: (Use All Edge Locations (Best Performance)).
  9. Alternate Domain Names (CNAMEs)s: add your domain URL name if there is any
  10. SSL Certificate : Under SSL you can opt default or your custom certificate that depends upon your requirement.(Opt default.)

Note : For Custom SSL Certificate (certificate should be in in us-east-1 region).

  1. It can took 10 to 15 minutes
like image 20
Anoop Kumar Avatar answered Oct 19 '22 03:10

Anoop Kumar


IP in Origin would give error: com.amazonaws.services.cloudfront.model.InvalidArgumentException: The parameter origin name cannot be an IP address. (Service: AmazonCloudFront; Status Code: 400; Error Code: InvalidArgument;

Even though AWS Documentation says you can use IP.

Public DNS (IPv4) as Origin gives '504 error The request could not be satisfied'
I've had that when Origin was S3 and if S3 has limited permissions. But EC2 has no permissions control. it is public.

like image 3
Manly Electronics Avatar answered Oct 19 '22 04:10

Manly Electronics