Connecting Your Heroku App with Its Amazon MySQL Database

Starting With Amazon Web Services

If you’re building a complete web app you’re probably using Amazon Web Services (AWS) taking advantage of their excellent cloud-based platform. Why not use them for server-side support? Especially if you’re a small shop and don’t want to worry about scaling up quickly and easily when your great idea finally goes viral. Until the world is beating down your doors it’s so convenient letting AWS NetOps experts upgrade hardware, apply OS patches, and power their data centers while you build solutions and value for your customers. Separation of concerns for the win!

Local Development – Whitelist Your IP

Right then. I’ll share my particular experience here. While building my web app all services were constructing locally, in PHP, served up by Apache. You might be developing under OSX while I use Windows 7 and it’s quite easily done. After signing up with AWS I spun up a RDS instance of MySQL through the AWS dashboard. From there I wondered, how do I ping it? That thing is locked down quit tightly (thank-you for that BTW) but how do I hit it from my local machine’s PHP services?

  • AWS dashboard (https://console.aws.amazon.com/console/home)
  • Click “RDS”
  • Click your instance under “My Resources”
  • Select the RDS instance from the list
  • “DB Security Groups”
  • Select the group, for example “default” from the list
  • Add your ip address as a “CIDR/IP” – if you’re at work that’s your company’s and it won’t change, but if you’re at home that might change as your connection renews – watch out for that when you least want a delay!
  • Google up “what’s my ip?” to confirm your spot on the internets
  • So easy, now have a coffee

That all goes swimmingly. Perhaps your dev server is on your home network, on your company network, or even up on a secondary R&D server you keep with a hosting provider. Could be any or all of those options in fact.

After Deploying to Heroku

Fast-forward. Service-side environment is setup locally, and even integrating the remote DBS. Now it’s time to get serious about the production-level environment. I’m using Heroku because it’s the awesome. You ought to check it out to, or you’re already signed up and wondering how to get it talking to your hot and ready AWS RDS.

Pushing my service up to Heroku is easy. There using of “Git” takes some getting used to of course. Excited to try it all the very first time I ran my web app no data came back. Figures. What in the world is that all about? Oh right, I need to whitelist the service ip just like i did when develop…what a minute. Heroku is a flexable managed server mesh. I’ll never know what IP address my service runs on, and it might be on lots of servers inside the Heroku network. Hrrmmm. That’s kinda crazytown.

Whitelisting Your Heroku App with AWS

Researching on it I found what I need to do is very similar to whitelisting an IP, and it’s found in the security group dashboard area. Instead of a “CIDR/IP” I add a “EC2 Security Group” using Heroku’s magic number and that’s: 098166147350

How in the world was I supposed to know that? Where did I find the info? Check out the Heroku article for additional background: https://devcenter.heroku.com/articles/amazon_rds

Happy Days

Once I allowed Heroku into my little bit of the Amazon playground my services worked as well as expected. Data came back, data went up, and everything worked nicely.

Have a coffee, reflect, celebrate, and do something awesome today!

 

You may enjoy my book if you learned from this article. It’s called Responsive Web Design Toolkit: Hammering Websites Into Shape. Get it on Amazon today!