Monday, September 1, 2014

Using Chef to auto create an instance on Amazon ec2

Chef is one of the major server automation tools out there along with Puppet and Ansible. I decided to give chef a try by auto creating an ec2 instance. They're all trying to get you to buy saas packages for nodes greater than around 5. I will be using the free account offered by opscode to store my recipes.

1) Create a new chef-repo or use your current chef-repo

For testing I cloned a new chef-repo
git clone git:// ec2chefrepo

2) From the previous tutorial ./chef-repo copy over ./chef-repo/.chef into our new chef-repo

3) In Amazon Ec2 click on your name in the top right

I get a password old/new page. Instead go to the left side and click on Users.

Then click on your email and then the security credential tab

4) Get the Access Keys and Secret Keys

Click on Manage Access Keys
Create a new key and you will get two strings.
Note: The secret key is show and available this one time. You will need to create new access keys if the secret key is lost.

Access Key Id

Secret Access Key

5) With the above access keys and your ec2 .pem add these lines to ec2chefrepo/.chef/knife.rb

knife[:aws_access_key_id] = 'AKAJG6Z4AFQ5YPQ'
knife[:aws_ssh_key_id] = 'bunwichchef'
knife[:aws_secret_access_key] = 'Mw5sHvDgJRtAVP2vkA8gL8XJkvoZijhNMf' 

Note: the ssh_key_id is your key name without the pem

6) Create a gemfile to install some gems

gem install bundle
rbenv rehash
cd ~/ec2chefrepo/
vim Gemfile

source ''
gem 'chef'
gem 'knife-ec2'

Note: I had to run this before running bundle installs
sudo yum install gcc-c++

bundle install

7) Create and Deploy An Instance

Had to run:
gem install rb-readline unf
To get a list of AMI Image Ids

knife ec2 server create \
  --availability-zone us-east-1b \
  --node-name bunwichchefinstance.demo \
  --flavor t1.micro \
  --image ami-ba18d2 \
  --run-list "role[memcached]" \
  --identity-file ~/.ssh/bunwichchef.pem \
  --ssh-user ec2-user 

No comments: