Deploying Geeky using AWS CLI — DevOps Project 01

GitHub Repo here, AWS CLI reference here.

Bhavyansh @ DiversePixel
4 min readJun 12, 2024

Hi everyone! This is me learning and building in public. The explanation in this post is limited, I hope the screenshots will cater to that, still if you have any questions, wanting to do it on your own, or any suggestions whatsoever please reach out to me on X.

Get to know Geeky: Geeky is my backend focused Ruby on Rails project, built as a monolith. You can find the GitHub repo for Geeky here. Please navigate to branch deploy_aws_clito see the changes I made during this deployment.

Let’s begin!

  1. Configure AWS
Configured AWS CLI with the IAM user credentials

Switched to WSL as my code sits there

What we need:

  1. key pair
key pair generated

2. security group

Using my aws cli guide to see the command for sg creation

My ip is needed, let’s find my ip online

Created security groups
  • Allowed port 22 for ssh and port 80 for the website to be visible
  • Curious about /32 (CIDR notation)? Check the article on Computer Networking

3. user-script (not mandatory, just me automating a bit more)

  • I already have the bash script ready.

This is the folder structure:
the app: geeky
secret-key: geeky-prod-cli.pem
launch-script: geeky_launch_script.sh

Now let’s do it, let’s see the image id for ubuntu 22

We are going with 64 bit x86 architecture

Using AWS CLI to run the instance:

aws ec2 run-instances \
--image-id ami-0e001c9271cf7f3b9 \
--count 1 \
--instance-type t2.micro \
--key-name geeky-prod-cli \
--security-groups geeky-prod-cli-sg \
--user-data file://../geeky_launch_script.sh

The instance is up

Let’s name it

Now let’s try to ssh and see what we’ve accomplished

To address this warning:

chmod 400 ../geeky-prod-cli.pem  # only owner of file has read/write permissions

Now let’s check the database, did it get created

Database setup done, other things are working okay

I had issues downloading ruby, had to do it manually

To prolong the time for which you can ssh from your terminal, either use PuTTY or follow the steps

On your local machine:

  • Open ~/.ssh/config file
  • Add (or create file):
Host *
ServerAliveInterval 60
ServerAliveCountMax 5

On the instance:

  • Open /etc/ssh/sshd_config
  • Add:
ClientAliveInterval 60
ClientAliveCountMax 5
  • ClientAliveInterval 60: Instructs the SSH server to send a keepalive message to the client every 60 seconds.
  • ClientAliveCountMax 5: Instructs the SSH server to disconnect the client if it doesn't receive a response to keepalive messages after 5 attempts.
sudo service ssh restart

To be able to login as user deploy:

I can now login as user deploy, time to do just that, DEPLOY!

Let’s test it now!

I got 500 internal server error, fix it by giving proper permissions to the deploy user:

chmod o+x $HOME

The simple steps we took:

  1. AWS CLI
    - Create Key pair
    - Create Security Group
  2. Launch the EC2 instance
  3. Download ruby manually
  4. Configure SSH to be able to login as user deploy
  5. Deploy
  6. Test

Let’s also terminate this instance using AWS CLI

aws ec2 describe-instances
aws ec2 terminate-instances --instance-ids <instance-id here>

The script is available on my GitHub @ DevOps_Projects

Questions and Suggestions are welcome!

Completed the first project, follow me as I learn and build in public: X @ bhavyansh001

--

--

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