Vagrant with chef in AWS

Introduction:

Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increase development/production parity, and makes the “works on my machine” excuse a relic of the past

Requirements:

  • Local Machine running Windows
  • Vagrant Installation file
  • AWS Instance
  • Chef Development Kit
  • IAM User with secret keys and access ID

Installation:

  • On your local machine download and install the Chef DK as discussed above in this document.
  • Download the Vagrant installation file and run it from “https://www.vagrantup.com/downloads.html
  • Verify the installation using the following “vagrant –version” which displays the installed Vagrant version.
  • Now install the Chef DK which is discussed above.
  • When the installation is done, open the Chef DK terminal and verify the download by hitting “git –version” and “ssh” which must display a version number for GIT and a usage dialogue for ssh which looks like below.
  • chef3
  • If you’re using an Oracle Virtual Box instead AWS check the below link https://learn.chef.io/tutorials/learn-the-basics/ubuntu/virtualbox/set-up-a-machine-to-manage/
  • Now from the Chef DK, install the Vagrant AWS Plugin using the following command “vagrant plugin install vagrant-aws”
  • Now download the dummy Vagrant Box using “vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
  • After this, create a directory and within the directory, issue the command “vagrant init” which create a Vagrantfile in the directory.
  • Add the following lines in the Vagrantfile just created:
  • # Vagrantfile
  • configure(2) do |config|
  • vm.provider :aws do |aws, override|
  • access_key_id = ENV[‘AWS_KEY’]
  • secret_access_key = ENV[‘AWS_SECRET’]
  • keypair_name = ENV[‘AWS_KEYNAME’]
  • ami = “ami-a7fdfee2”
  • region = “us-west-1”
  • instance_type = “t2.micro”
  • vm.box = “dummy”
  • ssh.username = “ubuntu”
  • ssh.private_key_path = ENV[‘AWS_KEYPATH’]
  • end
  • Make sure that you change the values in AWS access key ID, secret access key, keypair name of the user you created in your AWS IAM and change the AMI, AWS region, AWS Instance type as per your requirement. Also, change the SSH private key path of your AWS Instance which is stored locally.
  • Creating a AWS IAM user will be discussed below shortly.
  • Give the IAM user full access on EC2.
  • Now you should have prepared everything that can spin up an Instance with a single command “vagrant up –provider=aws”
  • Wait for the process to run and hopefully you should see a Vagrant Instance gets launched in your AWS EC2 Console with provided inputs.
  • You can login to the AWS instance using Putty or from your Chef DK.
  • To login from Chef DK use the following command “vagrant ssh”
  • Now you’re logged into the machine which launched just now using Vagrant.

Creating a IAM User:

  • Login to your AWS Management Console and navigate to AWS Services and select IAM.
  • In this pane choose Users and Add Users.
  • Type the user name for the new user. This is the sign-in name for AWS.
  • Check the Programmatic access, AWS Management Console Access and choose custom password
  • On the next page add permissions and proceed to Create User.
  • Download the user credentials and keep them safe.
  • Now go to Polices and navigate to the Attached Entities
  • Select the user which you just created and attach a policy AWS EC2 Full Access for the user and save.
  • Give the AWS Access Key ID and Secret Access Key which you downloaded in the Vagrantfile script.

Chef with Vagrant (Unattended Installation of Chef-Client and Bootstrapping):

Agenda:

To Install Chef-Client on the node in AWS and Bootstrap the same node to Chef Server unattended installation using Vagrant

Requirements:

  • Vagrantfile
  • AWS
  • Chef DK

How to:

  • You have the following ready Chef DK, Vagrantfile and AWS Management Console.
  • Using the Vagrantfile add the following to the script which you already used before.
  • chef4.JPG
  • Insert the values as per your requirements.
  • Save the Vagrantfile.
  • From Chef DK, hit the following command “vagrant up” to launch the machine in AWS.
  • This will spin an instance in AWS and installs Chef-Client on the node machine.

Bootstrapping the Node to Chef Server:

  • In the Vagrantfile, add the following script
  • chef5.JPG
  • Change the values as per yours and save the file.
  • Now from your Chef DK, again hit “vagrant up” which will create a node “vagrant” in your AWS EC2 Console, installs Chef Client in that node and bootstraps the same to Chef Server.
  • So, your complete code should like this
  • chef6.JPG
  • Remember to change the values and you’re done.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s