How Do You Get Vagrant Working on a Debian/Ubuntu Server in AWS?

Problem scenario
You are using Ubuntu 16.x in AWS.  You want to get Vagrant working to create VMs in AWS.  What do you do?

Solution
1.  Install Vagrant.  If you need assistance see this link.

2.  Install gem and various other dependencies.  To do this run these three commands:  

sudo apt-get -y update
sudo apt-get -y install ruby-dev libffi-dev build-essential dh-autoreconf
sudo gem install ffi -v 1.9.14

3.  Install the vagrant-aws plugin:  vagrant plugin install vagrant-aws

4.  Retrieve a Vagrant box image with this command:

vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

5.  Create a Vagrant file with these three commands:

mkdir contint-demo
cd contint-demo
vagrant init

6.  Go to the ~/.ssh directory for the user that will run Vagrant.

7.  Create a file with the same name as the AWS key pair you will use with Vagrant.  This should be an existing key pair with AWS used to connect to your VMs as specified in the last step when you manually launch a new EC2 instance.  Let's say this key pair is called "foobar".  You should create a file named foobar in the ~/.ssh/ directory.  The content of foobar should be these three lines (but replace YourAWSAccessKeyID and YourAWSSecretAccessKey accordingly):

[default]
AWSAccessKeyId=YourAWSAccessKeyID
AWSSecretKey=YourAWSSecretAccessKey

To find the AWS Access Key ID and AWS Secret Access Key, in the AWS console, click on your name in the upper right hand corner. Click on "My Security Credentials."  Click "Create New Access Key." Then click "Show Access Key."

8.  The Vagrantfile must be replaced. Here is a template with six values to replace.

require 'vagrant-aws'
Vagrant.configure('2') do |config|
    config.vm.box = 'dummy'
    config.vm.provider 'aws' do |aws, override|
    aws.access_key_id = "value1"
    aws.secret_access_key = "value2"
    aws.keypair_name = 'value3'
    aws.instance_type = "t2.nano"
    aws.region = 'value4'
    aws.ami = 'value5'
    aws.security_groups = ['value6']
    override.ssh.username = 'ubuntu'
    override.ssh.private_key_path = '~/.ssh/value3'
  end
end

Change value1, value2, value3, value4, value5 and value6.  You may also want to change "t2.nano" and "ubuntu".

value1 and value2 would be determined in step 7 above.

value3 is the AWS Key Pair name.  For the ASS key pair name, this is a reference to the key pair to log into the instance OS with.  The last step of creating a VM in AWS gives you the opportunity to select the key pair.  If you want to test your machine's ability to connect with the Access Key ID and Secret Access Key, you may want to install the AWS CLI (sudo apt install awscli).  You can then run "aws configure".  This can test your network access, ensure that the time on your machine is configured properly etc.  This way if there is a problem with Vagrant connecting, you know the fundamentals are in place.  One advantage of testing like this gives you the ability to see the key pairs (with "aws ec2 describe-key-pairs").

value4 is the region name (e.g., us-west-1).  

value5 is the AMI ID.  
For the AMI (or image) ID, you could log into AWS and start to launch a new instance.  The AMI ID is visible in the first step when selecting an image.  

For value6, it should be fairly obvious what to change the Security Group name to.  With the AWS Console you can determine the Security Group name.  

9.  Run this command:  vagrant up --provider=aws --debug

10.  This will create VMs in the region that you provided for value4 in step #8.  You will want to make sure these are destroyed after you run the command to not incur an expense.  You can destroy them from the Vagrant host with this command:  vagrant destroy default

Leave a comment

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