I’m migrating some web apps from a few different cloud providers into AWS, one the apps includes object storage and a managed database but I had these services scattered on a few different boxes, this was by design, as I was testing reliability and resilience by switching things on and off and lifting replicas of the services on alternative providers. Now what I want to do is simply tie everything together on AWS using EC2, S3 and RDS.
I’m not going to go into detail on how I deploy my apps since I consider that off the scope in this case but I will mention, as on my previous post, that I use Hatchbox for simplicity. Regardless of the way you manage your deployments I believe it is likely that you might have end up tangled up in AWS config-hell just like I did so I think it’s good to explain exactly how to have an EC2 instance communicate to an RDS managed database.
I’d say the first step here will be to log into your AWS Console and go to the Virtual Private Cloud (VPC) dashboard:
Once in there go to the Your VPCs menu to access a list of all current VPCs, then go to the top right menu Create VPC.
You can almost leave all the details in this form as they are, but indeed this was exactly the reason I struggle to connect to my RDS instance.
If you select VPC and more at this point you can create the entire subnet configuration for this new VPC and then go and deploy the rest of your infrastructure. Unfortunately if this process was automated for you (like in my case) or if you just skipped this configuration you’ll effectively render your server and database unable to communicate:
-----> Starting deployment -----> Deploying a07a6bc798c92c3718e3a510858458f7b98b4058 from main branch -----> Deploying to aws-instance /home/deploy/hatchbox/releases/20220815150103/app/scripts/application_script.rb:61:in `rescue in on': rake aborted! (RuntimeError) ActiveRecord::ConnectionNotEstablished: connection to server at "111.11.1.00", port 5432 failed: Connection timed out Is the server running on that host and accepting TCP/IP connections?
To fix this issue you will now need to create Subnets for your VPC. Back to the VPC Console on the left hand side you will find the Subnets menu. Head in there and select the option Create Subnet.
From here, start by selecting your newly created VPC from the dropdown. My region is EU-West-2 and so I will add 2A, 2B and 2C to my availability zones
Select and IPv4 block (v6 is optional). An availability zone (you will need at least two to create an link a database from RDS), name is not mandatory. Continue adding as many as you need and finally commit the changes.
Now off to the EC2 dashboard. Creating an EC2 instance is the easiest thing. Just click on Launch Instance and head to the options for your server.
Once on the EC2 instance creation form select all the options that apply to your particular needs but pay attention to the Network Settings box. By default it will assign the default VPC network, not out newly created one. Click on edit and select the one that actually corresponds:
Finish by configuring the security group and options according to your needs and create the new instance. Off to RDS now:
Now to tie everything together head to the Relational Database Service (RDS) console.
First thing here is to go to the Subnet groups menu option on the left hand menu, you should be seeing a single subnet menu, the default one. Go ahead and click on Create DB subnet group.
Add a name and a description to the the new subnet group, select your VPC and add the availability zones created previously, remember you need at least 2 to save changes in this page. Finally just hit Create to commit this changes. Now back to the RDS console and into the Databases menu.
Select the Create database menu and then configure your managed database to suit your needs. Pay attention to the Connectivity options box:
Make sure to change the Virtual Private Cloud (VPC) option here to your custom VPC and select the newly created Subnet group.
Save the changes and give it a few minutes. Now deploy/redeploy and everything should work as expected:
-----> Starting deployment -----> Deploying a510858458f7b98b4058 from main branch -----> Deploying to aws-app -----> Deployed to aws-app -----> Restarting app -----> Running post deploy script -----> Deploy complete -----> Updating web server config
Just a final note. I faced one last challenge you might come across as well. Because I failed my original deployment I had to create the database manually, so SSH into the EC2 instance, locate your current deployment and hit it with:
bundle exec rails db:create
Wolf Zimmermann – Unsplash: https://unsplash.com/photos/6sf5rf8QYFE