Deploying a sinatra application to Elastic Beanstalk
This walkthrough shows how to deploy a simple Sinatra
Prerequisites
This tutorial assumes you have knowledge of the basic Elastic Beanstalk operations and the Elastic Beanstalk console. If you haven't already, follow the instructions in Getting started with Elastic Beanstalk to launch your first Elastic Beanstalk environment.
To follow the procedures in this guide, you will need a command line terminal or shell to run commands. Commands are shown in listings preceded by a prompt symbol ($) and the name of the current directory, when appropriate.
~/eb-project$ this is a command
this is output
On Linux and macOS, you can use your preferred shell and package manager. On Windows you can install the Windows Subsystem for Linux
Sinatra 2.1.0 requires Ruby 2.3.0 or newer. In this tutorial we use Ruby 3.0.2 and the corresponding Elastic Beanstalk platform version. Install Ruby by following the instructions at Setting up your Ruby development environment for Elastic Beanstalk.
Launch an Elastic Beanstalk environment
Use the Elastic Beanstalk console to create an Elastic Beanstalk environment. Choose the Ruby platform and accept the default settings and sample code.
To launch an environment (console)
-
Open the Elastic Beanstalk console using this preconfigured link: console.amazonaws.cn/elasticbeanstalk/home#/newApplication?applicationName=tutorials&environmentType=LoadBalanced
-
For Platform, select the platform and platform branch that match the language used by your application.
-
For Application code, choose Sample application.
-
Choose Review and launch.
-
Review the available options. Choose the available option you want to use, and when you're ready, choose Create app.
Environment creation takes about 5 minutes and creates the following resources:
-
EC2 instance – An Amazon Elastic Compute Cloud (Amazon EC2) virtual machine configured to run web apps on the platform that you choose.
Each platform runs a specific set of software, configuration files, and scripts to support a specific language version, framework, web container, or combination of these. Most platforms use either Apache or NGINX as a reverse proxy that sits in front of your web app, forwards requests to it, serves static assets, and generates access and error logs.
-
Instance security group – An Amazon EC2 security group configured to allow inbound traffic on port 80. This resource lets HTTP traffic from the load balancer reach the EC2 instance running your web app. By default, traffic isn't allowed on other ports.
-
Load balancer – An Elastic Load Balancing load balancer configured to distribute requests to the instances running your application. A load balancer also eliminates the need to expose your instances directly to the internet.
-
Load balancer security group – An Amazon EC2 security group configured to allow inbound traffic on port 80. This resource lets HTTP traffic from the internet reach the load balancer. By default, traffic isn't allowed on other ports.
-
Auto Scaling group – An Auto Scaling group configured to replace an instance if it is terminated or becomes unavailable.
-
Amazon S3 bucket – A storage location for your source code, logs, and other artifacts that are created when you use Elastic Beanstalk.
-
Amazon CloudWatch alarms – Two CloudWatch alarms that monitor the load on the instances in your environment and that are triggered if the load is too high or too low. When an alarm is triggered, your Auto Scaling group scales up or down in response.
-
Amazon CloudFormation stack – Elastic Beanstalk uses Amazon CloudFormation to launch the resources in your environment and propagate configuration changes. The resources are defined in a template that you can view in the Amazon CloudFormation console
. -
Domain name – A domain name that routes to your web app in the form
subdomain
.region
.elasticbeanstalk.com.Note
To augment the security of your Elastic Beanstalk applications, the eb.amazonaws.com.cn domain is registered in the Public Suffix List (PSL)
. For further security, we recommend that you use cookies with a __Host-
prefix if you ever need to set sensitive cookies in the default domain name for your Elastic Beanstalk applications. This practice will help to defend your domain against cross-site request forgery attempts (CSRF). For more information see the Set-Cookiepage in the Mozilla Developer Network.
All of these resources are managed by Elastic Beanstalk. When you terminate your environment, Elastic Beanstalk terminates all the resources that it contains.
Note
The Amazon S3 bucket that Elastic Beanstalk creates is shared between environments and is not deleted during environment termination. For more information, see Using Elastic Beanstalk with Amazon S3.
Write a basic sinatra website
To create and deploy a sinatra application
-
Create a configuration file named config.ru with the following contents.
Example config.ru
require './helloworld' run Sinatra::Application
-
Create a Ruby code file named helloworld.rb with the following contents.
Example helloworld.rb
require 'sinatra' get '/' do "Hello World!" end
-
Create a Gemfile with the following contents.
Example Gemfile
source 'https://rubygems.org' gem 'sinatra' gem 'puma'
-
Run bundle install to generate the
Gemfile.lock
~/eb-sinatra$
bundle install
Fetching gem metadata from https://rubygems.org/.... Resolving dependencies... Using bundler 2.2.22 Using rack 2.2.3 ... -
In order for Elastic Beanstalk to successfully deploy the application on the Ruby platform, we need to update
Gemfile.lock
. Some dependencies ofGemfile.lock
might be platform specific. Therefore, we need to addplatform ruby
toGemfile.lock
so that all required dependencies are installed with the deployment.~/eb-sinatra$
bundle lock --add-platform ruby
Fetching gem metadata from https://rubygems.org/.... Resolving dependencies... Writing lockfile to /Users/janedoe/EBDPT/RubyApps/eb-sinatra/Gemfile.lock -
Create a Procfile with the following contents.
Example Procfile
web: bundle exec puma -C /opt/elasticbeanstalk/config/private/pumaconf.rb
Deploy your application
Create a source bundle containing the your source files. The following command creates a source
bundle named sinatra-default.zip
.
~/eb-sinatra$ zip ../sinatra-default.zip -r * .[^.]*
Upload the source bundle to Elastic Beanstalk to deploy Sinatra to your environment.
To deploy a source bundle
Open the Elastic Beanstalk console
, and in the Regions list, select your Amazon Web Services Region. -
In the navigation pane, choose Environments, and then choose the name of your environment from the list.
Note
If you have many environments, use the search bar to filter the environment list.
-
On the environment overview page, choose Upload and deploy.
-
Use the on-screen dialog box to upload the source bundle.
-
Choose Deploy.
-
When the deployment completes, you can choose the site URL to open your website in a new tab.
Cleanup
When you finish working with Elastic Beanstalk, you can terminate your environment. Elastic Beanstalk terminates all Amazon resources associated with your environment, such as Amazon EC2 instances, database instances, load balancers, security groups, and alarms.
To terminate your Elastic Beanstalk environment from the console
Open the Elastic Beanstalk console
, and in the Regions list, select your Amazon Web Services Region. -
In the navigation pane, choose Environments, and then choose the name of your environment from the list.
Note
If you have many environments, use the search bar to filter the environment list.
-
Choose Actions, and then choose Terminate environment.
-
Use the on-screen dialog box to confirm environment termination.
With Elastic Beanstalk, you can easily create a new environment for your application at any time.
Next steps
For more information about Sinatra, visit sinatrarb.com
As you continue to develop your application, you'll probably want a way to manage environments and deploy your application without manually creating a .zip file and uploading it to the Elastic Beanstalk console. The Elastic Beanstalk Command Line Interface (EB CLI) provides easy-to-use commands for creating, configuring, and deploying applications to Elastic Beanstalk environments from the command line.
Finally, if you plan on using your application in a production environment, you will want to configure a custom domain name for your environment and enable HTTPS for secure connections.