Microservices on AWS EC2 — DevOps Project 03

Project repo here.

Bhavyansh @ DiversePixel
4 min readJun 15, 2024

Hi, I am learning and building in public.

In today’s project, we will be deploying Geeky following Microservices Architecture.

Let’s begin!

We have Geeky Prod, let’s bring it to life

So let’s set up the infrastructure

We will launch the instances for our services:

  • db
  • redis

We will also have different security groups for them, worthy to note that their inbound rules will accept ssh connections from ec2-sg, the security group of our main instance on which Geeky is running.

All set, we have all the instances ready

Tagging makes life easier, we will have to go to security groups often

Make changes in security groups:

  • Allow port 5432 inbound rule in db-geeky instance to accept traffic from geeky-prod security group (that is, ec2-sg in our case)
  • Similarly port 6379 for redis-geeky

Next let’s do configuration changes, first, redis:

Changed redis.conf in /etc/redis/

Restart service

sudo service redis-server restart

Redis is working when I ping it from my geeky-prod instance, good!

Next, postgres:

Any attempt of connecting to postgres from outside the machine will be refused
https://www.bigbinary.com/blog/configure-postgresql-to-allow-remote-connection

Files we will change:

  • /etc/postgresql/<version>/main/pg_hba.conf
This way we get better security (using private ip of prod instance only)
  • /etc/postgresql/<version>/main/postgresql.conf (I forgot to uncomment this line and it costed me a day of errors)
change from localhost to *
And also uncomment the line

netstat is a good tool to see ports on which traffic is allowed, run it to see the traffic from db instance

sudo netstat -plnt | grep postgres

We can allow it only from our specific IP by making changes to postgresql.conf but let’s proceed this way for now.

Trying to login to database from my application instance (geeky-prod)

And I can now login, because we specified trust in pg_hba.conf, it doesn’t ask for password:

Okay, done with the configurations, now running migrations (rails specific)

Test time

Postgresql first

Log in to db instance

Get into the database

Check a table, say rooms

Now let’s go to the application and create a room

Saving as expected, psql working fine.

Let’s also test redis,

The user list in a room gets updated in real time, let’s see that

Cool!

So we successfully deployed a rails application using Microservices architecture, allowing services to be updated without having to push whole of the code again, along with other benefits.

Questions, suggestions always welcome.

Connect with me on X, I am learning and building in public!

--

--

Bhavyansh @ DiversePixel
Bhavyansh @ DiversePixel

Written by Bhavyansh @ DiversePixel

Hey I write about Tech. Join me as I share my tech learnings and insights. 🚀

No responses yet