Skip to content

Instantly share code, notes, and snippets.

@jtimberman
Created January 11, 2011 14:22
Show Gist options
  • Save jtimberman/774460 to your computer and use it in GitHub Desktop.
Save jtimberman/774460 to your computer and use it in GitHub Desktop.
this knife exec script example only creates a new instance. not wildly useful, but used as an example of the possibilities.
require 'fog'
require 'chef/knife/ec2_server_create'
connection = Fog::AWS::Compute.new(
:aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
:aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
:region => Chef::Config[:knife][:region]
)
server = connection.servers.create(
:image_id => Chef::Config[:knife][:aws_image],
:groups => Chef::Config[:knife][:aws_security_groups],
:flavor_id => Chef::Config[:knife][:aws_flavor],
:key_name => Chef::Config[:knife][:aws_ssh_key_id],
:availability_zone => Chef::Config[:knife][:aws_availability_zone]
)
puts "Instance ID: #{server.id}"
puts "Flavor: #{server.flavor_id}"
puts "Image: #{server.image_id}"
puts "Availability Zone: #{server.availability_zone}"
puts "Security Groups: #{server.groups.join(", ")}"
puts "SSH Key: #{server.key_name}"
print "\nWaiting for server"
server.wait_for { print "."; ready? }
puts("\n")
puts "Public DNS Name: #{server.dns_name}"
puts "Public IP Address: #{server.ip_address}"
puts "Private DNS Name: #{server.private_dns_name}"
puts "Private IP Address: #{server.private_ip_address}"
# Added to .chef/knife.rb
knife[:aws_access_key_id] = ENV['AWS_ACCESS_KEY_ID']
knife[:aws_secret_access_key] = ENV['AWS_SECRET_ACCESS_KEY']
knife[:aws_image] = "ami-a2f405cb" # ubuntu ami from canonical for t1.micro
knife[:aws_security_groups] = ["default"] # array of security group names
knife[:region] = "us-east-1" # change to suit
knife[:aws_flavor] = "t1.micro" # change to suit
knife[:aws_availability_zone] = "us-east-1b"
knife[:aws_ssh_key_id] = "jtimberman" # definitely change
% knife exec /tmp/ec2_server_create_only.rb
Instance ID: i-0797576b
Flavor: t1.micro
Image: ami-a2f405cb
Availability Zone: us-east-1b
Security Groups: default
SSH Key: jtimberman
Waiting for server........
Public DNS Name: ec2-184-72-92-239.compute-1.amazonaws.com
Public IP Address: 184.72.92.239
Private DNS Name: ip-10-122-225-92.ec2.internal
Private IP Address: 10.122.225.92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment