Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rameerez/b3e64acbe925c831ebea7c41680b4484 to your computer and use it in GitHub Desktop.
Save rameerez/b3e64acbe925c831ebea7c41680b4484 to your computer and use it in GitHub Desktop.
Configure an AWS EC2 instance running Amazon Linux 2023 to deploy Rails apps using Kamal (Docker)
#!/bin/bash
# This scrips takes a clean AWS Amazon Linux 2023 AMI and installs and configures
# everything needed to deploy a Rails app to it using Kamal.
# The resulting state is a clean instance ready to accept Kamal (Dockerized) apps.
# --- AESTHETICS ---
# Define the color code for green for echo messages
GREEN='\033[0;32m'
# Define the escape sequence for the alien emoji (U+1F47D)
ALIEN='\xF0\x9F\x91\xBD'
# Define the variable for resetting the color back to the default
NC='\033[0m'
# --- CONFIG SCRIPT STARTS ---
# Update package list and install Docker
echo -e "${GREEN}${ALIEN} Installing Docker and starting the Docker service...${NC}"
sudo dnf update -y
sudo dnf install -y docker --allowerasing
sudo systemctl start docker
echo -e "${GREEN}${ALIEN} Adding rails to the docker group and configuring the right Docker permissions...${NC}"
sudo chown root:docker /var/run/docker.sock
# Add current user to the docker group
sudo usermod -a -G docker $USER
# Update the group membership for the docker group - This will log us into a new shell session with the updated group membership.
newgrp docker
echo -e "${GREEN}${ALIEN} Restarting the Docker service...${NC}"
sudo systemctl restart docker
# crontab does not come installed by default in AL2023 (wtf?)
# echo -e "${GREEN}${ALIEN} Installing crontab to run scheduled background jobs...${NC}"
# sudo dnf install -y cronie
# sudo systemctl enable crond.service
# sudo systemctl start crond.service
# Delete previous command history so we leave the AMI in a clean state
history -c
echo -e "${GREEN}${ALIEN} Amazon Linux 2023 machine initial setup for Kamal deployments completed successfully.${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment