Otto Makes Development and Deployment a Breeze

Share this article

otto

Otto is an open source tool for creating virtual development environments for your application. It is the successor to the popular virtual development tool vagrant. Both of the projects are created by Hashicorp.

Virtual development environments abstract away the application’s environment from the underlying operating system. This will ensure the application’s consistency across most/all platforms and eliminating platform-based issues from creeping in. Otto makes it easy to create such development environments and can help set up the same environment on production servers, too.

Today we will see how to deploy a simple database-backed Rails application to AWS using Otto. Start by installing Otto in your machine if it’s not there already.

Installing Otto

Installing Otto is quite straightforward. There are packaged binaries already available for Windows, Mac, and popular distributions of Linux. Follow the steps provided on this page to install and configure Otto.

Before proceeding, verify Otto is properly installed on your machine by typing the following command in your terminal:

otto version

If you see a Command not available error, check if the binary is properly added to your system path or try in a fresh terminal session.

Setting Up the Rails App

Let’s create the Rails application now. Enter the following command in your terminal:

rails new otto-example -d postgresql

CD into the directory and create a file named Appfile at the root of the directory and add the contents below to it. Appfile is used by Otto to configure application-specific configurations, such as dependencies.

application {
  name = "otto-example"
  type = "ruby"
  dependency {
    source = "github.com/cromulus/otto-postgres"
  }
}

Run the following command to compile the application:

otto compile

Even without an Appfile, Otto automatically detects it’s a Rails application, figures out the dependency (PostgreSQL, in our case), and compiles the application. But it’s always a good practice to be explicit. Now fire up the development environment:

otto dev

If there is no Vagrant installed on your machine, this command will install Vagrant, VirtualBox, and other tools necessary for your application and Otto to run. These would be quite a big download and takes some time for the entire process to complete. However, this is a one time process, so it’s worth it.

If you see the message Otto can't install Vagrant automatically, head over to the Vagrant downloads page and install the respective package for your operating system.

If you see an error message with Vagrant failed with status code 1, try installing VirtualBox manually from their downloads page.

If you see any error message similar to “could not connect to server”, try ssh-ing into the dev environment using otto dev ssh and check if Postgres is already installed by typing psql into command line. If not, install it with the following commands:

. /etc/default/locale
sudo apt-get install -y postgresql-9.1
sudo -u postgres createuser --superuser vagrant

and run the rake commands to setup the database:

rake db:create

With that, the database is created for our application. As mentioned above, you can get into the development environment using:

otto dev ssh

Let’s quickly create a scaffold for our Rails application:

rails generate scaffold post title body:text
rake db:migrate

You can run these commands either outside of the environment or inside it. The files are automatically synced. To verify everything is working correctly, start the Rack app inside the Otto environment:

rackup --host 0.0.0.0

In another terminal, get the virtual machine’s IP by running the following command from inside the project directory:

otto dev address

The output for the above command is an IPV4 address. Go to http://<IP-ADDRESS>:9292 and, if everything works, you’ll see the Rails default page.

Building the App

We’ll now see how to build our application to make it ready for AWS.

Otto has a concept called infrastructure which refers to the target cloud platform where we want to deploy our application. In this tutorial, our infrastructure is AWS. For each infrastructure, there are multiple flavors with varying configurations. For AWS, there are two flavors readily available – simple and vpc-public-private. The simple flavor is for, um, simple applications and focuses on cost, sacrificing scalability. The vpc-public-private option, on the other hand, sets up other resources, like NAT instances, for future scalability. You can specify your flavor in the Appfile and, if not explicitly set, simple is taken as default.

Let’s begin building the app by typing the below command into the terminal:

otto infra

This command configures the cloud environment as per the specified flavor. This takes care of building all the necessary modules, such a VPC subnets, Router gateways, provisioning instances and the like. During this step, Otto will ask for your AWS Access Key and Secret which you can procure from here if you already have an account. If you don’t, you can sign up for a free account which gives a free tier for first 12 months that you can use to experiment with this tutorial.

Once you enter the AWS credentials, Otto will ask for confirmation to install Terraform. Type ‘yes’ and continue with the process. Terraform, put simply, is a configuration management tool for environments, also from the fine folks at Hashicorp.

Type otto status to verify everything went well. If you see the Infrastructure status is green, that means we have successfully configured the infrastructure for our application. Let’s build the application and deploy it.

We’ll now build the application. Create an Amazon Machine Image (AMI), which is a disk image recognized by AWS. You can also build a Docker container, if you like, using this step. To begin building the application, type the below command into the terminal:

otto build

It will ask for your password that you entered after entering the Access Key and Access Secret in the configuring infrastructure step. Enter the password and type ‘yes’ if it asks to install Packer. Packer is yet another Hashicorp tool that creates container and machine images for multiple destinations (like AWS or Digital Ocean) from a configuration.

If you see an error message like this, try initializing the repository in git and committing the changes:

Error building app: error getting git commit: exit status 128
stdout:
stderr: fatal: bad default revision 'HEAD'

The build process can take a few minutes. Once it’s done, verify the build is complete by typing:

otto status

you should see the build status in green.

Deploying the App

This is the final step of the tutorial. Let’s deploy our custom-built AMI into the configured AWS infrastructure. Type the below command into the terminal:

otto deploy

Otto will take the AMI we built and launch a new server in our infrastructure, then deploy our AMI there. Once the deployment is done, you should see the IP address of the server, open that in your browser and you can see our web application running on AWS. In case you want to re-visit the IP address of the deployment, you can type

otto deploy info

into the terminal to get the deployment information.

Conclusion

With that step, we have come to the conclusion of this tutorial. Otto, as exhibited here, is so simple yet so powerful. I recommend going through their documentation and make the most out of it. The people at Hashicorp are shining examples of why open source is a great thing, and Otto is a very compelling entry into environment manager forum. Check out Otto and let me know how you’re using it.

Vinoth is a Server Administrator turned Full stack web developer. He loves to try his hands on multiple programming languages but his primary programming language of choice is Ruby. He is currently a Software Engineer @ Intelllex building the server side of things. You can find more about him at avinoth.com.

GlennGhashicorp
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week