You walk into your hackathon this weekend and are handed a business card from MLH and AWS with a URL on it. You head to the URL, fill out the form, and a magic $100 promo credit appears in your inbox to use on Amazon Web Services.

Halfway through the hackathon, your hack is starting to take shape. You realize that for your demo to be successful, users need to be able to go to your web app on their own phones. In this tutorial, I’m going to show you how to deploy your hack on Elastic Beanstalk so that you can make it available for the whole world to check out! Now you can make the best use of that $100 in Amazon Web Services credit that you got from Major League Hacking.

 

Let’s dive right in. If you’re reading this tutorial, I’m going to assume that you have already signed up for an AWS account and redeemed your promo credit that you got from MLH and AWS at your hackathon. I am also going to assume that you have git installed on your computer. Click here for help installing git.

 

To keep this tutorial short, I’ll be showing you how deploy a basic Sinatra.rb (Ruby) Hello World web server.  Amazon Web Services and Elastic Beanstalk support any dev environment, so feel free to experiment with other languages aside from Ruby and other frameworks aside from Sinatra (like Rails!). If you want to follow along, make sure you have Ruby, and Python already installed on your computer.

 

Step 1: Install the Elastic Beanstalk Command Line Tool

Click here for instructions on installing the latest AWS Elastic Beanstalk Command Line Tool

For most people it should be a simple pip install awsebcli .

After you’ve got the CLI installed you want to make sure it’s working.

Now try typing eb status. You should get the following error message if your PATH has been configured correctly and you have the eb command line tool properly installed:

ERROR: This directory has not been set up with the EB CLI
You must first run “eb init”.

In some shells you may need to start a new shell or run rehash in order to find the CLI.

Step 2: Set Up Your App

Note: If you are not using Ruby, skip this part

Now that you have properly configured the Elastic Beanstalk command line utility, you will need to set up your Hello World app. Since we are using Ruby as our language and Sinatra as our web framework for this tutorial, I will show you how to set those up. But feel free to use any language or framework that you are comfortable with!

First off, go into a new directory (or your existing app’s project directory) and create a new git repository by using the git init command. It should respond with:

Initialized empty Git repository in /your-folder-name

Now create a new file called Gemfile – in Ruby apps the Gemfile is what manages your dependencies aka Rubygems. Add the following content to your Gemfile:
Then run the bundle install (you may need to run gem install bundler) command – this will load up your Gemfile and install all of the required Rubygems for your project. If it was successful, you should see something similar to the following output:

Fetching gem metadata from https://rubygems.org/………..
Fetching version metadata from https://rubygems.org/..
Resolving dependencies…
Installing rack 1.6.0
Installing rack-protection 1.5.3
Installing tilt 2.0.1
Installing sinatra 1.4.6
Using bundler 1.9.4
Bundle complete! 1 Gemfile dependency, 5 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

You should also see a new file called Gemfile.lock – this file records the versions of each Rubygem that Bundler installed for your app. Next, create another file called config.ru with the following contents:
This file tells Elastic Beanstalk how to run your application once it is deployed.

Last but not least, you need to create app.rb – this is the code for your Hello World application. Just to recap, we are using the Sinatra web framework. It’s really straightforward and lightweight – if you use Python, flask is a comparable framework.

This script simply responds with ‘Hello World!’ whenever anyone navigates to http://yoururl.com/hi
Nice, sweet, and simple! You can test it out locally by running ruby app.rb and navigating to http://localhost:4567/hi

At this point, you should make sure to commit all of your changes using the following git commands:

git add Gemfile Gemfile.lock

git add config.ru

git add app.rb

git commit -m “Set up Sinatra Hello World app.rb”

Step 3: Deploy to Elastic Beanstalk

Your app is ready to go, you installed the eb command line utility. Now you can SHIP IT!

The nice thing about Elastic Beanstalk is that it is very configurable, but not in an overbearing way. It has just enough configuration for you to have control over how your app is deployed and hosted.

So, to kick off the process, just type eb init in your app’s project directory (where app.rb lives). The first thing you will be asked to do is to authenticate your AWS account.

To get your AWS Access Key ID and Secret Access Key,
visit “https://aws-portal.amazon.com/gp/aws/securityCredentials”.
Enter your AWS Access Key ID:
Enter your AWS Secret Access Key:

Once you have authenticated successfully, you will be asked to select a ‘service region’ – this is the datacenter where your app is physically located in the world. The general rule of thumb is to select the one closest to where your users live. Since you’re at a hackathon, you should just select the service region closest to your current location!

Select an AWS Elastic Beanstalk service region.
Available service regions are:
1) US East (Virginia)
2) US West (Oregon)
3) US West (North California)
4) EU West (Ireland)
5) EU Central (Frankfurt)
6) Asia Pacific (Singapore)
7) Asia Pacific (Tokyo)
8) Asia Pacific (Sydney)
9) South America (Sao Paulo)
Select (1 to 9):

