DEPLOYING RAILS 7 WITH KAMAL 1.0 ON AWS LIGHTSAIL
Ruby on Rails
Kamal
AWS Lightsail
cloud services
VPS
EC2
RDS
Load Balancers
Block Storage
DNS
Route53
Running Ruby on Rails with Kamal has revolutionized the process of full-stack development, seamlessly integrating development and production environments. With various deployment options available, deploying Rails to the cloud offers unparalleled scalability and reliability. In this guide, we'll focus on deploying Rails to AWS Lightsail—a comprehensive cloud solution offering a suite of services including VPS, EC2, RDS, Load Balancers, Block Storage, DNS, and Route53—all bundled into one easy-to-use platform at an affordable price point. Our goal is to walk you through the process of getting your Rails project up and running on the cloud effortlessly.
Requirements
Before proceeding, ensure your Rails project is up and running on your local server. If you haven't set up your Rails project yet, you can follow this Rails Guide to get started quickly.
Next, integrate Kamal into your Rails framework by installing it via Gems. For guidance on Kamal installation, refer to our dedicated guide here (link to Kamal Installation guide).
To set up AWS Lightsail, you'll need an active AWS account. If you haven't done so already, sign up for an AWS account to access all the services. Additionally, take advantage of Free Tier services available for a limited time to minimize costs during deployment
Creating a Rails 7 Application
Now to generate a new Ruby on Rails 7 project,
rails new blog
this will create a new application Blog and install the necessary default gems to support the framework.
Configuring the application for deployment with Kamal
Adding Kamal to Rails applications is by adding the it through gems.
gem install kamal
initialize Kamal inside the application
kamal init
this will create the following files:
config/deploy.yml .env .kamal/hooks
Setting Up AWS Lightsail Instance
Access Lightsail Dashboard: Log in to your AWS Management Console and navigate to the Lightsail dashboard.
Create Instance: Click on the "Create instance" button to begin the instance creation process.
Choose Instance Location: Select the AWS region where you want to deploy your instance. Choose the region closest to your target audience for optimal performance.
Select Instance Platform: Choose the platform for your instance. Since you're deploying a Rails application, select the "Linux/Unix" platform.
Choose Instance Blueprint: Select a blueprint that best fits your needs. You can choose a blueprint that includes a pre-configured environment for Rails, or opt for a basic Linux distribution and configure Rails manually.
Select Instance Plan: Choose an instance plan based on your resource requirements and budget. Lightsail offers various plans with different combinations of CPU, RAM, and storage.
Name Your Instance: Provide a unique name for your instance to easily identify it within the Lightsail dashboard.
Create Instance: Click on the "Create instance" button to initiate the instance creation process. Lightsail will provision the instance based on your selected options.
Wait for Provisioning: Wait for Lightsail to provision the instance. This process usually takes a few minutes.
Access Instance: Once the instance is provisioned, you can access it via SSH using the provided public IP address and default username (usually "ubuntu" for Linux instances).
(Optional) Configure Firewall: If necessary, configure the instance firewall settings to allow inbound traffic on specific ports (e.g., port 80 for HTTP, port 443 for HTTPS).
(Optional) Allocate Static IP: Consider allocating a static IP address to your instance to ensure that its IP address remains unchanged across reboots.
Deploying Rails 7 Application with Kamal
Configuring Kamal for deployment
After initializing Kamal, it should be able to produce a file `config/deploy.yml`. Below is sample setup for Kamal to get you going for the project to server.
# Name of your application. Used to uniquely configure containers. service: myapp # Name of the container image. image: myapp_image builder: remote: arch: amd64 # Deploy to these servers. servers: web: hosts: - example.com labels: traefik.http.routers.myapp.rule: Host(`example.com`,`www.example.com`) traefik.http.routers.myapp_secure.entrypoints: websecure traefik.http.routers.myapp_secure.rule: Host(`example.com`,`www.example.com`) traefik.http.routers.myapp_secure.tls: true traefik.http.routers.myapp_secure.tls.certresolver: letsencrypt # Credentials for your image host. registry: # Specify the registry server, if you're not using Docker Hub # server: registry.digitalocean.com / ghcr.io / ... username: my_username # Always use an access token rather than real password when possible. password: - MY_REGISTRY_PASSWORD # Inject ENV variables into containers (secrets come from .env). # Remember to run `kamal env push` after making changes! env: secret: - RAILS_MASTER_KEY # Use a different ssh user than root ssh: user: ubuntu traefik: options: publish: - "443:443" volume: - "/letsencrypt/acme.json:/letsencrypt/acme.json" args: entryPoints.web.address: ":80" entryPoints.websecure.address: ":443" certificatesResolvers.letsencrypt.acme.email: "my_email@example.com" certificatesResolvers.letsencrypt.acme.storage: "/letsencrypt/acme.json" certificatesResolvers.letsencrypt.acme.httpchallenge: true certificatesResolvers.letsencrypt.acme.httpchallenge.entrypoint: web volumes: - "storage:/rails/storage"
After reviewing the Kamal deploy.yml, it's evident that you'll need to set up an account with a registry service similar to DockerHub, or your preferred image hosting platform. Once registered, you'll then replace the placeholder values within the 'registry:' section with your own authentication credentials.
Deploy application to AWS Lightsail Instance
At this moment, you should have already setup your instance in AWS Lightsail with the ability to connect via ssh. Inside the project directory, you can now run the following Kamal commands,
For first time installation and complete deployment,
kamal deploy
Or you can run the following for code updates of the application,
kamal redeploy
Executing the Kamal deploy command streamlines the process of launching your application for production, ensuring all essential commands are executed seamlessly. Once deployed, your application will be accessible to the public domain, or via its assigned IP address if applicable.