I am just setting our Rails app on Amazon AWS. We have 2 EC2 instances, one for the Rails app, the second one for Redis. And then Amazon RDS for MySQL.

When I connect our Rails app with Redis, I need to set the Redis URL, which contains IP address. However, if I restart the Redis instance, its IP address will change and the Redis URL IP I entered to the Rails app for Redis will not work (because after restarting the Redis instance was generated a new IP).

How to handle this behavior? How to avoid "loosing" EC2 instance when restarting it? The same probably goes for Amazon RDS - this is how I set the host in database.yml:

  host: myapp-production.cg4mxcg62ca0.us-west-2.rds.amazonaws.com

Thank you in advance.

  • Their intent is for you to use an ELB infront of your server so the apps point to it (via DNS) not an IP address. For RDS you should be ok since you're using DNS and it will update dynamically, same is true for Redis, use the DNS name, not the IP. – TheFiddlerWins Mar 30 '16 at 20:52
  • Thank you for the answer, @TheFiddlerWins. Speaking about the Redis instance - I see in the EC2 dashboard "Private DNS", "Private IPs", "Public DNS" and "Public IP" - but all of these address contain an IP address (and all of these address will be re-created once the instance is restarted, right). – user984621 Mar 30 '16 at 21:05
  • Are you using a VPC? – TheFiddlerWins Mar 31 '16 at 12:50
  • In my console if I go to ElasticCache Dashboard > Cache Clusters > Nodes there is an "endpoint" listed that's a DNS name. These should be valid DNS entries – TheFiddlerWins Mar 31 '16 at 12:52
  • @TheFiddlerWins the OP is not using ElastiCache. He is using Redis on his own EC2 instance. – Matt Houser Mar 31 '16 at 13:53

Issue 1:

Elastic IP addresses should not be used to preserve the address of a "back-end" server. There are 2 reasons:

  1. Usually, you do not want back-end servers to be exposed to the internet and Elastic IP addresses only work from the internet into your EC2 instance.
  2. If you connect from one EC2 instance to another EC2 instance by it's Elastic IP address, then the connection leaves your VPC and re-enters the VPC. This makes security harder and will incur data traffic charges.

Instead, you should reference your back-end server by it's private IP address. Stopping and restarting an EC2 instance will not change the private IP address. Only when an EC2 instance is terminated will the internal IP address be released. And when you launch an EC2 instance, you can optionally choose it's private IP address.

You can create a DNS entry (Route 53 private hosted zones) for your back-end server.

Another thing you can do, but it's not required, is to create a network interface with your private IP address. Then attach that network interface to the EC2 instance. If you need a new back-end server, then just move the network interface to the new instance and the private IP address will move with it.

Issue 2:

RDS instance endpoints do not change over time.

Also, RDS instances cannot be stopped. They can only be deleted. But, even if you delete an RDS instance and re-create it, if it has the same RDS instance ID, then it's endpoint will remain consistent.

So it's safe, and best-practice, to reference your RDS instance by it's hostname myapp-production.cg4mxcg62ca0.us-west-2.rds.amazonaws.com.

Final Thoughts:

Put your Redis server and your RDS instance in private subnets in your VPC. There is no reason why the public needs access to them. Your Rails app instance should be in a public subnet because the public will access your app through that instance.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.