Once you have selected a service region, you will be asked to name your app:

Select an application to use
1) [ Create new Application ]
(default is 1): 1

Use a descriptive name:

Enter Application Name
(default is “test”): sinata-test
Application sinata-test has been created.

Next you are asked to select an environment tier:

It appears you are using Ruby. Is this correct?

(y/n): y

Select a platform version.
1) Ruby 2.2 (Puma)
2) Ruby 2.1 (Puma)
3) Ruby 2.0 (Puma)
4) Ruby 2.2 (Passenger Standalone)
5) Ruby 2.1 (Passenger Standalone)
6) Ruby 2.0 (Passenger Standalone)
7) Ruby 1.9.3
(default is 1): 1
Do you want to set up SSH for your instances?
(y/n): y

Select a keypair.
1) [ Create new KeyPair ]
(default is 4): 1

Now we want to create the environment that the project will run in (in this case a development environment, not production). We run eb create.

$ eb create
Enter Environment Name
(default is sinata-test-dev):
Enter DNS CNAME prefix
(default is sinata-test-dev):
Environment details for: sinata-test-dev
Application name: sinata-test
Region: us-east-1
Deployed Version: 9af9
Environment ID: e-jy5ezr9t8i
Platform: 64bit Amazon Linux 2015.03 v1.3.0 running Ruby 2.2 (Puma)
Tier: WebServer-Standard-
CNAME: sinata-test-dev.elasticbeanstalk.com
Updated: 2015-04-16 03:25:53.947000+00:00
Printing Status:
INFO: createEnvironment is starting.
INFO: Using elasticbeanstalk-us-east-1-054060359478 as Amazon S3 storage bucket for environment data.
INFO: Created load balancer named: awseb-e-j-AWSEBLoa-13LP0N21GGC01
INFO: Created security group named: awseb-e-jy5ezr9t8i-stack-AWSEBSecurityGroup-149RI69DE7H0V
INFO: Created Auto Scaling launch configuration named: awseb-e-jy5ezr9t8i-stack-AWSEBAutoScalingLaunchConfiguration-1XFOA9D9KTO1K
INFO: Waiting for EC2 instances to launch. This may take a few minutes.
INFO: Created Auto Scaling group named: awseb-e-jy5ezr9t8i-stack-AWSEBAutoScalingGroup-1M46J15H39XJI
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:054060359478:scalingPolicy:06e1b0c6-eeb9-4a6f-b0fd-1f55a5156f36:autoScalingGroupName/awseb-e-jy5ezr9t8i-stack-AWSEBAutoScalingGroup-1M46J15H39XJI:policyName/awseb-e-jy5ezr9t8i-stack-AWSEBAutoScalingScaleUpPolicy-ERY62MWUT7UT
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:054060359478:scalingPolicy:c0874ffa-6077-43ce-8651-f3c0b385443d:autoScalingGroupName/awseb-e-jy5ezr9t8i-stack-AWSEBAutoScalingGroup-1M46J15H39XJI:policyName/awseb-e-jy5ezr9t8i-stack-AWSEBAutoScalingScaleDownPolicy-BBEXPPILSMFH
INFO: Created CloudWatch alarm named: awseb-e-jy5ezr9t8i-stack-AWSEBCloudwatchAlarmLow-1W91IUM9H34U7
INFO: Created CloudWatch alarm named: awseb-e-jy5ezr9t8i-stack-AWSEBCloudwatchAlarmHigh-FGZXODRR2VOF
INFO: Added EC2 instance ‘i-3c0d32eb’ to Auto Scaling Group ‘awseb-e-jy5ezr9t8i-stack-AWSEBAutoScalingGroup-1M46J15H39XJI’.
INFO: Application available at sinata-test-dev.elasticbeanstalk.com.
INFO: Successfully launched environment: sinata-test-dev

Alright your app is deployed. From now on when you want to deploy again all you have to do is run eb deploy.

Once this command completes, try typing eb status – it should give you the URL to your running app (remember to add /hi to your URL when you check it in your browser):

Environment details for: sinata-test-dev
Application name: sinata-test
Region: us-east-1
Deployed Version: 9af9
Environment ID: e-jy5ezr9t8i
Platform: 64bit Amazon Linux 2015.03 v1.3.0 running Ruby 2.2 (Puma)
Tier: WebServer-Standard-
CNAME: sinata-test-dev.elasticbeanstalk.com
Updated: 2015-04-16 03:34:47.029000+00:00
Status: Ready
Health: Green

#protip – go to http://jon-test-eb-env-znf3pnhjvf.elasticbeanstalk.com/hi to see my running tutorial app on Elastic Beanstalk

Congratulations, you’ve deployed your hack to Elastic Bean Stalk! EBS is extremely configurable, so I would highly recommend that inquiring minds head on over to the documentation to dig deeper into what is possible.

Happy hacking, see you at your next hackathon!

Jon Gottfried