Create a new Chef Cookbook and push to Github – Start Here!

Basic steps to get you started.

1) gem install knife-spec

More info about knife-spec – https://github.com/sethvargo/knife-spec

2) knife cookbook create my-cookbook

Run the above knife command. You will see that the basic cookbook template is created.

** Creating cookbook my-cookbook
** Creating README for cookbook: my-cookbook
** Creating CHANGELOG for cookbook: my-cookbook
** Creating metadata for cookbook: my-cookbook
** Creating specs for cookbook: my-cookbook

3) Create a new repo on your github

https://github.com/UserOrOrgName => Create a new repo.

You can select a Readme or License and make other settings to your new repo. Name the repo as “my-cookbook”

4) git clone https://github.com/UserOrOrgName/my-cookbook

5) Copy all your files created in step 2 to your new git repo

6) git add, git commit, git push

You have a basic template of your cookbook on git!

Note- This is a dummies guide to start with writing Chef Cookbooks. There are better ways to manage cookbooks – try out Berkshelf (berkshelf.com) !

Advertisements

Creating your first AWS instance using knife-ec2

To install knife-ec2, simply do a –

gem install knife-ec2

Assuming you have setup your Chef account on a hosted chef server. You will have a knife.rb which will look like:

# See http://docs.opscode.com/config_rb_knife.html for more information on knife configuration options
current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                “mynodename”
client_key               “#{current_dir}/mynodename.pem”
validation_client_name   “myorgname-validator”
validation_key           “#{current_dir}/myorgname-validator.pem”
chef_server_url          “https://api.opscode.com/organizations/myorgname”
cache_type               ‘BasicFile’
cache_options( :path => “#{ENV[‘HOME’]}/.chef/checksums” )
cookbook_path            [“#{current_dir}/../cookbooks”]

Update the knife.rb with your AWS account creds (access key) –

knife[:aws_access_key_id]     = “abcd”
knife[:aws_secret_access_key] = “xyz”

Also add this line to your knife.rb too:

knife[:aws_ssh_key_id] = “ec2keypair-name”

And simply run the command below –

knife ec2 server create -N chef-node-name -I ami-2d088144 -c /path/to/knife.rb –identity-file /path/to/ec2keypair.pem  -VV  –ssh-user ubuntu –bootstrap-protocol ssh