Using Vagrant to install an EC2 instance

Let’s test Vagrant to deploy an EC2 instance to AWS. For this, the first thing we need is to install the aws plugin for Vagrant. My Vagrant instance currently just has the Virtualbox plugin.

To install the aws plugin, use the command #vagrant plugin install vagrant-aws

ayans-mbp:Desktop ayan$ vagrant plugin install vagrant-aws
Installing the 'vagrant-aws' plugin. This can take a few minutes...
Fetching: excon-0.62.0.gem (100%)
Fetching: formatador-0.2.5.gem (100%)
Fetching: fog-core-1.45.0.gem (100%)
Fetching: fog-json-1.2.0.gem (100%)
Fetching: mini_portile2-2.4.0.gem (100%)
Fetching: nokogiri-1.10.0.gem (100%)
Building native extensions.  This could take a while...
Fetching: fog-xml-0.1.3.gem (100%)
Fetching: ipaddress-0.8.3.gem (100%)
Fetching: xml-simple-1.1.5.gem (100%)
Fetching: fog-aliyun-0.3.2.gem (100%)
Fetching: fog-atmos-0.1.0.gem (100%)
Fetching: fog-aws-2.0.1.gem (100%)
Fetching: dry-inflector-0.1.2.gem (100%)
Fetching: fog-brightbox-0.16.1.gem (100%)
Fetching: fog-cloudatcost-0.1.2.gem (100%)
Fetching: fog-digitalocean-0.4.0.gem (100%)
Fetching: fog-dnsimple-1.0.0.gem (100%)
Fetching: fog-dynect-0.0.3.gem (100%)
Fetching: fog-ecloud-0.3.0.gem (100%)
Fetching: fog-google-0.1.0.gem (100%)
Fetching: fog-internet-archive-0.0.1.gem (100%)
Fetching: fog-joyent-0.0.1.gem (100%)
Fetching: fog-local-0.6.0.gem (100%)
Fetching: fog-openstack-0.3.9.gem (100%)
Fetching: ovirt-engine-sdk-4.2.5.gem (100%)
Building native extensions.  This could take a while...
Fetching: rbovirt-0.1.7.gem (100%)
Fetching: fog-ovirt-1.1.4.gem (100%)
Fetching: fog-powerdns-0.2.0.gem (100%)
Fetching: fog-profitbricks-4.1.1.gem (100%)
Fetching: fog-rackspace-0.1.6.gem (100%)
Fetching: fog-radosgw-0.0.5.gem (100%)
Fetching: fog-riakcs-0.1.0.gem (100%)
Fetching: fog-sakuracloud-1.7.5.gem (100%)
Fetching: fog-serverlove-0.1.2.gem (100%)
Fetching: fog-softlayer-1.1.4.gem (100%)
Fetching: fog-storm_on_demand-0.1.1.gem (100%)
Fetching: fog-terremark-0.1.0.gem (100%)
Fetching: CFPropertyList-2.3.6.gem (100%)
Fetching: fission-0.5.0.gem (100%)
Fetching: fog-vmfusion-0.1.0.gem (100%)
Fetching: fog-voxel-0.1.0.gem (100%)
Fetching: trollop-2.9.9.gem (100%)
!    The 'trollop' gem has been deprecated and has been replaced by 'optimist'.
!    See: https://rubygems.org/gems/optimist
!    And: https://github.com/ManageIQ/optimist
Fetching: rbvmomi-1.13.0.gem (100%)
Fetching: fog-vsphere-2.5.0.gem (100%)
Fetching: xmlrpc-0.3.0.gem (100%)
Fetching: fog-xenserver-1.0.0.gem (100%)
Fetching: fog-1.42.1.gem (100%)
------------------------------
Thank you for installing fog!

IMPORTANT NOTICE:
If there's a metagem available for your cloud provider, e.g. `fog-aws`,
you should be using it instead of requiring the full fog collection to avoid
unnecessary dependencies.

'fog' should be required explicitly only if:
- The provider you use doesn't yet have a metagem available.
- You require Ruby 1.9.3 support.
------------------------------
Fetching: iniparse-1.4.4.gem (100%)
Fetching: vagrant-aws-0.7.2.gem (100%)
Installed the plugin 'vagrant-aws (0.7.2)'!

Now, Vagrant has public boxes that can be used – these are available at https://app.vagrantup.com/boxes/search But for this, the easiest would be to use the dummy box available at https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

Now, let’s create a directory for the Vagrant file

$mkdir aws-ec2-test

Enter the directory and add the box to Vagrant

$ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'dummy' (v0) for provider: 
    box: Downloading: https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
    box: Download redirected to host: raw.githubusercontent.com
==> box: Successfully added box 'dummy' (v0) for 'aws'!

What happens if you get an error that the box already exists? Well, you could either delete the box or just overwrite

ayans-mbp:aws-ec2-test ayan$ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'dummy' (v0) for provider: 
    box: Downloading: https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
    box: Download redirected to host: raw.githubusercontent.com
The box you're attempting to add already exists. Remove it before
adding it again or add it with the `--force` flag.

Name: dummy
Provider: aws
Version: 0

So just use the –force

ayans-mbp:aws-ec2-test ayan$ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box --force
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'dummy' (v0) for provider: 
    box: Downloading: https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
    box: Download redirected to host: raw.githubusercontent.com
==> box: Successfully added box 'dummy' (v0) for 'aws'!

Or just list the box and destroy

ayans-mbp:aws-ec2-test ayan$ vagrant box list
dummy               (aws, 0)
hashicorp/precise64 (virtualbox, 1.1.0)

To destroy/remove

ayans-mbp:aws-ec2-test ayan$ vagrant box remove dummy
Removing box 'dummy' (v0) with provider 'aws'...

List to ensure the box has been removed

ayans-mbp:aws-ec2-test ayan$ vagrant box list
hashicorp/precise64 (virtualbox, 1.1.0)

Now, on doing a vagrant init, a Vagrant file is created in the directory which will need to be modified based on our configuration

$vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Let’s now cat the Vagrant file to check and modify the configuration

ayans-mbp:aws-ec2-test ayan$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "base"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end
ayans-mbp:aws-ec2-test ayan$ 

At this point, change the Vagrant file to suit your deployment examples are below

https://github.com/mitchellh/vagrant-aws/blob/master/example_box/Vagrantfile

https://blog.scottlowe.org/2016/09/15/using-vagrant-with-aws/

My configuration looks like below

require 'vagrant-aws'
Vagrant.configure('2') do |config|
    config.vm.box = 'dummy'
    config.vm.provider 'aws' do |aws, override|
    aws.access_key_id = “xxxxxxxxxxxxxxxxxxxxxxxxxxx”
    aws.secret_access_key = “xxxxxxxxxxxxxxxxxxxxxxxxxx”
    aws.keypair_name = 'ayan-ssh-keypair'
    aws.instance_type = "t2.micro”
    aws.region = 'us-west-2'
    aws.ami = 'ami-b6947fd6'
    aws.security_groups = ['default']
    override.ssh.username = 'ubuntu'
    override.ssh.private_key_path = '~/.ssh/ssh-keypair-file'
  end
end

The list of Ubuntu AMI’s can be found here

https://cloud-images.ubuntu.com/locator/ec2/

To bring up the EC2 instance

$vagrant up --provider=aws

For logging into the instance

$vagrant ssh

Stay tuned for more

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous post EC2 on AWS
Kitematic install error Next post Resolving the Kitematic install error