Vagrant: AWS (Launch single instance)

By | 23/10/2015

Introduction

In this post, we will get Vagrant working together with AWS instead of Virtualbox (as described here) or DigitalOcean (as described here). As in previous posts, we will use a shell script to install Apache on the EC2 instance automatically and run it as a webserver.

Installing AWS plugin

In order for Vagrant to work together with AWS, we need to install a plugin. The plugin can be found here

macbookpro:aws wim$ vagrant plugin install vagrant-aws
Installing the 'vagrant-aws' plugin. This can take a few minutes...
Installed the plugin 'vagrant-aws (0.6.0)'!

Downloading dummy box

macbookpro:aws wim$ 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: Successfully added box 'dummy' (v0) for 'aws'!

Creating Vagrantfile

Create a Vagrant file similar to the snippet below. Fill in your specific AWS credentials.

Vagrant.configure("2") do |config|
  config.vm.box = "dummy"

  config.vm.provider :aws do |aws, override|
    aws.access_key_id = "YOUR KEY"
    aws.secret_access_key = "YOUR SECRET KEY"
    aws.keypair_name = "KEYPAIR NAME"
    aws.security_groups = "default"
    aws.ami = "ami-7747d01e"

    override.ssh.username = "ec2-user"
    override.ssh.private_key_path = "PATH TO YOUR PRIVATE KEY ON LOCAL PC"
  end
end 

The access_key_id and secret_access_key can be found on your AWS console dashboard. There are a couple of ways to do this. I have used IAM to create a user and group and used these credentials. Most people by nature are lazy and they will use the root keys. These can be created and found by logging into your dashboard, click on your name (right corner), go to ‘Security Credentials’ and then open the ‘Access keys’ panel.

The keypair can be found in the EC2 dashboard and go to ‘Keypairs’. In case you have not created them, you can create them here and then download them to your local PC. The details can then be added to the Vagrantfile.

The security groups contains the list of all security group name;s you would want to use. To find your security group name, go to the EC2 dashboard and click security groups. Note that you should select a security group that allows SSH from your IP address. While you could set the ‘source’ for the rules to ‘anywhere’ (which means everybody can get access to the TCP or UDP port under discussion), it’s much safer to set it to ‘My IP’ which prevents random people to connect to your instance in case they should obtain the keypair.

The SSH username for the default Ubuntu images would be ‘ec2-user’. Note that if you use a different AMI, also the username could be different.

The AMI file is free to choose. I usually use an Ubuntu server. The easiest way to see what images are available is to launch an EC2 instance using the dashboard and it will present you with a list of available images. Also the AMI id is shown there.

When doing a simple ‘vagrant up’, you will see that an instance gets launched in the AWS dashboard. It will move from pending to ready state.

EC2_Vagrant

You could also see on your local PC’s console:

macbookpro:aws wim$ sudo vagrant up --provide=aws
Bringing machine 'default' up with 'aws' provider...
==> default: Warning! The AWS provider doesn't support any of the Vagrant
==> default: high-level network configurations (`config.vm.network`). They
==> default: will be silently ignored.
==> default: Launching an instance with the following settings...
==> default:  -- Type: m3.medium
==> default:  -- AMI: ami-69b9941e
==> default:  -- Region: eu-west-1
==> default:  -- Keypair: 
==> default:  -- Block Device Mapping: []
==> default:  -- Terminate On Shutdown: false
==> default:  -- Monitoring: false
==> default:  -- EBS optimized: false
==> default:  -- Assigning a public IP address in a VPC: false
==> default: Waiting for instance to become "ready"...
==> default: Waiting for SSH to become available...
==> default: Machine is booted and ready for use!

All set. In order to get access to the server, just do ‘vagrant ssh’ and you will see the something as follows:

Last login: Fri Oct 23 10:45:36 2015 from 173.38.220.41

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2015.09-release-notes/
No packages needed for security; 21 packages available
Run "sudo yum update" to apply all updates.
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[ec2-user@ip-10-11-158-86 ~]$