Class: Awsum::Ec2
- Includes:
- Requestable
- Defined in:
- lib/ec2/ec2.rb,
lib/ec2/image.rb,
lib/ec2/region.rb,
lib/ec2/volume.rb,
lib/ec2/address.rb,
lib/ec2/keypair.rb,
lib/ec2/instance.rb,
lib/ec2/snapshot.rb,
lib/ec2/security_group.rb,
lib/ec2/availability_zone.rb
Overview
Handles all interaction with Amazon EC2
Getting Started
Create an Awsum::Ec2 object and begin calling methods on it.
require 'rubygems'
require 'awsum'
ec2 = Awsum::Ec2.new('your access id', 'your secret key')
images = ec2.my_images
...
All calls to EC2 can be done directly in this class, or through a more object oriented way through the various returned classes
Examples
ec2.image('ami-ABCDEF').run
ec2.instance('i-123456789').volumes.each do |vol|
vol.create_snapsot
end
ec2.regions.each do |region|
region.use
images.each do |img|
puts "#{img.id} - #{region.name}"
end
end
end
Errors
All methods will raise an Awsum::Error if an error is returned from Amazon
Missing Methods
-
ConfirmProductInstance
-
ModifyImageAttribute
-
DescribeImageAttribute
-
ResetImageAttribute
If you need any of this functionality, please consider getting involved and help complete this library.
Defined Under Namespace
Classes: Address, AddressParser, AvailabilityZone, AvailabilityZoneParser, Image, ImageParser, Instance, InstanceParser, KeyPair, KeyPairParser, Region, RegionParser, RegisterImageParser, SecurityGroup, SecurityGroupParser, Snapshot, SnapshotParser, Volume, VolumeParser
Instance Method Summary collapse
-
#address(public_ip) ⇒ Object
Get the Address with a specific public ip.
-
#addresses(*public_ips) ⇒ Object
List Addresses.
-
#allocate_address ⇒ Object
Allocate Address.
-
#associate_address(instance_id, public_ip) ⇒ Object
Associate Address.
-
#attach_volume(volume_id, instance_id, device = '/dev/sdh') ⇒ Object
Attach a volume to an instance.
-
#authorize_security_group_ingress(group_name, options = {}) ⇒ Object
Authorize access on a specific security group.
-
#availability_zones(*zone_names) ⇒ Object
List all AvailabilityZone(s).
-
#create_key_pair(key_name) ⇒ Object
Create a new KeyPair.
-
#create_security_group(name, description) ⇒ Object
Create a new SecurityGroup.
-
#create_snapshot(volume_id) ⇒ Object
Create a Snapshot of a Volume.
-
#create_volume(availability_zone, options = {}) ⇒ Object
Create a new volume.
-
#delete_key_pair(key_name) ⇒ Object
Delete a KeyPair.
-
#delete_security_group(group_name) ⇒ Object
Delete a SecurityGroup.
-
#delete_snapshot(snapshot_id) ⇒ Object
Delete a Snapshot.
-
#delete_volume(volume_id) ⇒ Object
Delete a volume.
-
#deregister_image(image_id) ⇒ Object
Deregister an Image.
-
#detach_volume(volume_id, options = {}) ⇒ Object
Detach a volume from an instance.
-
#disassociate_address(public_ip) ⇒ Object
Disassociate Address.
-
#host ⇒ Object
The host to make all requests against.
- #host=(host) ⇒ Object
-
#image(image_id) ⇒ Object
Retrieve a single Image.
-
#images(options = {}) ⇒ Object
Retrieve a list of available Images.
-
#initialize(access_key = nil, secret_key = nil) ⇒ Ec2
constructor
Create an new ec2 instance.
-
#instance(instance_id) ⇒ Object
Retrieve the information on a single Instance.
-
#instances(*instance_ids) ⇒ Object
Retrieve the information on a number of Instance(s).
-
#key_pair(key_name) ⇒ Object
Get a single KeyPair.
-
#key_pairs(*key_names) ⇒ Object
List KeyPair(s).
-
#me ⇒ Object
Retrieves the currently running Instance This should only be run on a running EC2 instance.
-
#my_images ⇒ Object
Retrieve all Image(s) owned by you.
-
#region(region_name) ⇒ Object
List a Region.
-
#regions(*region_names) ⇒ Object
List all Region(s).
-
#register_image(image_location) ⇒ Object
Register an Image.
-
#release_address(public_ip) ⇒ Object
Releases an associated Address.
-
#release_address!(public_ip) ⇒ Object
Releases an associated Address.
-
#revoke_security_group_ingress(group_name, options = {}) ⇒ Object
Revoke access on a specific SecurityGroup.
-
#run_instances(image_id, options = {}) ⇒ Object
(also: #launch_instances)
Launch an ec2 Instance.
-
#security_group(group_name) ⇒ Object
Get a single SecurityGroup.
-
#security_groups(*group_names) ⇒ Object
List SecurityGroup(s).
-
#snapshot(snapshot_id) ⇒ Object
Get the information about a Snapshot.
-
#snapshots(*snapshot_ids) ⇒ Object
List Snapshot(s).
-
#terminate_instances(*instance_ids) ⇒ Object
Terminates the Instance(s).
-
#user_data ⇒ Object
Retreives the user-data supplied when starting the currently running Instance This should only be run on a running EC2 instance.
-
#volume(volume_id) ⇒ Object
Retreive information on a Volume.
-
#volumes(*volume_ids) ⇒ Object
Retrieve the information on a number of Volume(s).
Constructor Details
#initialize(access_key = nil, secret_key = nil) ⇒ Ec2
Create an new ec2 instance
The access_key and secret_key are both required to do any meaningful work.
If you want to get these keys from environment variables, you can do that in your code as follows:
ec2 = Awsum::Ec2.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
57 58 59 60 |
# File 'lib/ec2/ec2.rb', line 57 def initialize(access_key = nil, secret_key = nil) @access_key = access_key @secret_key = secret_key end |
Instance Method Details
#address(public_ip) ⇒ Object
Get the Address with a specific public ip
393 394 395 |
# File 'lib/ec2/ec2.rb', line 393 def address(public_ip) addresses(public_ip)[0] end |
#addresses(*public_ips) ⇒ Object
List Addresses
380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/ec2/ec2.rb', line 380 def addresses(*public_ips) action = 'DescribeAddresses' params = { 'Action' => action } params.merge!(array_to_params(public_ips, 'PublicIp')) response = send_query_request(params) parser = Awsum::Ec2::AddressParser.new(self) parser.parse(response.body) end |
#allocate_address ⇒ Object
Allocate Address
Will aquire an elastic ip address for use with your account
400 401 402 403 404 405 406 407 408 409 |
# File 'lib/ec2/ec2.rb', line 400 def allocate_address action = 'AllocateAddress' params = { 'Action' => action } response = send_query_request(params) parser = Awsum::Ec2::AddressParser.new(self) parser.parse(response.body)[0] end |
#associate_address(instance_id, public_ip) ⇒ Object
Associate Address
Will link an allocated elastic ip address to an Instance
NOTE: If the ip address is already associated with another instance, it will be associated with the new instance.
You can run this command more than once and it will not return an error.
418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/ec2/ec2.rb', line 418 def associate_address(instance_id, public_ip) action = 'AssociateAddress' params = { 'Action' => action, 'InstanceId' => instance_id, 'PublicIp' => public_ip } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#attach_volume(volume_id, instance_id, device = '/dev/sdh') ⇒ Object
Attach a volume to an instance
260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/ec2/ec2.rb', line 260 def attach_volume(volume_id, instance_id, device = '/dev/sdh') action = 'AttachVolume' params = { 'Action' => action, 'VolumeId' => volume_id, 'InstanceId' => instance_id, 'Device' => device } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#authorize_security_group_ingress(group_name, options = {}) ⇒ Object
Authorize access on a specific security group
Options:
User/Group access
-
:source_security_group_name
- Name of the security group to authorize access to when operating on a user/group pair -
:source_security_group_owner_id
- Owner of the security group to authorize access to when operating on a user/group pair
CIDR IP access
-
:ip_protocol
- IP protocol to authorize access to when operating on a CIDR IP (tcp, udp or icmp) (default: tcp) -
:from_port
- Bottom of port range to authorize access to when operating on a CIDR IP. This contains the ICMP type if ICMP is being authorized. -
:to_port
- Top of port range to authorize access to when operating on a CIDR IP. This contains the ICMP type if ICMP is being authorized. -
:cidr_ip
- CIDR IP range to authorize access to when operating on a CIDR IP. (default: 0.0.0.0/0)
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/ec2/ec2.rb', line 577 def (group_name, = {}) got_at_least_one_user_group_option = ![:source_security_group_name].nil? || ![:source_security_group_owner_id].nil? = ![:source_security_group_name].nil? && ![:source_security_group_owner_id].nil? got_at_least_one_cidr_option = ![:ip_protocol].nil? || ![:from_port].nil? || ![:to_port].nil? || ![:cidr_ip].nil? #Add in defaults = {:cidr_ip => '0.0.0.0/0'}.merge() if got_at_least_one_cidr_option = {:ip_protocol => 'tcp'}.merge() if got_at_least_one_cidr_option = ![:ip_protocol].nil? && ![:from_port].nil? && ![:to_port].nil? && ![:cidr_ip].nil? raise ArgumentError.new('Can only authorize user/group or CIDR IP, not both') if got_at_least_one_user_group_option && got_at_least_one_cidr_option raise ArgumentError.new('Need all user/group options when authorizing user/group access') if got_at_least_one_user_group_option && ! raise ArgumentError.new('Need all CIDR IP options when authorizing CIDR IP access') if got_at_least_one_cidr_option && ! raise ArgumentError.new('ip_protocol can only be one of tcp, udp or icmp') if got_at_least_one_cidr_option && !%w(tcp udp icmp).detect{|p| p == [:ip_protocol] } action = 'AuthorizeSecurityGroupIngress' params = { 'Action' => action, 'GroupName' => group_name } params['SourceSecurityGroupName'] = [:source_security_group_name] unless [:source_security_group_name].nil? params['SourceSecurityGroupOwnerId'] = [:source_security_group_owner_id] unless [:source_security_group_owner_id].nil? params['IpProtocol'] = [:ip_protocol] unless [:ip_protocol].nil? params['FromPort'] = [:from_port] unless [:from_port].nil? params['ToPort'] = [:to_port] unless [:to_port].nil? params['CidrIp'] = [:cidr_ip] unless [:cidr_ip].nil? response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#availability_zones(*zone_names) ⇒ Object
List all AvailabilityZone(s)
349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/ec2/ec2.rb', line 349 def availability_zones(*zone_names) action = 'DescribeAvailabilityZones' params = { 'Action' => action } params.merge!(array_to_params(zone_names, 'ZoneName')) response = send_query_request(params) parser = Awsum::Ec2::AvailabilityZoneParser.new(self) parser.parse(response.body) end |
#create_key_pair(key_name) ⇒ Object
Create a new KeyPair
499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/ec2/ec2.rb', line 499 def create_key_pair(key_name) action = 'CreateKeyPair' params = { 'Action' => action, 'KeyName' => key_name } response = send_query_request(params) parser = Awsum::Ec2::KeyPairParser.new(self) parser.parse(response.body)[0] end |
#create_security_group(name, description) ⇒ Object
Create a new SecurityGroup
542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/ec2/ec2.rb', line 542 def create_security_group(name, description) action = 'CreateSecurityGroup' params = { 'Action' => action, 'GroupName' => name, 'GroupDescription' => description } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#create_snapshot(volume_id) ⇒ Object
Create a Snapshot of a Volume
306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/ec2/ec2.rb', line 306 def create_snapshot(volume_id) action = 'CreateSnapshot' params = { 'Action' => action, 'VolumeId' => volume_id } response = send_query_request(params) parser = Awsum::Ec2::SnapshotParser.new(self) parser.parse(response.body)[0] end |
#create_volume(availability_zone, options = {}) ⇒ Object
Create a new volume
Options:
-
:size
- The size of the volume to be created (in GB) (NOTE: Required if you are not creating from a snapshot) -
:snapshot_id
- The snapshot id from which to create the volume
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/ec2/ec2.rb', line 243 def create_volume(availability_zone, = {}) raise ArgumentError.new('You must specify a size if not creating a volume from a snapshot') if [:snapshot_id].blank? && [:size].blank? action = 'CreateVolume' params = { 'Action' => action, 'AvailabilityZone' => availability_zone } params['Size'] = [:size] unless [:size].blank? params['SnapshotId'] = [:snapshot_id] unless [:snapshot_id].blank? response = send_query_request(params) parser = Awsum::Ec2::VolumeParser.new(self) parser.parse(response.body)[0] end |
#delete_key_pair(key_name) ⇒ Object
Delete a KeyPair
512 513 514 515 516 517 518 519 520 521 |
# File 'lib/ec2/ec2.rb', line 512 def delete_key_pair(key_name) action = 'DeleteKeyPair' params = { 'Action' => action, 'KeyName' => key_name } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#delete_security_group(group_name) ⇒ Object
Delete a SecurityGroup
555 556 557 558 559 560 561 562 563 564 |
# File 'lib/ec2/ec2.rb', line 555 def delete_security_group(group_name) action = 'DeleteSecurityGroup' params = { 'Action' => action, 'GroupName' => group_name } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#delete_snapshot(snapshot_id) ⇒ Object
Delete a Snapshot
337 338 339 340 341 342 343 344 345 346 |
# File 'lib/ec2/ec2.rb', line 337 def delete_snapshot(snapshot_id) action = 'DeleteSnapshot' params = { 'Action' => action, 'SnapshotId' => snapshot_id } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#delete_volume(volume_id) ⇒ Object
Delete a volume
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/ec2/ec2.rb', line 294 def delete_volume(volume_id) action = 'DeleteVolume' params = { 'Action' => action, 'VolumeId' => volume_id } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#deregister_image(image_id) ⇒ Object
Deregister an Image. Once deregistered, you can no longer launch the Image
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ec2/ec2.rb', line 108 def deregister_image(image_id) action = 'DeregisterImage' params = { 'Action' => action, 'ImageId' => image_id } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#detach_volume(volume_id, options = {}) ⇒ Object
Detach a volume from an instance
Options
-
:instance_id
- The ID of the instance from which the volume will detach -
:device
- The device name -
:force
- Whether to force the detachment. NOTE: If forced you may have data corruption issues.
279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/ec2/ec2.rb', line 279 def detach_volume(volume_id, = {}) action = 'DetachVolume' params = { 'Action' => action, 'VolumeId' => volume_id } params['InstanceId'] = [:instance_id] unless [:instance_id].blank? params['Device'] = [:device] unless [:device].blank? params['Force'] = [:force] unless [:force].blank? response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#disassociate_address(public_ip) ⇒ Object
Disassociate Address
Will disassociate an allocated elastic ip address from the Instance it’s allocated to
NOTE: You can run this command more than once and it will not return an error.
435 436 437 438 439 440 441 442 443 444 |
# File 'lib/ec2/ec2.rb', line 435 def disassociate_address(public_ip) action = 'DisassociateAddress' params = { 'Action' => action, 'PublicIp' => public_ip } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#host ⇒ Object
The host to make all requests against
648 649 650 |
# File 'lib/ec2/ec2.rb', line 648 def host @host ||= 'ec2.amazonaws.com' end |
#host=(host) ⇒ Object
652 653 654 |
# File 'lib/ec2/ec2.rb', line 652 def host=(host) @host = host end |
#image(image_id) ⇒ Object
Retrieve a single Image
90 91 92 |
# File 'lib/ec2/ec2.rb', line 90 def image(image_id) images(:image_ids => [image_id])[0] end |
#images(options = {}) ⇒ Object
Retrieve a list of available Images
Options:
-
:image_ids
- array of Image id’s, default: [] -
:owners
- array of owner id’s, default: [] -
:executable_by
- array of user id’s who have executable permission, default: []
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ec2/ec2.rb', line 68 def images( = {}) = {:image_ids => [], :owners => [], :executable_by => []}.merge() action = 'DescribeImages' params = { 'Action' => action } #Add options params.merge!(array_to_params([:image_ids], "ImageId")) params.merge!(array_to_params([:owners], "Owner")) params.merge!(array_to_params([:executable_by], "ExecutableBy")) response = send_query_request(params) parser = Awsum::Ec2::ImageParser.new(self) parser.parse(response.body) end |
#instance(instance_id) ⇒ Object
Retrieve the information on a single Instance
178 179 180 |
# File 'lib/ec2/ec2.rb', line 178 def instance(instance_id) instances([instance_id])[0] end |
#instances(*instance_ids) ⇒ Object
Retrieve the information on a number of Instance(s)
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ec2/ec2.rb', line 165 def instances(*instance_ids) action = 'DescribeInstances' params = { 'Action' => action } params.merge!(array_to_params(instance_ids, 'InstanceId')) response = send_query_request(params) parser = Awsum::Ec2::InstanceParser.new(self) parser.parse(response.body) end |
#key_pair(key_name) ⇒ Object
Get a single KeyPair
494 495 496 |
# File 'lib/ec2/ec2.rb', line 494 def key_pair(key_name) key_pairs(key_name)[0] end |
#key_pairs(*key_names) ⇒ Object
List KeyPair(s)
481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/ec2/ec2.rb', line 481 def key_pairs(*key_names) action = 'DescribeKeyPairs' params = { 'Action' => action } params.merge!(array_to_params(key_names, 'KeyName')) response = send_query_request(params) parser = Awsum::Ec2::KeyPairParser.new(self) parser.parse(response.body) end |
#me ⇒ Object
Retrieves the currently running Instance This should only be run on a running EC2 instance
184 185 186 187 188 189 190 191 192 |
# File 'lib/ec2/ec2.rb', line 184 def me require 'open-uri' begin instance_id = open('http://169.254.169.254/latest/meta-data/instance-id').read instance instance_id rescue OpenURI::HTTPError => e nil end end |
#my_images ⇒ Object
Retrieve all Image(s) owned by you
85 86 87 |
# File 'lib/ec2/ec2.rb', line 85 def my_images images :owners => 'self' end |
#region(region_name) ⇒ Object
List a Region
375 376 377 |
# File 'lib/ec2/ec2.rb', line 375 def region(region_name) regions(region_name)[0] end |
#regions(*region_names) ⇒ Object
List all Region(s)
362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/ec2/ec2.rb', line 362 def regions(*region_names) action = 'DescribeRegions' params = { 'Action' => action } params.merge!(array_to_params(region_names, 'Region')) response = send_query_request(params) parser = Awsum::Ec2::RegionParser.new(self) parser.parse(response.body) end |
#register_image(image_location) ⇒ Object
Register an Image
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ec2/ec2.rb', line 95 def register_image(image_location) action = 'RegisterImage' params = { 'Action' => action, 'ImageLocation' => image_location } response = send_query_request(params) parser = Awsum::Ec2::RegisterImageParser.new(self) parser.parse(response.body) end |
#release_address(public_ip) ⇒ Object
Releases an associated Address
NOTE: This is not a direct call to the Amazon web service. This is a safe operation that will first check to see if the address is allocated to an instance and fail if it is
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/ec2/ec2.rb', line 449 def release_address(public_ip) address = address(public_ip) if address.instance_id.nil? action = 'ReleaseAddress' params = { 'Action' => action, 'PublicIp' => public_ip } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) else raise 'Address is currently allocated' #FIXME: Add a proper Awsum error here end end |
#release_address!(public_ip) ⇒ Object
Releases an associated Address
NOTE: This will disassociate an address automatically if it is associated with an instance
469 470 471 472 473 474 475 476 477 478 |
# File 'lib/ec2/ec2.rb', line 469 def release_address!(public_ip) action = 'ReleaseAddress' params = { 'Action' => action, 'PublicIp' => public_ip } response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#revoke_security_group_ingress(group_name, options = {}) ⇒ Object
Revoke access on a specific SecurityGroup
Options:
User/Group access
-
:source_security_group_name
- Name of the security group to authorize access to when operating on a user/group pair -
:source_security_group_owner_id
- Owner of the security group to authorize access to when operating on a user/group pair
CIDR IP access
-
:ip_protocol
- IP protocol to authorize access to when operating on a CIDR IP (tcp, udp or icmp) (default: tcp) -
:from_port
- Bottom of port range to authorize access to when operating on a CIDR IP. This contains the ICMP type if ICMP is being authorized. -
:to_port
- Top of port range to authorize access to when operating on a CIDR IP. This contains the ICMP type if ICMP is being authorized. -
:cidr_ip
- CIDR IP range to authorize access to when operating on a CIDR IP. (default: 0.0.0.0/0)
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/ec2/ec2.rb', line 617 def revoke_security_group_ingress(group_name, = {}) got_at_least_one_user_group_option = ![:source_security_group_name].nil? || ![:source_security_group_owner_id].nil? = ![:source_security_group_name].nil? && ![:source_security_group_owner_id].nil? got_at_least_one_cidr_option = ![:ip_protocol].nil? || ![:from_port].nil? || ![:to_port].nil? || ![:cidr_ip].nil? #Add in defaults = {:cidr_ip => '0.0.0.0/0'}.merge() if got_at_least_one_cidr_option = {:ip_protocol => 'tcp'}.merge() if got_at_least_one_cidr_option = ![:ip_protocol].nil? && ![:from_port].nil? && ![:to_port].nil? && ![:cidr_ip].nil? raise ArgumentError.new('Can only authorize user/group or CIDR IP, not both') if got_at_least_one_user_group_option && got_at_least_one_cidr_option raise ArgumentError.new('Need all user/group options when revoking user/group access') if got_at_least_one_user_group_option && ! raise ArgumentError.new('Need all CIDR IP options when revoking CIDR IP access') if got_at_least_one_cidr_option && ! raise ArgumentError.new('ip_protocol can only be one of tcp, udp or icmp') if got_at_least_one_cidr_option && !%w(tcp udp icmp).detect{|p| p == [:ip_protocol] } action = 'RevokeSecurityGroupIngress' params = { 'Action' => action, 'GroupName' => group_name } params['SourceSecurityGroupName'] = [:source_security_group_name] unless [:source_security_group_name].nil? params['SourceSecurityGroupOwnerId'] = [:source_security_group_owner_id] unless [:source_security_group_owner_id].nil? params['IpProtocol'] = [:ip_protocol] unless [:ip_protocol].nil? params['FromPort'] = [:from_port] unless [:from_port].nil? params['ToPort'] = [:to_port] unless [:to_port].nil? params['CidrIp'] = [:cidr_ip] unless [:cidr_ip].nil? response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#run_instances(image_id, options = {}) ⇒ Object Also known as: launch_instances
Launch an ec2 Instance
Options:
-
:min
- The minimum number of instances to launch. Default: 1 -
:max
- The maximum number of instances to launch. Default: 1 -
:key_name
- The name of the key pair with which to launch instances -
:security_groups
- The names of security groups to associate launched instances with -
:user_data
- User data made available to instances (Note: Must be 16K or less, will be base64 encoded by Awsum) -
:instance_type
- The size of the instances to launch, can be one of [m1.small, m1.large, m1.xlarge, c1.medium, c1.xlarge], default is m1.small -
:availability_zone
- The name of the availability zone to launch this Instance in -
:kernel_id
- The ID of the kernel with which to launch instances -
:ramdisk_id
- The ID of the RAM disk with which to launch instances -
:block_device_map
- A ‘hash’ of mappings. E.g. => ‘sdb’
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ec2/ec2.rb', line 132 def run_instances(image_id, = {}) = {:min => 1, :max => 1}.merge() action = 'RunInstances' params = { 'Action' => action, 'ImageId' => image_id, 'MinCount' => [:min], 'MaxCount' => [:max], 'KeyName' => [:key_name], 'UserData' => [:user_data].nil? ? nil : Base64::encode64([:user_data]).gsub(/\n/, ''), 'InstanceType' => [:instance_type], 'Placement.AvailabilityZone' => [:availability_zone], 'KernelId' => [:kernel_id], 'RamdiskId' => [:ramdisk_id] } if [:block_device_map].respond_to?(:keys) map = [:block_device_map] map.keys.each_with_index do |key, i| params["BlockDeviceMapping.#{i+1}.VirtualName"] = key params["BlockDeviceMapping.#{i+1}.DeviceName"] = map[key] end else raise ArgumentError.new("options[:block_device_map] - must be a key => value map") unless [:block_device_map].nil? end params.merge!(array_to_params([:security_groups], "SecurityGroup")) response = send_query_request(params) parser = Awsum::Ec2::InstanceParser.new(self) parser.parse(response.body) end |
#security_group(group_name) ⇒ Object
Get a single SecurityGroup
537 538 539 |
# File 'lib/ec2/ec2.rb', line 537 def security_group(group_name) security_groups(group_name)[0] end |
#security_groups(*group_names) ⇒ Object
List SecurityGroup(s)
524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/ec2/ec2.rb', line 524 def security_groups(*group_names) action = 'DescribeSecurityGroups' params = { 'Action' => action } params.merge!(array_to_params(group_names, 'GroupName')) response = send_query_request(params) parser = Awsum::Ec2::SecurityGroupParser.new(self) parser.parse(response.body) end |
#snapshot(snapshot_id) ⇒ Object
Get the information about a Snapshot
332 333 334 |
# File 'lib/ec2/ec2.rb', line 332 def snapshot(snapshot_id) snapshots(snapshot_id)[0] end |
#snapshots(*snapshot_ids) ⇒ Object
List Snapshot(s)
319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/ec2/ec2.rb', line 319 def snapshots(*snapshot_ids) action = 'DescribeSnapshots' params = { 'Action' => action } params.merge!(array_to_params(snapshot_ids, 'SnapshotId')) response = send_query_request(params) parser = Awsum::Ec2::SnapshotParser.new(self) parser.parse(response.body) end |
#terminate_instances(*instance_ids) ⇒ Object
Terminates the Instance(s)
Returns true if the terminations succeeds, false otherwise
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ec2/ec2.rb', line 208 def terminate_instances(*instance_ids) action = 'TerminateInstances' params = { 'Action' => action } params.merge!(array_to_params(instance_ids, 'InstanceId')) response = send_query_request(params) response.is_a?(Net::HTTPSuccess) end |
#user_data ⇒ Object
Retreives the user-data supplied when starting the currently running Instance This should only be run on a running EC2 instance
196 197 198 199 200 201 202 203 |
# File 'lib/ec2/ec2.rb', line 196 def user_data require 'open-uri' begin open('http://169.254.169.254/latest/user-data').read rescue OpenURI::HTTPError => e nil end end |
#volume(volume_id) ⇒ Object
Retreive information on a Volume
233 234 235 |
# File 'lib/ec2/ec2.rb', line 233 def volume(volume_id) volumes(volume_id)[0] end |
#volumes(*volume_ids) ⇒ Object
Retrieve the information on a number of Volume(s)
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/ec2/ec2.rb', line 220 def volumes(*volume_ids) action = 'DescribeVolumes' params = { 'Action' => action } params.merge!(array_to_params(volume_ids, 'VolumeId')) response = send_query_request(params) parser = Awsum::Ec2::VolumeParser.new(self) parser.parse(response.body) end |