As developers, our utmost concern is mostly about the development part of any application and have it work on
localhost:3000. We don’t think much about the deployment to
PROD environments as we consider it to be a responsibility of the SysAdmins or Devops engineers. But many times, we don’t have a dedicated SysAdmin available to deploy every small commit we have made, so we have to put on the Devops hat and get things done and we will wear that cap using EB CLI in this tutorial. There are many options to deploy your application to an AWS EC2 server and one of them is the AWS EB CLI.
we’ll cover the following things:
- AWS EB CLI Features.
- How to create an EC2 Application/Server on AWS.
- How to setup EB CLI on your machine.
- EB CLI commands and how to use them.
- How to deploy a sample node application.
A Quick Premier on EB CLI
The EB CLI is a command line interface for Elastic Beanstalk that provides interactive commands that simplify creating, updating and monitoring environments from a local repositories and it also serves as an alternative to the AWS Management Console. The EB CLI is developed in Python and requires Python version 2.7, version 3.4, or newer.
BASIC AWS EB COMMANDS
|eb init||initializes eb in folder|
|eb create||creates a new environment|
|eb status||returns the state of your environment|
|eb logs||retrieves server logs for debugging|
|eb deploy||basic command to move your code to the server|
|eb open||open’s your environment’s website in a browser|
|eb ssh||logs in to the ec2 application selected during eb init|
How to create an EC2 Server on AWS.
Log in to the EC2 Management Console (obviously, you’ll need to sign up for an AWS account and do the necessary setup like those mentioned here. Click on the Services drop down at the top left hand corner of the page and click on Elastic Beanstalk a sub option under Compute like so;
NOW LET’S CREATE OUR WEB SERVER
Once you click on the Elastic Beanstalk link, locate the create new application button at the top right hand corner of the page and click on it.
Now follow the prompt that shows to setup your server. Don’t worry, we’ll go through the steps together :). Click on Create new Application button and enter the name of your new EC2 application like so;
After typing in your application name and optionally the description, you can proceed to the next step to create the environment. For this tutorial we will use a web server environment.
Node.js and the environment type to
load balancing and autoscaling ( if it is a public facing app). You can select the environment type that suits you based on the language used to build your application.
After selecting your environment type, click on next to set the application version. On the application version page, leave the default values with sample application selected as shown below.
The next page that loads basically shows your default application name and you can type in your preferred environment name which will be appended to your environment url. Click on check availability to ensure that the environment name you entered is unique to your aws account like so; Click on the next button afterwards.
On the next page, select the option that works for you. For my AWS setup, I create all additional resources inside my AWS Virtual Private Cloud (VPC)
Click on next to load the configuration details page. For my AWS account I use
dev/qa environments and
t2.small for production environments. Also select the key pair added to your account to allow developers
ssh into the server. Your screen should look like this;
You can skip the environment tag page and configure the VPC and security groups. Your screen should look like this depending on your account settings and configuration.
We’re almost done with our environment configuration, just set the permissions and review all your settings to ensure that no mistake was made. Your screen should look like this.
Finally, click on the launch button and wait patiently while your environment is created.
Once you see something like the image below, be rest assured that your application was successfully created and ready for deployment.
Setting up EB CLI on your machine
Up next, lets setup eb cli on our system. To setup eb cli, you need two major parameters ( Access Key ID and AWS Secret key). If you are the super admin user for the AWS account then you can generate yours, else you need to ask your CTO to provide it.
INSTALLATION OF EB CLI GLOBALLY
For MAC OSX users, you have to install Hombrew as your package manager if it’s not installed on your computer like so:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
After installing your package manager, follow this guide to install EB CLI for your operating system. After installing eb cli, verfy that it installed correctly by running;
$ eb --version // returns the current version of eb cli installed e.g EB CLI 3.7.6 (Python 2.7.1)
Great!! You have scaled through the first level. Now let’s clone the project and initialize EB in the project directory by running.
$ git clone https://github.com/amirrajan/word-finder.git dev-wordfinder-scotch //clones the git repo to your machine
$ cd dev-wordfinder-scotch // change directory into the clone projects
$ eb init // intializes elastic beanstalk in the cloned directory
Once you run eb init, you will get an output like:
Select the area that is closer to where your webapp will be mostly accessed. For me I selected 4, which is to closest region to me. Hit the enter key and you will be prompted to enter your
aws_secret_access_key as provide earlier. Hit your enter key and the next prompt will be the list of all applications created. You should see the most recent application you created eg mine is dev-wordfinder-scotch. Type in the corresponding number, which of course should be 1 and hit your enter key. To confirm that eb was setup correctly and your folder is now connected to the eb instance run;
$ eb status
You should get an output like this:
Install project dependencies by running
$ npm install // installs all project dependencies to your local repository
Test if your app is properly setup locally by ruuning
$ node start.js
The app will be hosted on port 3000 and you can access it from your browser like so;
Now you’re all set to deploy to the EC2 server we created ealier by simply running
$ eb deploy // this command bundles your app and uploads to an S3 bucket on AWS
Patietently wait for the deployment to be completed and run
eb open on your terminal to launch the ec2 url in your browser like so;
Once you run
eb open from your terminal, the ec2 url automatically opens on your browser.
AWS provides a feature called Route 53 which allows you to point a cleaner url that your users can remember to the ec2 url by creating a