We want to create an instance on Amazon and provision (setup) oit using Chef provisioning. We will use gem chef-provisioning-fog to work with Amazon EC2.
-
Install ChefDK read install ChefDK
-
install gems
chef gem install chef-provisioning-fog
- Directory structure
our project:
/path/to/project/
|__ .chef/ - directory with settings for knife
|__ keys - directory with AWS keys
|__ cookbooks/ - directory with cookbooks for the machine
|__ myserver.rb
|__ destroy.rb
chef-repo - somewhere on the workstation
/path/to/chef-repo
|_ cookbooks
...
-
create directory .chef in your project
-
edit/create
.chef/knife.rb
current_dir = File.dirname(__FILE__)
dir_local_base = File.expand_path('../../', __FILE__)
local_mode true
# ???
ssl_verify_mode :verify_none
# ssh
knife[:ssh_attribute] = "knife_zero.host"
knife[:use_sudo] = false
knife[:private_key_paths] = dir_local_base+"/keys"
# repos
dir_base_repo = '/work/chef-repo'
cookbook_path [
"#{dir_local_base}/cookbooks",
"#{dir_base_repo}/cookbooks",
]
## Attributes of node objects will be saved to json file.
## the automatic_attribute_whitelist option limits the attributes to be saved.
knife[:automatic_attribute_whitelist] = %w[
fqdn
os
os_version
hostname
ipaddress
roles
recipes
ipaddress
platform
platform_version
platform_version
cloud
cloud_v2
chef_packages
]
-
place key to
.chef/keys
folder -.chef/keys/mykey.pem
-
config.json - file with custom instance settings
{
"myname": "mykey"
}
- myserver.rb
require 'chef/provisioning'
account_id = 'your_account_id'
with_driver 'fog:AWS', :compute_options => {
aws_access_key_id: 'your_id',
aws_secret_access_key: 'your_access_key',
region: 'us-west-2' # your zone
}
with_machine_options :bootstrap_options => {
key_name: 'mykey',
flavor_id: 't2.micro',
image_id: 'ami-xxxx', # this is ubuntu 14.04.1
#security_group_ids: "sg-xxxxxxxx"
#:groups => ['agroup'],
client_pem_path: "keys/mykey.pem",
}
machine 'myawsserver' do
action :converge
recipe 'base::default'
end
- recipes
place recipes in cookbooks
.
We have one simple recipe cookbooks/base/recipes/default.rb
:
# debug
file '/tmp/debug.txt' do
content "it is debug"
end
- run
chef-client -z myserver.rb -j config.json
destroy.rb
require 'chef/provisioning'
with_driver 'fog:AWS', :compute_options => {
aws_access_key_id: 'xxx',
aws_secret_access_key: 'xxxx',
region: 'us-west-2'
}
with_machine_options :bootstrap_options => {
key_name: 'mykey',
flavor_id: 't2.micro',
image_id: 'ami-xxx', # this is ubuntu 14.04.1
#security_group_ids: "sg-xxxxxxxx"
#:groups => ['agroup'],
client_pem_path: "keys/mykey.pem",
}
machine 'myawsserver' do
action :destroy
end
- run
chef-client -z destroy.rb -j config.json