Vagrant is a really powerful tool for managing virtual machines. If you’re a developer it can make it push-button simple to setup a dev box on your laptop. It manages the VMs, and uses configuration files to describe specifics of your machines. We can use VirtualBox, VMWare, AWS, DigitalOcean as provider to launch our servers and automatically provision it.

Prerequisites

I am assuming you have the latest version of Vagrant installed in your dev machine and familiar with Vagrantfile and its commands. If you don’t know about Vagrant, you can visit the official docummentation of Vagrant and learn about it.

You need to have a AWS account and the following

  • AWS access key
  • AWS secret key
  • SSH keypair name
  • SSH private key file (.pem extension)
  • Make sure the your security group enables SSH (port 22) access from anywhere

The EC2 instance used to deploy here may not fit in your AWS Free Tier Plan and cost your money.

Setting Environment Variable

I like to set these up as environment variables to keep them out of the Vagrantfile. On Mac or Linux systems you can add this to your $HOME/.profile or $HOME/.bashrc file:

export AWS_KEY='your-access-key'  
export AWS_SECRET='your-secret-secret'  
export AWS_KEYNAME='your-keyname'  
export AWS_KEYPATH='your-keypath'  
export AWS_DEFAULT_REGION='your-region'  

Install the vagrant-aws plugin

To install vagrant-aws plugin run the following command in terminal

vagrant plugin install vagrant-aws  

Fetch AWS Dummy Box

To fetch the Vagrant AWS dummy box run the following command in terminal

vagrant box add dummybox-aws https://github.com/mitchellh/vagrant-aw  

Creating Vagrantfile

Run the following commands in terminal to create a Vagrantfile

mkdir -p /path/to/your/dir && cd $_  
vagrant init dummybox-aws  

This will create a Vagrantfile in current directory using dummybox-aws that we added in previous step.

Configure Vagrantfile

Now our Vagrantfile with the specifics needed for AWS. Refer to the vagrant-aws documentation to understand all the options.

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrant on AWS Example
# Nur Mohammed Rony

# Adjustable settings
# timezone, like US/Pacific, US/Eastern, UTC, Europe/Warsaw, etc.
CFG_TZ = "US/Pacific"

# Provisioning script
provision_script = <<SCRIPT  
#!/bin/bash

# set timezone
echo "#{CFG_TZ}" > /etc/timezone  
dpkg-reconfigure -f noninteractive tzdata

# install a few base packages
apt-get update  
apt-get install vim curl git build-essential -y  
#long provisioning command
echo Provisioning is complete  
SCRIPT

Vagrant.configure(2) do |config|

  config.vm.box = "dummybox-aws"
  config.vm.synced_folder ".", "/home/ubuntu/devpc", type: "rsync", rsync__exclude: ".git/"
  config.vm.hostname = "stagingbox"
  config.vm.provision :shell, :inline => provision_script
  config.vm.provider :aws do |aws, override|

    #AWS Settings
    aws.access_key_id = ENV['AWS_KEY']
    aws.secret_access_key = ENV['AWS_KEYNAME']
    aws.region = ENV['AWS_DEFAULT_REGION']

    aws.tags = {
      'Name' => 'NurRony StagingBox',
      'Team' => 'devops',
      'Status' => 'active'
    }

    #Override Settings
    override.ssh.username = "ubuntu"
    override.ssh.private_key_path = ENV['AWS_KEYPATH']

    aws.region_config "us-west-2" do |region|
      region.ami = 'ami-35143705'
      region.instance_type = 'c4.large'
      region.keypair_name = ENV['AWS_KEYNAME']
      region.security_groups = "devops"
    end
  end
end  

Add or edit more options according to your need.

Startup Vagrant box

Run the following command to start vagrant box which will create and run an AWS EC2 instance with your provided settings in previous step

vagrant up —-provider=aws  

Verify in the Amazon dashboard

Jump over to your amazon dashboard with this link. If you’re logged in already, that will take you to your EC2 instances. You should see a new one, based on the parameters in your Vagrantfile.

Working with newly created EC2 instance

You can connect via SSH, stop, reload or re-provision your newly created EC2 instance with regular Vagrant command like below

#To SSH to instance
vagrant ssh  
#To restart the instance or re-provision
vagrant reload reload [--provision]  
#To stop 
vagrant halt  

Note

  • You need to configure a specific AMI for Vagrant to use. I find the Ubuntu Amazon EC2 AMI Finder very helpful to match the version and region I wanted to use.
  • A common tripping point is the default security group not allowing SSH (port 22) from any IP address. Also make sure to add any other ports depending on your application (e.g., port 80 for HTTP).
  • Once you have the basics working, make sure to read through the vagrant-aws project to understand all the options available.
  • Make sure to vagrant destroy your VMs when done, and check the AWS Console to make sure they were terminated correctly (to avoid unexpected charges). Good Luck!!! If you face any problem feel free to poke me.

© 2016. All Rights Reserved.

Proudly published with Ghost