profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Tuesday, April 3, 2018

Deploying Vagrant VM on AWS

Let us first make sure we have the vagrant aws plugin ready.

vagrant plugin install vagrant-aws
Create a new directory.
get inside that directory
Now let us add a dummy box to AWS made just for this.
Create a Vagrantfile by running vagrant init.

mkdir lab_aws 
cd lab_aws
vagrant box add aws-dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
vagrant init
Then open the Vagrantfile with any text editor and populate it with the following.

# Install the below plugin with 'vagrant plugin install vagrant-aws'
require 'vagrant-aws'

# VM config
Vagrant.configure('2') do |config|

  # dummy AWS box https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
  config.vm.box = 'awsDummy'
  # settings from aws
  config.vm.provider :aws do |aws, override|
    # aws credentials
    aws.access_key_id = 'xxxxxxxxxxxxxxxxxxxx'
    aws.secret_access_key = 'yyyyyyyyyyyyyyy'
    aws.keypair_name = "vagrant" # ssh key pair name
    aws.ami = 'ami-5cd4a126'
    aws.region = 'us-east-1'
    aws.instance_type = 't2.micro'
    aws.security_groups = "vagrant" # enabled ssh ports in/out
    
    # the below line will help you avoid asking for username and password for smb if you are doing this from windows
    config.vm.synced_folder ".", "/vagrant", disabled: true
    override.ssh.username = 'vagrant'
    override.ssh.private_key_path = '<path>/vagrant.pem'
  end

end

So the above are the entries for your new Vagrantfile.
I chose the region 'us-east-1'
I also created a security policy group "vagrant" and enabled ssh on it. Make sure this security group is created on your chosen region in the vagrantfile. Also, make sure the ami ID that you have chosen is present in that region. I say to be safe just mimic your aws configuration to mirror this vagrantfile. Also, have your key pair download and saved somewhere. Give that path in the vagrantfille. Now just do a vagrant up and your VM will be deployed on AWS.