4

I am trying to make an ios application that uses Amazon Web Services as its backend. I have an MySQL database in RDS and want to query this database from the app to retrieve content for the app. After some research, it looks like I should approach this by adding my own API for querying the database to an EC2 instance. Then, rather than directly accessing the RDS instance from the app, I access the API on the EC2 instance which accesses the RDS instance for me. Firstly, is this the way to go? I'm open to other suggestions too.

Here's what I have done so far, after reading this post:

  1. Installed apache and php on my EC2 instance.
  2. Obtained an elastic IP address for my EC2 instance.
  3. Added a file (let's call it retrieve-data.php) to apache that uses my RDS instance endpoint, username, and password to query the database and return data.

From my ios application, I can then send an http request to elastic_ip_address_of_my_ec2_instance/retrieve-data.php to get the data from my RDS database in my app. Is this the way to go? Can this be improved upon in any way?

Community
  • 1
  • 1
kcstricks
  • 1,489
  • 3
  • 17
  • 32
  • possible duplicate of [How to connect to EC2 instance within an iOS App](http://stackoverflow.com/questions/25082921/how-to-connect-to-ec2-instance-within-an-ios-app) – Mark B Aug 15 '15 at 15:26
  • @ mbaird - My question is the same in essence, but I am looking for more detail on how to accomplish it. I would have posted a comment rather than asking a new question, but I don't have enough reputation for that and had been stuck for a while. In any case, I'm hoping this will open up a bit more detailed a discussion on how this can and should be done. – kcstricks Aug 15 '15 at 15:41
  • If you need more details about a specific aspect of the answer to that question, then you need to post a question asking about the specific part that you need help with. Duplicating a question and just saying "I need more details" is going to be frowned upon here. – Mark B Aug 15 '15 at 15:58
  • @mbaird thanks for the heads up. I have updated my question. – kcstricks Aug 15 '15 at 16:27

2 Answers2

3

Edit: changing this answer to whats in my comment below;

The solution is what is suggested in How to connect amazon RDS in iOS, which is installing a webserver on the EC2 instance and writing a web app to make your RDS calls for you. This will involve some kind of web programming, there's a simple explanation here in the top comment. Then you can use RestKit to implement the calls from your app to the EC2 instance: github.com/RestKit/RestKit

  • Thanks Nick. I've checked out the links and looked into Cognito, but cannot find out how to connect to EC2 or RDS using it. It seems very useful for connecting to other AWS services such as DynamoDB, but RDS is a better fit for my needs. Can I still use Cognito to access an EC2 or RDS instance? Perhaps I am missing something. Thanks! – kcstricks Aug 13 '15 at 19:48
  • Ah, due to RDS being an actual database endpoint you can access with database clients, it looks like RDS isn't accessible through Cognito unfortunately. Have a look at https://stackoverflow.com/questions/27331911/is-it-possible-to-connect-amazon-web-service-rds-with-amazon-web-service-cognito Due to this, I think you're right in your suggestion of either connecting via an API you implement in EC2, or completely moving to a different model of storing data (ie whatever cognito uses, if you wanted to go that way). – Nick Triantafillou Aug 15 '15 at 04:30
  • Thanks for clarifying this Nick! A relational database is the best fit for my app, so I'd like to connect through an API on EC2. Unfortunately, this sort of brings me back to my original question: do you know how I can connect to EC2 from my app without having to ssh in (as was advised against in the post linked in my question)? Thanks! – kcstricks Aug 15 '15 at 10:46
  • 1
    The solution is what is suggested in that answer, which is installing a webserver on the EC2 instance and writing a web app to make your RDS calls for you. This will involve some kind of web programming, there's a simple explanation here in the top comment: https://stackoverflow.com/questions/21624853/how-to-connect-amazon-rds-in-ios - then you can use RestKit to implement the calls from your app to the EC2 instance: https://github.com/RestKit/RestKit – Nick Triantafillou Aug 15 '15 at 11:00
  • Alright I'm finally making progress so that's good news. Can you elaborate a bit on "some kind of web programming"? To test that this works, I wrote up a simple php file called test.php with code to access and query my RDS database and uploaded this file to apache on my EC2 server. Then, I can open up a web browser and enter the following: public_ip_address_of_my_ec2_instance/test.php and the output of test.php is printed in my browser, so it is indeed working. Is this all you mean by "some kind of web programming"? I'm quite new to all this so please bear with me! – kcstricks Aug 15 '15 at 14:06
  • Building on the above, is it a good idea to use public_ip_address_of_my_ec2_instance/test.php from my ios app? At this point, that is the address I would use to make my http request. – kcstricks Aug 15 '15 at 14:12
  • What you're doing with php is exactly the web programming I was referring to, so that's definitely the right path! What I would do instead of using the public IP address is possibly register a domain and use that, possibly putting the server behind an elastic load balancer to handle load (if you require it). Then all you have to do is configure your domain as a CNAME to your load balancer endpoint. – Nick Triantafillou Aug 16 '15 at 03:39
  • Thanks Nick - I'm in the process of setting this up. It's not going to be completely free, but isn't expensive either. Nevertheless, could you explain why it's recommended to use a domain name rather than the IP, so I know if it's worth investing the money now or waiting until later? – kcstricks Aug 18 '15 at 02:58
  • The advantage is that you don't have to change any of your code later on if you're hardcoding the IP. If you use it in a lot of places, that could be a major benefit. If you're just using it in one or two places, then it's easy to change and you shouldn't worry. Also, if your EC2 instance is just using a public IP and not an elastic IP, the IP address will change if you reboot or stop/start the instance, so keep that in mind as well. It's just to make life easier for yourself. namecheap.com currently have the new .xyz TLD for $1.36/year if you want something super cheap! – Nick Triantafillou Aug 18 '15 at 03:30
  • Just grabbed one of those! It was actually only $1.18 for a year. Crazy! Thanks for the recommendation. – kcstricks Aug 19 '15 at 02:34
  • no problem :) must be $1.36 when converted to Australian currency! – Nick Triantafillou Aug 19 '15 at 02:35
  • Ah yes that makes sense. So I've finally got this set up. I have a load balancer running and I'm using Amazon Route 53 to set up A records to make my domain name a CNAME for the load balancer endpoint. Just so I am sure that I fully understand what I've done, is it true that it is no longer useful for my EC2 instance to have an elastic IP address? Before, my Route 53 hosted zone contained A records pointing to the EC2's elastic IP address. But now that the A records point to my load balancer, I don't see the benefit of having an elastic IP. Doesn't hurt but doesn't help. Is this right? – kcstricks Aug 20 '15 at 17:18
  • EIP's are useful for when you want to connect to it to develop on etc. They're also required in certain VPC configurations if you aren't allocating a public IP automatically. But in your case you might be fine without it. Also, instead of using the A record for CNAME to the load balancer end point, use an Alias Record. They only exist in route53 and they work better with load balancers. https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/resource-record-sets-choosing-alias-non-alias.html – Nick Triantafillou Aug 20 '15 at 23:25
  • Thanks Nick! I'm using an alias record now. I'll add an answer to this question summarizing all the steps you've helped me work through. Thanks so much! – kcstricks Aug 25 '15 at 14:44
3

For anyone who might find this useful, below is a summary of the steps you can take to get this all set up.

Set-up:

There are three options covered in the below steps:

A = NO domain name, NO load balancing

B = YES domain name, NO load balancing

C = YES domain name, YES load balancing

  1. (A+B+C) Create an EC2 instance.
  2. (A+B+C) Install all the necessities on your instance. At the very least, you'll want apache2 and whatever language(s) you wish to use for any backend scripts you want to run on your instance. I've used php.
  3. (A+B) If you don't want to get a domain name to use to access your EC2 instance, or if you want a domain name but don't need load balancing, you'll want to get an elastic IP address for your instance. This can be easily done through AWS. (C) If you want to use a domain name and load balancing, you won't need an elastic IP address. Go ahead and get yourself a domain name. I used namecheap.com. They currently (August, 2015) have .xyz TLDs for just $1.18 USD/year.
  4. (A+B+C) Add your backend files to the apache web server you installed on your EC2 instance in step 2.
  5. (B+C) Set up a hosted zone in Amazon Route 53. NOTE: Route 53 is NOT included in the AWS free tier and is therefore NOT FREE. The costs are here. (B only) Add A record(s) to your Route 53 hosted zone that point to the elastic IP of your EC2 instance.
  6. (B+C) Transfer the name servers of your domain name to the 3-5 name servers Route 53 gave you after completing step 5. The transfer will be done from the site you registered your domain name with.
  7. (C) Set up load balancing for your EC2 instance(s) in AWS. Note your load balancer's end point.
  8. (C) Add alias record(s) to your Route 53 hosted zone that point to your load balancer's end point.

Accessing your files on EC2

Let's say you have a php script myscript.php that you saved to your server in step 4. To run this script on your EC2 server after finishing the set up, you can do the following:

A - Open up a web browser and enter the following into the address bar: elastic_IP_of_your_EC2_instance/myscript.php

B+C - Open up a web browser and enter the following into the address bar: yourdomain.com/myscript.php

Hope this is helpful!

kcstricks
  • 1,489
  • 3
  • 17
  • 32