I'm currently using Heroku (which uses AWS) and my program needs to communicate with my own EC2 instance. I've made my EC2 instance in the same Region as Herokus', however, in order to reduce costs further I want to be able to community via the private IP address between my Heroku dyno and EC2 instance in the same Availability Zone.
I know I can restart Heroku to switch between servers and potentially different availability zones, however, is there any way to determine if they're in the same availability zone or in a different availability zone?
Potentially I can write a program that will restart Heroku to the point that it is in the same zone.
That's not an easy task (if solvable at all), because availability zones are different per Amazon EC2 account in the first place, see FAQ How can I make sure that I am in the same Availability Zone as another developer?:
Eric Hammond has explored the topic and developed a technique/trick to work around this limitation, see Matching EC2 Availability Zones Across AWS Accounts:
However, he stresses the respective caveats as well:
The trick seems to work for the time being, so you might be able to achieve your goal by applying this technique, but be prepared for it not yielding a reliable solution down the road eventually.