Vagrant and DigitalOcean (APIv2)

By | 03/10/2015

Introduction

In this tutorial we learned to very basics of Vagrant. Essentially, it was showing how to create virtual machines on top of VirtualBox. In today’s article, I wanted to show you how to do this with DigitalOcean. In other words, we will create a recipe to create a virtual machine running on DigitalOcean.

Installing the DigitalOcean plugin

Vagrant does not provide support for DigitalOcean out of the box, but is flexible enough for companies to develop their own plugins. Let’s install the Digitalocean plugin.

macbook-wim:Vagrant wim$ vagrant plugin install vagrant-digitalocean
Installing the 'vagrant-digitalocean' plugin. This can take a few minutes...
Installed the plugin 'vagrant-digitalocean (0.7.7)'!

Once this is done, we need to add the DigitalOcean box (the “image”) to vagrant.

macbook-wim:Vagrant wim$ vagrant box add digital_ocean https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'digital_ocean' (v0) for provider: 
    box: Downloading: https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box
==> box: Successfully added box 'digital_ocean' (v0) for 'digital_ocean'!

Go ahead and create a project folder, perform a ‘vagrant init’ (as lined out in the initial article ).

Before continuing, we need to create an SSH keypaor for authentication with DigitalOcean. Run the following command on your host station:

macbook-wim:digitalocean wim$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wim/.ssh/id_rsa): 
/Users/wim/.ssh/id_rsa already exists.
....

In order to find your token, login to your DigitalOcean account. Go to ‘API’, ‘Your tokens’ and click ‘Generate new token’ as indicated in the screenshot below:

DO_Token

Important note: Be careful with the token!! Anyone who has it can generate droplets on your DO account.

Your Vagrantfile should look as follows as per the instruction on GitHub for this plugin. Obviously, you are free to choose your image, region, size etc…

# -*- mode: ruby -*-
Vagrant.configure('2') do |config|

  config.vm.provider :digital_ocean do |provider, override|
    override.ssh.private_key_path = '~/.ssh/id_rsa'
    override.vm.box = 'digital_ocean'
    override.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"

    provider.token = ''
    provider.image = 'ubuntu-14-04-x64'
    provider.region = 'ams3'
    provider.size = '512mb'
  end
end

Then launch the virtual machine (DigitalOcean calls them droplets) using the following command:

macbook-wim:digitalocean wim$ vagrant up
Bringing machine 'default' up with 'digital_ocean' provider...
==> default: Using existing SSH key: Vagrant
==> default: Creating a new droplet...
==> default: Assigned IP address: 188.166.74.154
==> default: Rsyncing folder: /Users/wim/Dropbox/Technologies/Vagrant/digitalocean/ => /vagrant...

And here it is….fully up and running.
vagrant_do
Note: I was initially using ams1, but I got a note (when running ‘vagrant up’) that ams1 was not available. Indeed, when checking on the DO web app, ams1 is greyed out.

You should now be able to ssh into the droplet that was just created by using the ‘vagrant ssh’ command.