How to Configure AWS EC2 Instance Using Vagrant

Reading Time: 3 minutes

In this blog I am going to explain how simply we can configuring AWS EC2 Instance Using Vagrant.Before going forward first lets understand what is Vagrant.Lets Start!!

What is Vagrant?

Vagrant is a tool for working with virtual environments, and in most circumstances, this means working with virtual machines. It provides a simple and easy to use command-line client for managing these environments, and an interpreter for the text-based definitions of what each environment looks like, called Vagrantfile. Vagrant is open source, which means that anyone can download it, modify it, and share it freely.

Use case:

A powerful usage of Vagrant can be with remote IaaS resources such as Amazon EC2. Amazon Web Services Elastic Compute Cloud (EC2) and similar Infrastructure-as-a-Service providers like Google Cloud, Azure or Digital Ocean.

If you want to learn more about what and why Vagrant checkout the following blog:

Know more About Vagrant

Steps to Configure AWS EC2

The first step in the process is to make sure that you have Vagrant installed on your machine.You could use providers by installing the respective plugins. For the purpose of this blog, I will download the vagrant-aws plugin through the following command:

$ vagrant plugin install vagrant-aws

You can verify that the plugin is installed or not by following command

$ vagrant plugin list

After installing the plugin the quickest way to get started is to actually use a dummy AWS box and specify all the details manually within a config.vm.provider block. So first, add the dummy box using any name you want.Run the following command to add the dummy box here I named it as dummy.

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

Now we are good to create an AWS EC2 Instance.For Configuring an EC2 Instance we will need a Vagrantfile.

What is Vagrantfile?

The Vagrantfile is a Ruby file used to configure Vagrant on a per-project basis. The main function of the Vagrantfile is to described the virtual machines required for a project as well as how to configure and provision these machines.We can can create Vagrantfile through manually or by using command vagrant init.When you run the command vagrant init it will automatically create a Vagrantfile.

I have created a Vagrantfile manually that looks like this:

require 'vagrant-aws'
Vagrant.configure('2') do |config|
    config.vm.box = 'dummy'
    config.vm.provider 'aws' do |aws, override|
    aws.access_key_id = ENV['AWS_ACCESS_KEY_ID']
    aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
    aws.keypair_name = '[your key pair name]'
    aws.instance_type = 't2.micro'
    aws.region = 'ap-south-1'
    aws.ami = 'ami-0851b76e8b1bce90b'
    aws.security_groups = '[your security group]'
    aws.subnet_id = '[your subnet-id]'
    override.ssh.username = 'ubuntu'
    override.ssh.private_key_path = '[path of your key-pair]'
  end
end

Now I am going to explain the things that I have written in Vagrantfile:

  • Firstly I have specify the box. Using dummy aws box for AWS deployment.
  • Then I have defined provider.
  • We need the AWS Access Keys, preferably from environment variables so you don’t hardcode them in your Vagrantfile.
  • I have Included the type of Instance .
  • After that Indicated the region and availability zone where you want the instance to start.
  • I have provided ami-id.
  • Indicated in which security group this instance will live as well as defined subnet-id to boot the instance into, for VPC.
  • At the end I have provided ssh key-pair path.

If you want to tag the instances, You can add the following to the Vagrantfile.

aws.tags = {
'Name' => 'Vagrant'
}

Launch The Instance

After configuring the Vagrantfile now run the follwing command to create Instance.

$ vagrant up

You can view the newly created instance in the AWS console.Also You can now SSH into your AWS instance or perform other Vagrant operations.If want to ssh just perform below command:

$ vagarnt ssh

Now you can see I have successfully ssh to my instance.Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs-ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.

Refrences:

https://www.vagrantup.com/docs

knoldus

Written by 

Jubair Ahmad is a Software Consultant (DevOps)at Knoldus.Inc.He loves learning new technology and also have interest in playing cricket.