Guy
Guy

Reputation: 13296

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,

Upvotes: 1

Views: 848

Answers (1)

Charles Engelke
Charles Engelke

Reputation: 5649

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.

Upvotes: 3

Related Questions