First, I’m assuming you’ve got both an EC2 instance and an RDS instance in the same VPC, as mentioned in my article on setting up a Rails application to deploy to Elastic Beanstalk. I’m also assuming you’ve created a .pem
key and downloaded it to connect to this EC2 instance
configure an alias in ssh config to connect to the EC2 instance
Host my_app
HostName xx.xx.xxx.xxx
User ec2-user
IdentityFile ~/.ssh/my_app.pem
ForwardAgent yes
test the ssh config: ssh my_app
Last login:
_____ _ _ _ ____ _ _ _
| ____| | __ _ ___| |_(_) ___| __ ) ___ __ _ _ __ ___| |_ __ _| | | __
| _| | |/ _` / __| __| |/ __| _ \ / _ \/ _` | '_ \/ __| __/ _` | | |/ /
| |___| | (_| \__ \ |_| | (__| |_) | __/ (_| | | | \__ \ || (_| | | <
|_____|_|\__,_|___/\__|_|\___|____/ \___|\__,_|_| |_|___/\__\__,_|_|_|\_\
Amazon Linux AMI
This EC2 instance is managed by AWS Elastic Beanstalk. Changes made via SSH
WILL BE LOST if the instance is replaced by auto-scaling. For more information
on customizing your Elastic Beanstalk environment, see our documentation here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
[ec2-user@xx-xx-xxx-xxx ~]$
add this alias to ~/.bashrc
alias my_rds='ssh -N -L 55432:my-app-db-instance.us-east-1.rds.amazonaws.com:5432 my_app'
you’re using your my_app
ssh config to forward traffic from my-app-db-instance
, port 5432 to localhost 55432
Traffic from your db instance will now forward to 55432, you could theoretically change your .env
file to have this configuration
database_name: 'my-rds-db-name'
database_username: 'my-rds-db-username'
database_password: 'my-rds-db-password'
database_port: '55432'
database_url: 'localhost'
and your config/database.yml
to have this configuration
development:
<<: *default
database: <%= ENV['database_name'] %>
pool: 5
username: <%= ENV['database_username'] %>
password: <%= ENV['database_password'] %>
host: <%= ENV['database_url'] %>
port: <%= ENV['database_port'] %>
reaping_frequency: 10
As a note of warning, if you’re doing this with a production RDS instance while doing local development, you’re making changes to live production data while doing dev work, definitely not recommended.