Created
April 19, 2012 22:03
-
-
Save systembell/2424525 to your computer and use it in GitHub Desktop.
Create EC2 VPC Instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fog' | |
require 'trollop' | |
require 'yaml' | |
STDOUT.sync = true | |
opts = Trollop::options do | |
version "#{File.basename($0)} 0.0.1 (c) 2011 Create EC2 instance" | |
banner <<-EOS | |
Usage: | |
#{File.basename($0)} [options] | |
where [options] are: | |
EOS | |
opt :flavor, "Instance flavor", :type => :string, :default => "m1.large" | |
opt :key, "Keypair", :type => :string | |
opt :ami, "AMI", :type => :string, :default => "ami-1bd68a5e" | |
opt :security_group, "Friendly Security Group name", :type => :string | |
opt :subnet_id, "Subnet ID", :type => :string | |
opt :name, "Instance name tag", :type => :string | |
opt :region, "EC2 region", :type => :string, :default => "us-west-1" | |
opt :environment, "Environment", :type => :string, :default => "development" | |
opt :count, "Number of instances", :type => :integer, :default => 1 | |
end | |
environment = opts[:environment] | |
Trollop::die :key, 'is required' if opts[:key].nil? | |
Trollop::die :security_group, 'is required' if opts[:security_group].nil? | |
Trollop::die :subnet_id, 'is required' if opts[:subnet_id].nil? | |
Trollop::die :name, 'is required' if opts[:name].nil? | |
settings = YAML.load_file("#{ENV['HOME']}/.fog") | |
aws_access_key = settings[:"#{environment}"][:aws_access_key_id] | |
aws_secret_key = settings[:"#{environment}"][:aws_secret_access_key] | |
compute = Fog::Compute.new({ | |
:provider => 'AWS', | |
:aws_access_key_id => aws_access_key, | |
:aws_secret_access_key => aws_secret_key, | |
:region => opts[:region] | |
}) | |
instances = "" | |
opts[:count].times do |c| | |
instance = compute.servers.create ({ | |
:flavor_id => opts[:flavor], | |
:key_name => opts[:key], | |
:image_id => opts[:ami], | |
:security_group_ids => opts[:security_group], | |
:subnet_id => opts[:subnet_id], | |
:tags => {"Name" => opts[:name]} | |
}) | |
instance.wait_for { print "."; ready? } | |
instances << "#{instance.private_dns_name}\t\t#{instance.private_ip_address}\t#{instance.id}\n" | |
end | |
puts "" | |
puts "#{instances}" | |
puts "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment