Amazon cloudfront - s3 or ec2? - amazon-ec2

I have an application hosted on EC2 instance.
Now I want to fetch all the static content used in application from Cloudfront.
Read from a source that Cloudfront uses S3/EC2 or private servers to get the static files.
I can't come to solution what exactly to use? Can I use same EC2 instance for this purpose? Any better option for this implementation?

Amazon CloudFront sits "in front" of your application on Amazon EC2 and/or your content in Amazon S3. It caches content in 50+ locations when people access your application.
For example, let's say you had a web app running on an EC2 instance serving HTML pages, and also some pictures in S3.
You would create a CloudFront distribution and configure two origins: one for your web server and one for your S3 bucket. Behaviours can be configured to tell CloudFront when to use each origin -- for example, serve *.htm URLs from EC2 and *.jpg URLs from S3.
Your users would then access your application via the supplied CloudFront URL. Content will be cached (if appropriate) at one of the 50+ CloudFront locations around the world that is closest to each of your users, resulting in faster response times for your user.
You can also use your own domain name with CloudFront so that it has a more-friendly URL than the one supplied on the CloudFront distribution (which looks like d3i7tv8nzqzfbt.cloudfront.net).

simply Amazon s3 is used for cloud storage where as Amazon EC2(elastic cloud) is used to server web pages(hosting) same as like godaddy.com.

Related

how to serve node js api through AWS cloudfront?

I am trying to serve my node js API (deployed on AWS EC2 and attached with application load balancer) through cloudfront url, is it possible?
Here are the steps I followed so far -
Created S3 bucket to host static website hosting
Created cloudfront distribution and linked S3 bucket with it. I can access S3 bucket contents with default url generated from cloudfront
Created custom origin for node js instance
Created behavior "api/*" to access node js API through cloudfront.
But when I am trying to access API with following url -
http://d3m30a4naen9t2.cloudfront.net/api/getItems
it throws "not found", it's not 404, this response is from EC2 server however the specified route exists.
Can anyone help please?
I am using ELB. I deployed my code which is on node.js, and everything is working fine. I was facing a lots of problem in ELB but finally we came to stable stage. If you want to serve your APIs then first use SSL which has lesser protocols in other words, use less secure SSL otherwise, your API will not be able to hit from any other source. Simply deploy your code through git or directly from filezilla and run the command on both servers (primary and secondary) as pm2 start index.js/server.js or whatever your main express file is.
Suggestion: Please be careful while selecting security certificates, because on ELB, if you do not follow the correct implementation, you will definitely face the problem of "API not accessible" or "Remote server is unable to connect".

Can I store static files on nginx whith cloudbees?

As far as I know, cloudbees is using nginx servers for routing the requests.
I am wondering if there is a way to drop my static files on nginx to make it serves my static files and save some load on my application server.
M.
On runtime the file system is "ephermeral". You have more info about it here.
You should use an external service like Amazon S3. The Amazon S3 ClickStart explains how to use this service from CloudBees.
You can also use other services like Dropbox or Google Drive.

AWS: To redirect or to proxy files

Let me explain dilemma.
I use 3 services from Amazon: EC2, S3 and CloudFront.
EC2 receives an file as upload, stores it in the S3 bucket. Then CloudFront mirrors the S3 bucket. The only limit is to have user friendly URLs.
Which approach to deliver those files is better?
Client > CloudFront > EC2 > S3
Client does a HTTP request to CloudFront URL
Cloudfront forwards the request to EC2
EC2 translates user friendly URL to raw file URL
EC2 reads the file from S3
Client > EC2 ... redirect ... CloudFront > S3
Client does a HTTP request to EC2
EC2 translates user friendly URL to raw file URL
EC2 redirects to CloudFront, witch mirrors the S3
There are two dimensions for this: the speed and the cost.
I see facebook using the second approach when serving profile images
http://graph.facebook.com/platform/picture
You certainly don't want to force the file transfer between S3 and your client to go through your EC2 instance. That will add load to the EC2 instance, increase bandwidth usage, and undoubtedly slow the response to the user.
Your EC2 instance gives you the greatest control over URL format, so you'll want the client to make the initial request to it (to have a "user-friendly" URL). The EC2 instance can just send the client a redirect to the CloudFront or S3 URL that actually has the content, and the client will pull it directly from there without the further involvement of EC2.
I wouldn't bother with CloudFront unless you have an enormous number of requests, or network latency has to be as low as possible.
I personally would just give the files a nice name and a proper content type metatag in S3 when first doing the file upload and, as such, remove your EC2 instances from the download path altogether. You would just serve the clean-url to you client in your HTML content and that clean URL will point directly to you resources in Cloudfront/S3, which has the proper metatags to serve up the desired content-type header.

amazon S3 database options and using EC2 as a REST api from S3

I am hosting my website on S3.
On my local host I am using backboneJS above a PHP Rest API that uses mySQL as a database.
So i opened an EC2 to host my Rest API but then realized this means cross domain AJAX.
How can i use EC2 as a Rest API if my index.html sits on S3?
What are my other DB options for S3?
many thanks,
Your JavaScript is being executed on web pages served from S3, and it has to access a REST API from a server you run on EC2. Unless the web pages and server are in the same domain (say, example.com), this will be a cross-origin request, prohibited by browsers by default.
Solution 1: have your S3 pages and your EC2 server in the same domain. S3 allows static website hosting that makes your S3 objects (web pages) available at the address of your choice. Put them and your EC2 server at addresses on the same domain, and it can work.
Solution 2: have your REST API server allow cross-origin requests. Since you control this EC2 server you can modify it to tell web browsers to allow pages from other domains to make such requests to your server. This involves setting several headers on your responses, and usually requires your server to respond to HTTP OPTIONS requests properly, too. See the CORS specification to get started on that.
You also ask about other DB options. If you keep your own EC2 server to provide the REST API it can use pretty much any kind of database you like, either running on the same or other EC2 instances, or database-as-a-service offerings like AWS RDB or AWS DynamoDB. But if you want to connect to the database directly from your web pages' JavaScript you would have to use a service that provides an HTTP API directly and that supports CORS. RDB does not provide an HTTP API at all, and DynamoDB does not seem to support CORS at this time, so neither of them would work.

Static hosting on S3 can only be done by CNAME from subdomain?

I read this:
"I can't use this until I can serve my root domain without redirection to "www". Can Amazon designate an IP address (or set of IP addresses) for S3 that I can point my root A record to?"
Is it still true that I need to have a domain host just as a proxy to S3 and setup CNAME to point a subdomain to S3 bucket? And there is no better way?
There are no better ways, only most costly ways.
You can set up an EC2 image with a proxy, and allow the proxy to access S3 on your behalf, while accessing the remainder of your web site somewhere else. Since scalability is a concern of yours, you'll also want to use the automatic scaling tools for EC2 as access to your proxy grows.
Or, just re-architect your application to use the CNAME-based approach for all content located in S3.

Resources