The deployment of Ruby on Rails and Rack frameworks is embedded in Cloud 66’s DNA, but we are always interested to see other Ruby frameworks becoming popular, and happy to support developers who prefer to use them instead of Rails. In this post, we’re going to show you how easy it is to deploy a Hanami Ruby application on AWS cloud with Cloud 66. The recent Hanami v2.0 release brought a lot of incremental upgrades to the framework, which made it more mature. It will be interesting to see how Hanami's popularity grows in 2023.
Ruby Hanami Framework Overview 🌸
Hanami is a modern web framework for full-stack Ruby applications. The framework uses small, single-purpose, independent libraries instead of one mono-repo application. This allows developers to pull out an API into a separate micro-service, for example.
At the end of 2022, after four years, we finally got to try out the Hanami 2.0.1 release. There have been so many changes that it feels almost like a brand new framework - “better, faster, stronger!” The core of Hanami 2.0 is now the
app/ directory. This release focuses on the simplicity to maintain, test, and scale small services and large monolith applications.
The Main Advantages of the Hanami Framework:
- Featured First - Hanami focused on building applications around lightweight features rather than e.g. MVC. This gives developers a lot of freedom, as they can decide how to assemble an application based on the features. Additionally, it consumes 60% less memory than other Ruby frameworks e.g. Rails.
- Fast and Lightweight - the framework is built with small independent repositories and, as a result, it's optimized for use with content delivery networks.
- Simple in Design - the simplicity of the design and minimalism of its components allows flexibility when changing code (now and in the future).
- Secure - Hanami applications use modern browser technologies like Content Security Policy, X-Frame headers, synchronized tokens against CSRF, HTML escaping to prevent XSS, and a clear database API to avoid SQL injection.
What type of application is best for Hanami
Hanami best suits applications with the following characteristics:
- Complex websites that deliver fast responses time and accuracy;
- API development;
- Secure web apps like eCommerce platforms or email apps;
Ruby Frameworks: Hanami vs Rails
Rails is the most popular Ruby framework and focuses on ease of use, whereas Hanami is considered a less-known, young, light alternative to Ruby on Rails with a focus on speed and simplicity.
Although there are many similarities, there are important differences between frameworks:
- Hanami offers a web-first approach (including view rendering, helpers, API only, crawlers, scripting, webhooks, events, data processing, and business logic engines), whereas Rails offer a web-only approach.
- Hanami is a lighter faster framework, as the files load on demand, compared to Rails when the files load at the boot time.
- Hanami offers a more functional style of programming, whereas Rails is object orientated.
- With Hanami, you can write a stateless object, whereas in Rails you have stateful objects.
- Hanami offers a larger number of small files compared to Rails, which offers fewer but bigger files.
- Hanami offers dependency injection, which results in memory optimization and speed, whereas Rails offers global access.
Note: For a more detailed comparison read the “Learn new things easily - mapping Rails to Hanami” article.
Deploy Hanami web app to AWS with Cloud 66
Now, let’s use a Hanami application sample and deploy it to AWS with Cloud 66.
What do you need to deploy with Cloud 66?
Cloud 66 offers ‘PaaS on cloud’, which means that it provides PaaS functionality on any cloud. To get started you will need the following:
- A Cloud 66 account - You can sign up for a Cloud 66 account here, with a GitHub or Google account, or with your email. With the 4-week free trial, and credit card is not required.
- An AWS account - You can use your existing AWS account or sign-up and create a brand new AWS account. Alternatively, you can use any other cloud provider.
- Application code and/or pre-built images - Your application code should be hosted in a (secure) publicly accessible git repository. However, for the purpose of this blog post, we will use a Cloud 66 Hanami 2.0 sample: https://github.com/cloud66-samples/hanami#bookshelf
We created a simple Hanami 2.0 sample, with Phusion Passenger and a MySQL Database.
Note: Cloud 66 will detect and create your database for you (on the server you want) and then you can connect to it via your Hanami configuration using the environment variable
MYSQL_URL(for MySQL for example).
Let’s get started:
To get started, log in to your Cloud 66 account and grant Cloud 66 read-only access to your git repository. Choose the Hanami 2.0 repository (our sample) and set the branch (main) and environment (production). Next, give the application a name (My-Hanami-App) and then click the ‘Analyze’ button. Cloud 66 will now scan the repository and suggest the best settings for our application.
Now, you can review the application configuration, and if there is anything that you would like to change, update your application and you can click the “Reanalyze my code” button.
The next step is to connect your cloud provider account with Cloud 66, so we can determine the deployment destination. Simply select AWS from the drop-down list and add your AWS API Key to confirm the access. Once they are connected, choose the server region (London) and the server size. Lastly, specify whether your datastore will share the app server. Sharing servers is not recommended for the production environment, but for the purpose of this demo, we will choose that option. Now click the ‘Start Deployment’ button.
The deployment time depends on the size and complexity of your application. Once the deployment is completed we will send you an email. Alternatively, you can have a look at the LiveLogs to see what is happening under the bonnet.
Now, that your application is shared with the world. Explore Cloud 66 dashboard and take the advantage of Cloud 66 features to enhance your application. All features are included in your account.
Cloud 66 dashboard discovery:
- Overview - in this section you will see when and where the application was deployed, visit the site, and look at the health check. Check your application servers, jobs, and deployment timeline, and set up a preview deployment. Additionally, we will suggest a few tips on how you can improve your application.
- Network - under this tab you will be able to set up Firewalls, SSL Certification, Load Balancers, Traffic, Redirects, and App Private Networks.
- ActiveProtect - protects your application and servers against denial of service and brute-force attacks.
- LiveLogs - collects and displays real-time logs that can help you find and eliminate bugs in your Rails app.
- Notifications - this tab will allow you to set up what notifications would you like to receive and via which channels.
- Application Settings - this tab will give you access to general application settings (for your servers, repository, deployment, and application action), environment variables, health checks, configuration files, ConfigStore, Application Updates, and Deployment Profiles).