Chef setup from Scratch

Launch a new instance for Chef Workstation

Go To :

Click “Get Chef”


Fill up the form :

formchefLogin Credentials :  unni  / unni123456

Login Chef Console

consolechefNow the Chef Workstation Setup :


Current OS – Ubuntu 12 and will use chef omnibus installer for quick setup.

curl -L | sudo bash

echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile

Login to the Management Console ->
Select the Organization - minjarpoc
On Left pane select "Starter Kit" (The link only becomes active after selecting your organization.)
On right window click "Set up your Workstation"

 Click "Download Starter Kit"
scp -i sshkey.pem Downloads/  ubuntu@<ip>:/home/ubuntu/
ssh <ip>
sudo su -
mv /home/ubuntu/ .
apt-get update
apt-get install unzip
cd chef-repo
#knife client list
[knife is a command-line tool that provides an interface between a local Chef repository and the Chef Server.]
apt-get install git
$ git init .
$ git add .
$ git commit -m "Initial commit"

Download the apt and apache2 community cookbooks using knife. In a terminal:
$ knife cookbook site install apt
$ knife cookbook site install apache2

Steps in creating a Simple Cookbook from Chef Docs
knife cookbook site install magic_shell
Creating a New Cookbook
knife cookbook create aliases
vim cookbooks/aliases/metadata.rb
Add the following line:
depends 'magic_shell'

If you're not familiar with Chef, this allows us to leverage LWRP (light weight resource provider) functionality of the magic_shellcookbook inside our aliases cookbook (allowing us to create aliases on our node).
vim chef-repo/cookbooks/aliases/recipes/default.rb
# Alias `h` to go home
magic_shell_alias 'h' do
  command 'cd ~'

# Alias `sites` to cd into apache
magic_shell_alias 'sites' do
  command "cd #{node['apache']['dir']}/sites-enabled"

# Set Nano as the default editor
magic_shell_environment 'EDITOR' do
  value 'nano'
Upload the Cookbooks

In order for our nodes to download these cookbooks, we need to upload them to the Enterprise Chef Server using knife. In a terminal:

$ knife cookbook upload --all

Our workstation is now set up. We now have:

  • Credentials setup and verified
  • Our chef-repo repository setup
  • The following recipes to use on a new node:
  1. recipe[apt] – via community
  2. recipe[apache2] – via community
  3. recipe[magic_shell] – via community
  4. recipe[aliases] – created ourselves

Converge to EC2 Node

Pre-requisite steps:

  1. Register for an Amazon AWS Account
  2. Setup and download your EC2 Key Pair
  3. Create an Access Key and Associated Secret Token on AWS
  4. Allow port 22 access on your EC2 security group

Existing Contents of chef-repo/.chef is

  • minjarpoc-validator.pem
  • unni.pem
  • knife.rb

Add the following lines to knife.rb file:

knife[:aws_access_key_id] = 'ACCESS_KEY_ID'
knife[:aws_ssh_key_id] = 'KEY_ID'
knife[:aws_secret_access_key] = 'SECRET_ACCESS_KEY'
#IMPORTANT NOTE : The aws_ssh_key_id is the name of your EC2 Key Pair.

apt-get install libxml2 libxml2-dev gcc build-essentials

To install the knife ec2 plugin using RubyGems, run the following command:
$ /opt/chef/embedded/bin/gem install knife-ec2

where /opt/chef/embedded/bin/ is the path to the location where the chef-client expects Knife plugins to be located. If the chef-client was installed using RubyGems, omit the path in the previous example.

Documentation with Knife EC2 Plugin :

Create a Role and add apache to Runlist from the Chef Management Console

Role = testrole AMI = ami-8e987ef9 #knife ec2 server create -r “role[testrole]” -I ami-8e987ef9 –flavor m1.small -G unni-test-sg -x ubuntu -N MinjarMcAfee2 –region eu-west-1 -i /root/unni-minjarteam-ireland.pem

For testing purpose we can use apache cookbook. If using apache cookbook it will have an empty site-enabled directory hence we have to modify cookbook or after executing chef-client on node configure apache to have site-enabled file and restart apache.


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s