View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
#BASTION SERVER #DATABASES #DEBUGGING #POSTGRESQL #RAILS #SECURITY #SSH TUNNELING

In a recent sequence of posts on deploying a Rails application to Elastic Beanstalk (Step1 is here) we set up a secure VPC with public and private subnets. Part of this secure setup is to place your PostgreSQL database server in the private subnets and not make it publicly accessible.

This is great from a security perspective, but raises the question - how do I run my rails console against the database? Typical reasons for doing this might be to bootstrap your users (e.g. create the first Admin user). Or for when you need to debug some data issue.

This post runs through how to tunnel through your Bastion server to your database.

To tunnel through to our database we use ssh:

ssh -i [IDENTITY_FILE] -L [LOCAL_PORT]:[RDS_HOST]:[RDS_PORT] ec2-user@[BASTION_HOST]

An example of what this command might look like is:

ssh -i DemoKeyPair.pem -L 6543:demo.c2syu2demiip.eu-west-1.rds.amazonaws.com:5432 ec2-user@52.214.152.242

This will create a port mapping through our bastion server from our local machine port 6543 to demo.c2syu2demiip.eu-west-1.rds.amazonaws.com port 5432.

Once this is successfully set up we can connect to our database as if it was running locally:

psql -h localhost -p 6543 -U db_user -d demo

And to run our rails console (assuming you have setup your production database yml to use DATABASE_URL):

DATABASE_URL=postgres://db_user:Y509q2XJ8M@localhost:6543/demo DISABLE_SPRING=1 bundle rails console production

One important thing to note - DISABLE_SPRING=1 - this is necessary as otherwise spring will bring up the console with the cached environment from previous runs. This can lead to you connecting to the develop database or to errors if you don’t have PostgreSQL running locally:

	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

As always, once you are finished doing what needs to be done with the database, make sure you shutdown your Bastion server. Ideally it should only be run on an as needed basis.

#BASTION SERVER #DATABASES #DEBUGGING #POSTGRESQL #RAILS #SECURITY #SSH TUNNELING

Related Posts

Step 3 - Configure RDS: Deploying a Rails Application to Elastic Beanstalk - We're moving on and tightening our database security in step 3. We're running our database in private subnets within a created RDS instance on our VPC, making sure our data isn't accidentally left in the open. After setting up our network and security settings, we'll dive into creating our databases manually via our secure Bastion server. With a few commands, we set up our dev and prod databases, don them with strong passwords and grant them access to our master username. Voila, our RDS instance is ready to go! Stay tuned for step 4, where we unveil the real deal, deploying a Rails app.
Step 1 - Setup VPC: Deploying a Rails Application to Elastic Beanstalk - In this blog post, I am guiding you through the process of deploying a Rails application to Elastic Beanstalk in a Virtual Private Cloud (VPC) on Amazon AWS. I detail the setup of a VPC, subnets, and internet gateways, as well as the configuration of NAT gateways and security groups. Ultimately, this will allow for a safe, internet-accessible environment for your application and its accompanying databases.
Step 2 - Setup Elastic Beanstalk: Deploying a Rails Application to Elastic Beanstalk - In this post, I describe how to set up, configure and deploy an Elastic Beanstalk application on a VPC in Amazon AWS, using Rails 5 and Ruby, using Puma for deployment and configuration of Public and Private subnets in Elastic Load Balancer. I also cover the details of network card settings, the selection process for subnets and security groups for Load Balancer and Instances, and finally shared the result of deploying the sample application on Elastic Beanstalk.
Step 7: Action Cable on Elastic Beanstalk - This blog provides detailed steps and configurations on setting up an application using Action Cable feature in Rails 5. It talks about setting up proper environment for the application to run and includes using both PostgreSQL and Redis as backend services for the ActionCable. The blog also shares troubleshooting commands in case of any issues encountered during the setup. Built on top of WebSockets, Action Cable was first announced at RailsConf 2015 and now a substantial part of Rails 5 providing real time communication with backend server.
Step 4: Deploy Rails App To Elastic Beanstalk from Command Line - This post guides you through the essential steps of creating and deploying a Rails application into Elastic Beanstalk environment. Topics include creating an AWS user, configuring AWS CLI for deployment, and setting up the environment variables for your app. Also, it provides you the valuable knowledge of connecting your Rails app to RDS instance and ensuring it successfully connects with the dev database.

Related Videos

The Hacker News Effect - The Website Didn't Catch Fire - Let's look at the traffic - Witness the Hacker News effect in action as the author's blog skyrocketed to popularity, easily handling massive traffic thanks to efficient hosting and Cloudfront!
Revolutionize Your Raspberry Pi Development with VSCode Remote! - Learn how to develop code on Raspberry Pi using VSCode without needing VNC or a desktop environment by setting up a remote development environment. Develop your projects more conveniently and efficiently with this powerful tool!
Raspberry Pi Hidden Settings - Setup Hostname, SSH and WiFi from the Pi Imager Application. - Uncover a hidden screen in the Raspberry Pi Imager Application to easily set up SSH, WiFi, and hostnames for your device without creating additional files. Just press Ctrl+Shift+X!
ESP32 Remote Logging - Learn how to remotely monitor your Arduino devices by sending log messages to a central server using UDP and a paper trail. Get insights into how your hardware is performing, even when you can't physically access it.
Bootlace Crimping - What?, Why?, How? - Learn about the benefits of using bootlace ferrules for stranded wires and how to easily crimp them for a secure connection in screw terminals.
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


Published

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts