Devops: Vagrant with AWS EC2 & Digital Ocean

Following on from my recent DevOps blog posts, The DevOps Tools We User & How We Use Them and Vagrant with Chef-Server, we will take another step forward and look into provisioning our servers in the cloud. There are many cloud providers out there, most who provide some sort of api. Dealing with the different apis and scripts can become cumbersome and confusing when your main focus is a fault tolerant, scalable system. This is where Vagrant and many of its plugins shine.

Vagrant has a wide range of plugins, from handling Chef and Puppet to provisioning servers on many different cloud providers. We’re going to focus on 3 plugins in specific: vagrant-aws, vagrant-digitalocean and vagrant-omnibus. The AWS and Digital Ocean plugins allow us to utilize both our Chef-server and the public infrastructure provided by both Amazon and Digital Ocean. The omnibus is used for installing a specified version of Chef on your servers.

Vagrant with AWS EC2 & Digital Ocean

To install the Vagrant plugins, run the following commands:

vagrant plugin install vagrant-aws
vagrant plugin install vagrant-digitalocean
vagrant plugin install vagrant-omnibus

Running Vagrant plugin list should give you the following output:

vagrant-aws (0.4.1)
vagrant-digitalocean (0.5.3)
vagrant-omnibus (1.3.1)

More information on the plugins can be found here:
https://github.com/mitchellh/vagrant-aws
https://github.com/smdahlen/vagrant-digitalocean
https://github.com/schisamo/vagrant-omnibus

Amazon AWS

The first cloud provider we will look at is Amazon AWS. If you don’t already have an account I suggest you sign up for one here, and have a quick read of their EC2_GetStarted docs. Once signed up you will need to generate an account access and secret key, to do this follow the instruction below:

  1. Go to the IAM console.
  2. From the navigation menu, click Users.
  3. Select your IAM user name.
  4. Click User Actions, and then click Manage Access Keys.
  5. Click Create Access Key.
  6. Your keys will look something like this:
    1. Access key ID example: ABCDEF0123456789ABCD
    2. Secret access key example: abcdef0123456/789ABCD/EF0123456789abcdef
  7. Click Download Credentials, and store the keys in a secure location.

Now we have everything to get started. Again we will create a Vagrant file similar to what was done in my last blog post DevOps: Vagrant with Chef-Server, however, this time we will omit a few things like config.vm.box and config.vm.boxurl. The reason for this is that we are going to point our Vagrant file to use an Amazon ami instead.

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_plugin 'vagrant-aws'
Vagrant.require_plugin 'vagrant-omnibus'
Vagrant.configure("2") do |config|
config.omnibus.chef_version = :latest
config.vm.synced_folder '.', '/vagrant', :disabled => true
config.vm.box = "dummy"
config.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
# Provider
config.vm.provider :aws do |aws, override|
aws.access_key_id = "ABCDEF0123456789ABCD"
aws.secret_access_key = "abcdef0123456/789ABCD/EF0123456789abcdef"
aws.keypair_name = "awskey"
aws.ami = "ami-9de416ea" #Ubuntu 12.04 LTS
aws.region = "eu-west-1"
aws.instance_type = "t1.micro"
aws.security_groups = ["default"]
override.ssh.username = "ubuntu"
override.ssh.private_key_path = "path/to/your/awskey.pem"
aws.tags = {
'Name' => 'Java'
}
end
# Provisioning
config.vm.provision :chef_client do |chef|
chef.chef_server_url = 'https://api.opscode.com/organizations/logentries'
chef.validation_key_path = '../../.chef/logentries-validator.pem'
chef.validation_client_name = 'logentries-validator'
chef.log_level = 'info'
chef.add_recipe 'java_wrapper'
end
end

You should replace the above bold/italics options with your own settings.
The only difference this time in running Vagrant is that you need to pass a provider argument, so your command should look like this:

vagrant up --provider=aws

Once your Chef run has completed you will have a new instance in Amazon running your specified version of Java. Your output should look similar to this:

root@ip-172-31-28-193:~# java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
Logentries_Try_It_Free_Promo_W

Digital Ocean

Another cloud provider which has been gaining a lot of attention lately is Digital Ocean. If you don’t already have a Digital Ocean account you can sign up for here. Also have a look at their getting getting started guide if you’re new to Digital Ocean. The main difference between this Vagrant file and the AWS Vagrant file is that the provider block is different.

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_plugin 'vagrant-digitalocean'
Vagrant.require_plugin 'vagrant-omnibus'
Vagrant.configure('2') do |config|
config.vm.provider :digital_ocean do |provider, override|
provider.client_id = 'abcdef0123456789ABCDEF'
provider.api_key = 'abcdef0123456789abcdef0123456789'
provider.image = 'Ubuntu 12.04.3 x64'
provider.region = 'Amsterdam 1'
provider.size = '512MB'
provider.ssh_key_name = 'KeyName'
override.ssh.private_key_path = '/path/to/your/key'
override.vm.box = 'digital_ocean'
override.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"
provider.ca_path = "/usr/local/opt/curl-ca-bundle/share/ca-bundle.crt"
end
config.vm.provision :chef_client do |chef|
chef.chef_server_url = 'https://api.opscode.com/organizations/logentries'
chef.validation_key_path = '../../.chef/logentries-validator.pem'
chef.validation_client_name = 'logentries-validator'
chef.log_level = 'info'
chef.node_name = 'do-java'
chef.add_recipe 'java_wrapper'
end
end

You should replace the above bold/italics options with your own settings. This time when we run Vagrantup we will pass a provider parameter of digital_ocean

vagrant up --provider=digital_ocean

Once the Vagrant run has complete, SSHing into your server and running java -version should give you the following output.

root@do-java:~# java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

If you do run into issues it might be that your cookbooks are out of date, try running the following

librarian-chef update
knife cookbook upload -a

Now with the above scripts at your disposal you can use Chef-server with Vagrant and as many different cloud providers as you wish to keep your systems up to date, in sync and running to keep your customers happy!









Posted in AWS, Cloud, DevOps, How To

Leave a Reply