The RightAws::EC2 class provides a complete interface to Amazon‘s Elastic Compute Cloud service, as well as the associated EBS (Elastic Block Store). For explanations of the semantics of each call, please refer to Amazon‘s documentation at developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=87
Examples:
Create an EC2 interface handle:
@ec2 = RightAws::Ec2.new(aws_access_key_id, aws_secret_access_key)
Create a new SSH key pair:
@key = 'right_ec2_awesome_test_key' new_key = @ec2.create_key_pair(@key) keys = @ec2.describe_key_pairs
Create a security group:
@group = 'right_ec2_awesome_test_security_group' @ec2.create_security_group(@group,'My awesome test group') group = @ec2.describe_security_groups([@group])[0]
Configure a security group:
@ec2.authorize_security_group_named_ingress(@group, account_number, 'default') @ec2.authorize_security_group_IP_ingress(@group, 80,80,'udp','192.168.1.0/8')
Describe the available images:
images = @ec2.describe_images
Launch an instance:
ec2.run_instances('ami-9a9e7bf3', 1, 1, ['default'], @key, 'SomeImportantUserData', 'public')
Describe running instances:
@ec2.describe_instances
Error handling: all operations raise an RightAws::AwsError in case of problems. Note that transient errors are automatically retried.
API_VERSION | = | "2009-11-30" | Amazon EC2 API version being used | |
DEFAULT_HOST | = | "ec2.amazonaws.com" | ||
DEFAULT_PATH | = | '/' | ||
DEFAULT_PROTOCOL | = | 'https' | ||
DEFAULT_PORT | = | 443 | ||
DEFAULT_ADDRESSING_TYPE | = | 'public' | Default addressing type (public=NAT, direct=no-NAT) used when launching instances. | |
DNS_ADDRESSING_SET | = | ['public','direct'] | ||
DEFAULT_INSTANCE_TYPE | = | 'm1.small' | Amazon EC2 Instance Types : www.amazon.com/b?ie=UTF8&node=370375011 Default EC2 instance type (platform) | |
INSTANCE_TYPES | = | ['m1.small','c1.medium','m1.large','m1.xlarge','c1.xlarge', 'm2.xlarge', 'm2.2xlarge', 'm2.4xlarge'] | ||
INSTANCE_ATTRIBUTE_MAPPING | = | { "instance_type" => "instanceType", "kernel" => "kernel", "ramdisk" => "ramdisk", "user_data" => "userData", "disable_api_termination" => "disableApiTermination", "instance_initiated_shutdown_behavior" => "instanceInitiatedShutdownBehavior", "root_device_name" => "rootDeviceName", "block_device_mapping" => "blockDeviceMapping" |
Create a new handle to an EC2 account. All handles share the same per process or per thread HTTP connection to Amazon EC2. Each handle is for a specific account. The params have the following options:
describe_images_by_owner, describe_images_by_executable_by, describe_availability_zones, describe_security_groups, describe_key_pairs, describe_addresses, describe_volumes, describe_snapshots methods, default: false.
Acquire a new elastic IP address for use with your account. Returns allocated IP address or an exception.
ec2.allocate_address #=> '75.101.154.140'
Associate an elastic IP address with an instance. Returns true or an exception.
ec2.associate_address('i-d630cbbf', '75.101.154.140') #=> true
Associate DHCP options
ec2.associate_dhcp_options("dopt-cb0de3a2", "vpc-890ce2e0" ) #=> true ec2.describe_vpcs #=>
[{:vpc_id=>"vpc-890ce2e0", :dhcp_options_id=>"dopt-cb0de3a2", :cidr_block=>"10.0.0.0/23", :state=>"available"}]
Attach the specified EBS volume to a specified instance, exposing the volume using the specified device name.
ec2.attach_volume('vol-898a6fe0', 'i-7c905415', '/dev/sdh') #=> { :aws_instance_id => "i-7c905415", :aws_device => "/dev/sdh", :aws_status => "attaching", :aws_attached_at => "2008-03-28T14:14:39.000Z", :aws_id => "vol-898a6fe0" }
Attach VPN gateway.
ec2.attach_vpn_gateway('vgw-dfa144b6','vpc-890ce2e0') #=> {:vpc_id=>"vpc-890ce2e0", :state=>"attaching"}
Add permission to a security group. Returns true or an exception. protocol is one of :’tcp’|’udp’|’icmp’.
ec2.authorize_security_group_IP_ingress('my_awesome_group', 80, 82, 'udp', '192.168.1.0/8') #=> true ec2.authorize_security_group_IP_ingress('my_awesome_group', -1, -1, 'icmp') #=> true
Authorize named ingress for security group. Allows instances that are member of someone else‘s security group to open connections to instances in my group.
ec2.authorize_security_group_named_ingress('my_awesome_group', '7011-0219-8268', 'their_group_name') #=> true
Bundle a Windows image. Internally, it queues the bundling task and shuts down the instance. It then takes a snapshot of the Windows volume bundles it, and uploads it to S3. After bundling completes, Rightaws::Ec2#register_image may be used to register the new Windows AMI for subsequent launches.
ec2.bundle_instance('i-e3e24e8a', 'my-awesome-bucket', 'my-win-image-1') #=> [{:aws_update_time => "2008-10-16T13:58:25.000Z", :s3_bucket => "kd-win-1", :s3_prefix => "win2pr", :aws_state => "pending", :aws_id => "bun-26a7424f", :aws_instance_id => "i-878a25ee", :aws_start_time => "2008-10-16T13:58:02.000Z"}]
Cancel an ināprogress or pending bundle task by id.
ec2.cancel_bundle_task('bun-73a7421a') #=> [{:s3_bucket => "my-awesome-bucket" :aws_id => "bun-0fa70206", :s3_prefix => "win02", :aws_start_time => "2008-10-14T13:00:29.000Z", :aws_error_message => "User has requested bundling operation cancellation", :aws_state => "failed", :aws_update_time => "2008-10-14T13:01:31.000Z", :aws_error_code => "Client.Cancelled", :aws_instance_id => "i-e3e24e8a"}
Cancel one or more Spot Instance requests.
ec2.cancel_spot_instance_requests('sir-60662c03',"sir-d3c96e04", "sir-4fa8d804","sir-6992ce04") #=> [{:state=>"cancelled", :spot_instance_request_id=>"sir-60662c03"}, {:state=>"cancelled", :spot_instance_request_id=>"sir-6992ce04"}, {:state=>"cancelled", :spot_instance_request_id=>"sir-4fa8d804"}, {:state=>"cancelled", :spot_instance_request_id=>"sir-d3c96e04"}]
Return the product code attached to instance or nil otherwise.
ec2.confirm_product_instance('ami-e444444d','12345678') #=> nil ec2.confirm_product_instance('ami-e444444d','00001111') #=> "000000000888"
Create customer gateway.
ec2.create_customer_gateway('ipsec.1', '12.1.2.3', 65534) #=> {:type=>"ipsec.1", :bgp_asn=>"65534", :ip_address=>"12.1.2.3", :state=>"pending", :customer_gateway_id=>"cgw-d5a643bc"}
Create DHCP options.
ec2.create_dhcp_options('domain-name' => 'my.awesomesite.ru', 'netbios-node-type' => 1) #=> {:dhcp_options_id=>"dopt-cb0de3a2", :dhcp_configuration_set=> {"netbios-node-type"=>["1"], "domain-name"=>["my.awesomesite.ru"]}}
Create a new image. Options: :name, :description, :no_reboot(bool)
ec2.create_image(instance, :description => 'KD: test#1', :no_reboot => true, :name => 'kd-1' ) #=> "ami-84a1f792"
Create new SSH key. Returns a hash of the key‘s data or an exception.
ec2.create_key_pair('my_awesome_key') #=> {:aws_key_name => "my_awesome_key", :aws_fingerprint => "01:02:03:f4:25:e6:97:e8:9b:02:1a:26:32:4e:58:6b:7a:8c:9f:03", :aws_material => "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAK...Q8MDrCbuQ=\n-----END RSA PRIVATE KEY-----"}
Create new Security Group. Returns true or an exception.
ec2.create_security_group('default-1',"Default allowing SSH, HTTP, and HTTPS ingress") #=> true
Create a snapshot of specified volume.
ec2.create_snapshot('vol-898a6fe0', 'KD: WooHoo!!') #=> {:aws_volume_id=>"vol-e429db8d", :aws_started_at=>"2009-10-01T09:23:38.000Z", :aws_description=>"KD: WooHoo!!", :aws_owner=>"648770000000", :aws_progress=>"", :aws_status=>"pending", :aws_volume_size=>1, :aws_id=>"snap-3df54854"}
Create the data feed for Spot Instances (Enables to view Spot Instance usage logs)
ec2.create_spot_datafeed_subscription('bucket-for-konstantin-eu', 'splogs/') #=> { :owner_id=>"826693181925", :bucket=>"bucket-for-konstantin-eu", :prefix=>"splogs/", :state=>"Active"}
Create Subnet.
ec2.create_subnet("vpc-890ce2e0",'10.0.1.0/24') #=> {:available_ip_address_count=>"251", :vpc_id=>"vpc-890ce2e0", :availability_zone=>"us-east-1a", :subnet_id=>"subnet-770de31e", :cidr_block=>"10.0.1.0/24", :state=>"pending"}
Create new EBS volume based on previously created snapshot. Size in Gigabytes.
ec2.create_volume('snap-000000', 10, zone) #=> {:snapshot_id => "snap-e21df98b", :aws_status => "creating", :aws_id => "vol-fc9f7a95", :zone => "merlot", :aws_created_at => "2008-06-24T18:13:32.000Z", :aws_size => 94}
Create VPC.
ec2.create_vpc('10.0.0.0/23') #=> {:vpc_id=>"vpc-890ce2e0", :dhcp_options_id=>"default", :cidr_block=>"10.0.0.0/23", :state=>"pending"}
Create VPN connection.
ec2.create_vpn_connection('ipsec.1', 'cgw-81a643e8' ,'vgw-dfa144b6') {:customer_gateway_id=>"cgw-81a643e8", :vpn_connection_id=>"vpn-a9a643c0", :customer_gateway_configuration=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<vpn_connection id=\"vpn-a9a643c0\">\n...</vpn_connection>\n", :state=>"pending", :vpn_gateway_id=>"vgw-dfa144b6"}
Create VPN gateway.
ec2.create_vpn_gateway('ipsec.1') #=> {:type=>"ipsec.1", :availability_zone=>"us-east-1a", :attachments=>[nil], :vpn_gateway_id=>"vgw-dfa144b6"}
Remove Security Group. Returns true or an exception.
ec2.delete_security_group('default-1') #=> true
Delete the specified EBS volume. This does not deletes any snapshots created from this volume.
ec2.delete_volume('vol-b48a6fdd') #=> true
Deregister image at Amazon. Returns true or an exception.
ec2.deregister_image('ami-e444444d') #=> true
List elastic IP addresses assigned to your account. Returns an array of 2 keys (:instance_id and :public_ip) hashes:
ec2.describe_addresses #=> [{:instance_id=>"i-d630cbbf", :public_ip=>"75.101.154.140"}, {:instance_id=>nil, :public_ip=>"75.101.154.141"}] ec2.describe_addresses('75.101.154.140') #=> [{:instance_id=>"i-d630cbbf", :public_ip=>"75.101.154.140"}]
Describes availability zones that are currently available to the account and their states. Returns an array of 2 keys (:zone_name and :zone_state) hashes:
ec2.describe_availability_zones #=> [{:region_name=>"us-east-1", :zone_name=>"us-east-1a", :zone_state=>"available"}, ... ] ec2.describe_availability_zones('us-east-1c') #=> [{:region_name=>"us-east-1", :zone_state=>"available", :zone_name=>"us-east-1c"}]
Describe the status of the Windows AMI bundlings. If list is omitted the returns the whole list of tasks.
ec2.describe_bundle_tasks(['bun-4fa74226']) #=> [{:s3_bucket => "my-awesome-bucket" :aws_id => "bun-0fa70206", :s3_prefix => "win1pr", :aws_start_time => "2008-10-14T16:27:57.000Z", :aws_update_time => "2008-10-14T16:37:10.000Z", :aws_error_code => "Client.S3Error", :aws_error_message => "AccessDenied(403)- Invalid according to Policy: Policy Condition failed: [\"eq\", \"$acl\", \"aws-exec-read\"]", :aws_state => "failed", :aws_instance_id => "i-e3e24e8a"}]
Describe customer gateways.
ec2.describe_customer_gateways [{:type=>"ipsec.1", :ip_address=>"12.1.2.3", :bgp_asn=>"65534", :state=>"available", :customer_gateway_id=>"cgw-d5a643bc"}]
Describe DHCP options.
ec2.describe_dhcp_options #=>
[{:dhcp_options_id=>"dopt-cb0de3a2", :dhcp_configuration_set=> {"netbios-node-type"=>["1"], "domain-name"=>["my.awesomesite.ru"]}}]
Describe image attributes. Currently ‘launchPermission’, ‘productCodes’, ‘kernel’, ‘ramdisk’ and ‘blockDeviceMapping’ are supported.
ec2.describe_image_attribute('ami-e444444d') #=> {:groups=>["all"], :users=>["000000000777"]}
Retrieve a list of images. Returns array of hashes describing the images or an exception: image_type = ‘machine’ || ‘kernel’ || ‘ramdisk‘
ec2.describe_images #=> [{:aws_id=>"ami-b0a1f7a6", :aws_image_type=>"machine", :root_device_name=>"/dev/sda1", :image_class=>"static", :aws_owner=>"826693181925", :aws_location=>"bucket_for_k_dzreyev/image_bundles/kd__CentOS_1_10_2009_10_21_13_30_43_MSD/image.manifest.xml", :aws_state=>"available", :aws_is_public=>false, :aws_architecture=>"i386"}, {:description=>"EBS backed Fedora core 8 i386", :aws_architecture=>"i386", :aws_id=>"ami-c2a3f5d4", :aws_image_type=>"machine", :root_device_name=>"/dev/sda1", :image_class=>"elastic", :aws_owner=>"937766719418", :aws_location=>"937766719418/EBS backed FC8 i386", :aws_state=>"available", :block_device_mappings=> [{:ebs_snapshot_id=>"snap-829a20eb", :ebs_delete_on_termination=>true, :device_name=>"/dev/sda1"}], :name=>"EBS backed FC8 i386", :aws_is_public=>true}, ... ]
If list param is set, then retrieve information about the listed images only:
ec2.describe_images(['ami-5aa1f74c'])
Example:
ec2.describe_images_by_executable_by('522821470517') ec2.describe_images_by_executable_by('self') ec2.describe_images_by_executable_by('all')
Describe instance attribute. Attributes: :instance_type, :kernel, :ramdisk, :user_data, :disable_api_termination, :instance_initiated_shutdown_behavior, :root_device_name, :block_device_mapping
ec2.describe_instance_attribute(instance, "BlockDeviceMapping") #=> [{:ebs_delete_on_termination=>true, :ebs_volume_id=>"vol-683dc401", :device_name=>"/dev/sda1"}] ec2.describe_instance_attribute(instance, "InstanceType") #=> "m1.small" ec2.describe_instance_attribute(instance, "InstanceInitiatedShutdownBehavior") #=> "stop"
Retrieve information about EC2 instances. If list is omitted then returns the list of all instances.
ec2.describe_instances #=> [{:private_ip_address=>"10.240.7.99", :aws_image_id=>"ami-c2a3f5d4", :ip_address=>"174.129.134.109", :dns_name=>"ec2-174-129-134-109.compute-1.amazonaws.com", :aws_instance_type=>"m1.small", :aws_owner=>"826693181925", :root_device_name=>"/dev/sda1", :instance_class=>"elastic", :aws_state=>"running", :private_dns_name=>"domU-12-31-39-04-00-95.compute-1.internal", :aws_reason=>"", :aws_launch_time=>"2009-11-18T14:03:25.000Z", :aws_reservation_id=>"r-54d38542", :aws_state_code=>16, :ami_launch_index=>"0", :aws_availability_zone=>"us-east-1a", :aws_groups=>["default"], :monitoring_state=>"disabled", :aws_product_codes=>[], :ssh_key_name=>"", :block_device_mappings=> [{:ebs_status=>"attached", :ebs_delete_on_termination=>true, :ebs_attach_time=>"2009-11-18T14:03:34.000Z", :device_name=>"/dev/sda1", :ebs_volume_id=>"vol-e600f98f"}, {:ebs_status=>"attached", :ebs_delete_on_termination=>true, :ebs_attach_time=>"2009-11-18T14:03:34.000Z", :device_name=>"/dev/sdk", :ebs_volume_id=>"vol-f900f990"}], :aws_instance_id=>"i-8ce84ae4"} , ... ]
Retrieve a list of SSH keys. Returns an array of keys or an exception. Each key is represented as a two-element hash.
ec2.describe_key_pairs #=> [{:aws_fingerprint=> "01:02:03:f4:25:e6:97:e8:9b:02:1a:26:32:4e:58:6b:7a:8c:9f:03", :aws_key_name=>"key-1"}, {:aws_fingerprint=> "1e:29:30:47:58:6d:7b:8c:9f:08:11:20:3c:44:52:69:74:80:97:08", :aws_key_name=>"key-2"}, ..., {...} ]
Retrieve reserved instances list. Returns a list of Reserved Instances.
ec2.describe_reserved_instances #=>
[{:aws_id=>"1ba8e2e3-1c40-434c-a741-5ff16a4c542e", :aws_duration=>31536000, :aws_instance_type=>"m1.small", :aws_usage_price=>0.03, :aws_availability_zone=>"us-east-1b", :aws_state=>"payment-pending", :aws_product_description=>"Test", :aws_fixed_price=>325.0, :aws_start=>"2009-12-18T20:39:39.569Z" :aws_instance_count=>1}]
Retrieve reserved instances offerings. Returns a set of available offerings.
Optional params:
:aws_instance_type => String :aws_availability_zone => String :aws_product_description => String ec2.describe_reserved_instances_offerings #=> [{:aws_instance_type=>"c1.medium", :aws_availability_zone=>"us-east-1c", :aws_duration=>94608000, :aws_product_description=>"Linux/UNIX", :aws_id=>"e5a2ff3b-f6eb-4b4e-83f8-b879d7060257", :aws_usage_price=>0.06, :aws_fixed_price=>1000.0}, ... {:aws_instance_type=>"m1.xlarge", :aws_availability_zone=>"us-east-1a", :aws_duration=>31536000, :aws_product_description=>"Linux/UNIX", :aws_id=>"c48ab04c-63ab-4cd6-b8f5-978a29eb9bcc", :aws_usage_price=>0.24, :aws_fixed_price=>2600.0}]
Retrieve Security Groups information. If list is omitted the returns the whole list of groups.
# Amazon cloud: ec2 = Rightscale::Ec2.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) ec2.describe_security_groups #=> [{:aws_perms=> [{:group=>"default", :owner=>"048291609141"}, {:to_port=>"22", :protocol=>"tcp", :from_port=>"22", :cidr_ips=>"0.0.0.0/0"}, {:to_port=>"9997", :protocol=>"tcp", :from_port=>"9997", :cidr_ips=>"0.0.0.0/0"}], :aws_group_name=>"photo_us", :aws_description=>"default group", :aws_owner=>"826693181925"}] # Eucalyptus cloud: ec2 = Rightscale::Ec2.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, :eucalyptus => true) ec2.describe_security_groups #=> [{:aws_perms=> [{:to_port=>"65535", :group=>"default", :protocol=>"tcp", :owner=>"048291609141", :from_port=>"1"}, {:to_port=>"65535", :group=>"default", :protocol=>"udp", :owner=>"048291609141", :from_port=>"1"}, {:to_port=>"-1", :group=>"default", :protocol=>"icmp", :owner=>"048291609141", :from_port=>"-1"}, {:to_port=>"22", :protocol=>"tcp", :from_port=>"22", :cidr_ip=>"0.0.0.0/0"}, {:to_port=>"9997", :protocol=>"tcp", :from_port=>"9997", :cidr_ip=>"0.0.0.0/0"}], :aws_group_name=>"photo_us", :aws_description=>"default group", :aws_owner=>"826693181925"}]
Describe snapshot attribute.
ec2.describe_snapshot_attribute('snap-36fe435f') #=> {:create_volume_permission=> {:users=>["826690000000", "826690000001"], :groups=>['all']}}
Describe all EBS snapshots.
ec2.describe_snapshots #=>
[ {:aws_volume_id=>"vol-545fac3d", :aws_description=>"Wikipedia XML Backups (Linux)", :aws_progress=>"100%", :aws_started_at=>"2009-09-28T23:49:50.000Z", :aws_owner=>"amazon", :aws_id=>"snap-8041f2e9", :aws_volume_size=>500, :aws_status=>"completed"}, {:aws_volume_id=>"vol-185fac71", :aws_description=>"Sloan Digital Sky Survey DR6 Subset (Linux)", :aws_progress=>"100%", :aws_started_at=>"2009-09-28T23:56:10.000Z", :aws_owner=>"amazon", :aws_id=>"snap-3740f35e", :aws_volume_size=>180, :aws_status=>"completed"}, ...]
Describe the data feed for Spot Instances.
ec2.describe_spot_datafeed_subscription #=> { :owner_id=>"826693181925", :bucket=>"bucket-for-konstantin-eu", :prefix=>"splogs/", :state=>"Active"}
Describe Spot Instance requests.
ec2.describe_spot_instance_requests #=> [{:type=>"one-time", :create_time=>"2010-03-10T10:30:32.000Z", :instance_type=>"c1.medium", :state=>"cancelled", :groups=>["default"], :product_description=>"Linux/UNIX", :spot_instance_request_id=>"sir-bfa06804", :image_id=>"ami-08f41161", :spot_price=>0.01, :monitoring_enabled=>false}, {:type=>"one-time", :create_time=>"2010-03-10T10:33:29.000Z", :instance_type=>"c1.medium", :state=>"open", :groups=>["default", "33"], :product_description=>"Linux/UNIX", :spot_instance_request_id=>"sir-b1713a03", :image_id=>"ami-08f41161", :spot_price=>0.01, :monitoring_enabled=>false, :key_name=>"tim"}, {:type=>"one-time", :instance_id=>"i-c516ceae", :create_time=>"2010-03-10T10:43:48.000Z", :instance_type=>"c1.medium", :state=>"active", :groups=>["default", "33"], :product_description=>"Linux/UNIX", :spot_instance_request_id=>"sir-5eb6c604", :image_id=>"ami-08f41161", :spot_price=>0.2, :monitoring_enabled=>false, :key_name=>"tim"}]
Describe Spot Price history. Options: :start_time, :end_time, instance_types, product_description
ec2.describe_spot_price_history #=> [{:spot_price=>0.054, :timestamp=>"2009-12-07T12:12:58.000Z", :product_description=>"Windows", :instance_type=>"m1.small"}, {:spot_price=>0.06, :timestamp=>"2009-12-07T12:18:32.000Z", :product_description=>"Linux/UNIX", :instance_type=>"c1.medium"}, {:spot_price=>0.198, :timestamp=>"2009-12-07T12:58:00.000Z", :product_description=>"Windows", :instance_type=>"m1.large"}, {:spot_price=>0.028, :timestamp=>"2009-12-07T13:48:50.000Z", :product_description=>"Linux/UNIX", :instance_type=>"m1.small"}, ... ] ec2.describe_spot_price_history(:start_time => 1.day.ago, :end_time => 10.minutes.ago, :instance_types => ["c1.medium", "m1.small"], :product_description => "Linux/UNIX" ) #=> [{:product_description=>"Linux/UNIX", :timestamp=>"2010-02-04T05:44:36.000Z", :spot_price=>0.031, :instance_type=>"m1.small"}, {:product_description=>"Linux/UNIX", :timestamp=>"2010-02-04T17:56:25.000Z", :spot_price=>0.058, :instance_type=>"c1.medium"}, ... ]
Describe Subnet.
ec2.describe_subnets #=> [{:available_ip_address_count=>"251", :vpc_id=>"vpc-890ce2e0", :availability_zone=>"us-east-1a", :subnet_id=>"subnet-770de31e", :cidr_block=>"10.0.1.0/24", :state=>"available"}]
Describe all EBS volumes.
ec2.describe_volumes #=> [{:aws_size => 94, :aws_device => "/dev/sdc", :aws_attachment_status => "attached", :zone => "merlot", :snapshot_id => nil, :aws_attached_at => "2008-06-18T08:19:28.000Z", :aws_status => "in-use", :aws_id => "vol-60957009", :aws_created_at => "2008-06-18T08:19:20.000Z", :aws_instance_id => "i-c014c0a9"}, {:aws_size => 1, :zone => "merlot", :snapshot_id => nil, :aws_status => "available", :aws_id => "vol-58957031", :aws_created_at => Wed Jun 18 08:19:21 UTC 2008,}, ... ]
Describe VPCs
ec2.describe_vpcs #=> [{:vpc_id=>"vpc-890ce2e0", :dhcp_options_id=>"default", :cidr_block=>"10.0.0.0/23", :state=>"available"}] ec2.describe_vpcs("vpc-890ce2e0")
Describe VPN connections.
ec2.describe_vpn_connections #=> [{:type=>"ipsec.1", :vpn_connection_id=>"vpn-a9a643c0", :customer_gateway_configuration=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<vpn_connection id=\"vpn-a9a643c0\">\n...</vpn_connection>\n", :state=>"available", :vpn_gateway_id=>"vgw-dfa144b6", :customer_gateway_id=>"cgw-81a643e8"}]
Describe VPN gateways.
ec2.describe_vpn_gateways #=> [{:type=>"ipsec.1", :availability_zone=>"us-east-1a", :attachments=>[{:vpc_id=>"vpc-890ce2e0", :state=>"attached"}], :vpn_gateway_id=>"vgw-dfa144b6"}]
Detach the specified EBS volume from the instance to which it is attached.
ec2.detach_volume('vol-898a6fe0') #=> { :aws_instance_id => "i-7c905415", :aws_device => "/dev/sdh", :aws_status => "detaching", :aws_attached_at => "2008-03-28T14:38:34.000Z", :aws_id => "vol-898a6fe0"}
Disassociate the specified elastic IP address from the instance to which it is assigned. Returns true or an exception.
ec2.disassociate_address('75.101.154.140') #=> true
Edit group permissions.
action - :authorize (or :grant) | :revoke (or :remove) group_name - security group name params - a combination of options below: :source_group_owner => grantee id :source_group => grantee group name :from_port => from port :to_port => to port :port => set both :from_port and to_port with the same value :protocol => :tcp | :udp | :icmp :cidr_ip => '0.0.0.0/0' ec2.edit_security_group( :grant, 'kd-sg-test', :source_group => "sketchy", :source_group_owner => "600000000006", :protocol => 'tcp', :port => '80', :cidr_ip => '127.0.0.1/32') #=> true
P.S. setting both group based and port based ingresses is not supported by Amazon but by Eucalyptus.
Retreive EC2 instance OS logs. Returns a hash of data or an exception.
ec2.get_console_output('i-f222222d') => {:aws_instance_id => 'i-f222222d', :aws_timestamp => "2007-05-23T14:36:07.000-07:00", :timestamp => Wed May 23 21:36:07 UTC 2007, # Time instance :aws_output => "Linux version 2.6.16-xenU (builder@patchbat.amazonsa) (gcc version 4.0.1 20050727 ..."
Get initial Windows Server setup password from an instance console output.
my_awesome_key = ec2.create_key_pair('my_awesome_key') #=> {:aws_key_name => "my_awesome_key", :aws_fingerprint => "01:02:03:f4:25:e6:97:e8:9b:02:1a:26:32:4e:58:6b:7a:8c:9f:03", :aws_material => "-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAK...Q8MDrCbuQ=\n-----END RSA PRIVATE KEY-----"} my_awesome_instance = ec2.run_instances('ami-a000000a',1,1,['my_awesome_group'],'my_awesome_key', 'WindowsInstance!!!') #=> [{:aws_image_id => "ami-a000000a", :aws_instance_id => "i-12345678", ... :aws_availability_zone => "us-east-1b" }] # wait until instance enters 'operational' state and get it's initial password puts ec2.get_initial_password(my_awesome_instance[:aws_instance_id], my_awesome_key[:aws_material]) #=> "MhjWcgZuY6"
Get Initial windows instance password using Amazon API call GetPasswordData.
puts ec2.get_initial_password_v2(my_awesome_instance[:aws_instance_id], my_awesome_key[:aws_material]) #=> "MhjWcgZuY6"
P.S. To say the truth there is absolutely no any speedup if to compare to the old get_initial_password method… ;(
Launch new EC2 instances. Options: :image_id, :addressing_type, :min_count, max_count, :key_name, :kernel_id, :ramdisk_id, :availability_zone, :monitoring_enabled, :subnet_id, :disable_api_termination, :instance_initiated_shutdown_behavior, :block_device_mappings
Returns a list of launched instances or an exception.
ec2.launch_instances( 'ami-c2a3f5d4', :min_count => 1, :group_ids => 'default', :user_data => 'Ohoho!', :availability_zone => "us-east-1a", :disable_api_termination => true, :instance_initiated_shutdown_behavior => 'terminate', :block_device_mappings => [ {:ebs_snapshot_id=>"snap-7360871a", :ebs_delete_on_termination=>true, :device_name => "/dev/sdk", :virtual_name => "mystorage"} ] ) #=> [{:aws_image_id=>"ami-c2a3f5d4", :dns_name=>"", :aws_instance_type=>"m1.small", :aws_owner=>"826693181925", :root_device_name=>"/dev/sda1", :instance_class=>"elastic", :state_reason_code=>0, :aws_state=>"pending", :private_dns_name=>"", :aws_reason=>"", :aws_launch_time=>"2009-11-18T14:03:25.000Z", :aws_reservation_id=>"r-54d38542", :state_reason_message=>"pending", :aws_state_code=>0, :ami_launch_index=>"0", :aws_availability_zone=>"us-east-1a", :aws_groups=>["default"], :monitoring_state=>"disabled", :aws_product_codes=>[], :ssh_key_name=>"", :aws_instance_id=>"i-8ce84ae4"}]
Modify an image‘s attributes. It is recommended that you use modify_image_launch_perm_add_users, modify_image_launch_perm_remove_users, etc. instead of modify_image_attribute because the signature of modify_image_attribute may change with EC2 service changes.
attribute : currently, only 'launchPermission' is supported. operation_type : currently, only 'add' & 'remove' are supported. vars: :user_group : currently, only 'all' is supported. :user_id :product_code
Add image launch permissions for users groups (currently only ‘all’ is supported, which gives public launch permissions). Returns true or an exception.
ec2.modify_image_launch_perm_add_groups('ami-e444444d') #=> true
Grant image launch permissions to users. Parameter user_id is a list of user AWS account ids. Returns true or an exception.
ec2.modify_image_launch_perm_add_users('ami-e444444d',['000000000777','000000000778']) #=> true
Remove image launch permissions for users groups (currently only ‘all’ is supported, which gives public launch permissions).
ec2.modify_image_launch_perm_remove_groups('ami-e444444d') #=> true
Revokes image launch permissions for users. user_id is a list of users AWS accounts ids. Returns true or an exception.
ec2.modify_image_launch_perm_remove_users('ami-e444444d',['000000000777','000000000778']) #=> true
Modify instance attribute. Attributes: :instance_type, :kernel, :ramdisk, :user_data, :disable_api_termination, :instance_initiated_shutdown_behavior, :root_device_name, :block_device_mapping
ec2.modify_instance_attribute(instance, :instance_initiated_shutdown_behavior, "stop") #=> true
Modify snapshot attribute.
attribute : currently, only 'createVolumePermission' is supported. operation_type : currently, only 'add' & 'remove' are supported. vars: :user_group : currently, only 'all' is supported. :user_id : an array of user ids
Grant create volume permission for user groups (currently only ‘all’ is supported).
ec2.modify_snapshot_attribute_create_volume_permission_add_groups('snap-36fe435f') #=> true
Grant create volume permission for a list of users.
ec2.modify_snapshot_attribute_create_volume_permission_add_users('snap-36fe435f', '000000000000', '000000000001') #=> true
Remove create volume permission for user groups (currently only ‘all’ is supported).
ec2.modify_snapshot_attribute_create_volume_permission_remove_groups('snap-36fe435f') #=> true
Revoke create volume permission for a list of users.
ec2.modify_snapshot_attribute_create_volume_permission_remove_users('snap-36fe435f', '000000000000', '000000000001') #=> true
Enables monitoring for a running instances. For more information, refer to the Amazon CloudWatch Developer Guide.
ec2.monitor_instances('i-8437ddec') #=> {:instance_id=>"i-8437ddec", :monitoring_state=>"pending"}
Purchase a Reserved Instance. Returns ReservedInstancesId value.
ec2.purchase_reserved_instances_offering('e5a2ff3b-f6eb-4b4e-83f8-b879d7060257', 3) # => '4b2293b4-5813-4cc8-9ce3-1957fc1dcfc8'
Reboot an EC2 instance. Returns true or an exception.
ec2.reboot_instances(['i-f222222d','i-f222222e']) #=> true
Register new image at Amazon. Options: :image_location, :name, :description, :architecture, :kernel_id, :ramdisk_id, :root_device_name, :block_device_mappings.
Returns new image id.
# Register S3 image ec2.register_image('bucket_for_k_dzreyev/image_bundles/kd__CentOS_1_10_2009_10_21_13_30_43_MSD/image.manifest.xml') #=> 'ami-e444444d' # or image_reg_params = { :image_location => 'bucket_for_k_dzreyev/image_bundles/kd__CentOS_1_10_2009_10_21_13_30_43_MSD/image.manifest.xml', :name => 'my-test-one-1', :description => 'My first test image' } ec2.register_image(image_reg_params) #=> "ami-bca1f7aa" # Register EBS image image_reg_params = { :name => 'my-test-image', :description => 'My first test image', :root_device_name => "/dev/sda1", :block_device_mappings => [ { :ebs_snapshot_id=>"snap-7360871a", :ebs_delete_on_termination=>true, :device_name=>"/dev/sda1"} ] } ec2.register_image(image_reg_params) #=> "ami-b2a1f7a4"
Release an elastic IP address associated with your account. Returns true or an exception.
ec2.release_address('75.101.154.140') #=> true
Create a Spot Instance request.
Mandatory params: :image_id, :spot_price, :instance_type Optional params: :valid_from, :valid_until, :instance_count, :type, :launch_group, :availability_zone_group, :key_name, :user_data, :addressing_type, :kernel_id, :ramdisk_id, :subnet_id, :availability_zone, :monitoring_enabled, :groups, :block_device_mappings
ec2.request_spot_instances( :image_id => 'ami-08f41161', :spot_price => 0.01, :key_name => 'tim', :instance_count => 2, :groups => ['33','default'], :instance_type => 'c1.medium') #=> [{:product_description=>"Linux/UNIX", :type=>"one-time", :spot_instance_requestId=>"sir-7a893003", :monitoring_enabled=>false, :image_id=>"ami-08f41161", :state=>"open", :spot_price=>0.01, :groups=>["default", "33"], :key_name=>"tim", :create_time=>"2010-03-10T10:33:09.000Z", :instance_type=>"c1.medium"}, {:product_description=>"Linux/UNIX", :type=>"one-time", :spot_instance_requestId=>"sir-13dc9a03", :monitoring_enabled=>false, :image_id=>"ami-08f41161", :state=>"open", :spot_price=>0.01, :groups=>["default", "33"], :key_name=>"tim", :create_time=>"2010-03-10T10:33:09.000Z", :instance_type=>"c1.medium"}] ec2.request_spot_instances( :image_id => 'ami-08f41161', :spot_price => 0.01, :instance_type => 'm1.small', :valid_from => 10.minutes.since, :valid_until => 1.hour.since, :instance_count => 1, :key_name => 'tim', :groups => ['33','default'], :availability_zone => 'us-east-1a', :monitoring_enabled => true, :launch_group => 'lg1', :availability_zone_group => 'azg1', :block_device_mappings => [ { :device_name => '/dev/sdk', :ebs_snapshot_id => 'snap-145cbc7d', :ebs_delete_on_termination => true, :ebs_volume_size => 3, :virtual_name => 'ephemeral2' } ] ) #=> [{:monitoring_enabled=>true, :type=>"one-time", :image_id=>"ami-08f41161", :launch_group=>"lg1", :state=>"open", :valid_until=>"2010-02-05T19:13:44.000Z", :create_time=>"2010-02-05T18:13:46.000Z", :availability_zone_group=>"azg1", :spot_price=>0.01, :block_device_mappings=> [{:ebs_delete_on_termination=>true, :ebs_volume_size=>3, :virtual_name=>"ephemeral2", :device_name=>"/dev/sdk", :ebs_snapshot_id=>"snap-145cbc7d"}], :instance_type=>"m1.small", :groups=>["default", "33"], :product_description=>"Linux/UNIX", :key_name=>"tim", :valid_from=>"2010-02-05T18:23:44.000Z", :availability_zone=>"us-east-1a", :spot_instance_request_id=>"sir-32da8a03"}]
Reset image attribute. Currently, only ‘launchPermission’ is supported. Returns true or an exception.
ec2.reset_image_attribute('ami-e444444d') #=> true
Describe instance attribute. Attributes: :kernel, :ramdisk
ec2.reset_instance_attribute(instance, :kernel) #=> true
Reset permission settings for the specified snapshot.
ec2.reset_snapshot_attribute('snap-cecd29a7') #=> true
Remove permission from a security group. Returns true or an exception. protocol is one of :’tcp’|’udp’|’icmp’ (‘tcp’ is default).
ec2.revoke_security_group_IP_ingress('my_awesome_group', 80, 82, 'udp', '192.168.1.0/8') #=> true
Revoke named ingress for security group.
ec2.revoke_security_group_named_ingress('my_awesome_group', aws_user_id, 'another_group_name') #=> true
Launch new EC2 instances. Returns a list of launched instances or an exception.
ec2.run_instances('ami-e444444d',1,1,['my_awesome_group'],'my_awesome_key', 'Woohoo!!!', 'public') #=> [{:aws_image_id => "ami-e444444d", :aws_reason => "", :aws_state_code => "0", :aws_owner => "000000000888", :aws_instance_id => "i-123f1234", :aws_reservation_id => "r-aabbccdd", :aws_state => "pending", :dns_name => "", :ssh_key_name => "my_awesome_key", :aws_groups => ["my_awesome_group"], :private_dns_name => "", :aws_instance_type => "m1.small", :aws_launch_time => "2008-1-1T00:00:00.000Z" :aws_ramdisk_id => "ari-8605e0ef" :aws_kernel_id => "aki-9905e0f0", :ami_launch_index => "0", :aws_availability_zone => "us-east-1b" }]
Start instances.
ec2.start_instances("i-36e84a5e") #=> [{:aws_prev_state_name=>"stopped", :aws_instance_id=>"i-36e84a5e", :aws_current_state_code=>16, :aws_current_state_name=>"running", :aws_prev_state_code=>80}]
Stop instances.
ec2.stop_instances("i-36e84a5e") #=> [{:aws_prev_state_code=>16, :aws_prev_state_name=>"running", :aws_instance_id=>"i-36e84a5e", :aws_current_state_code=>64, :aws_current_state_name=>"stopping"}]
Terminates EC2 instances. Returns a list of termination params or an exception.
ec2.terminate_instances(['i-cceb49a4']) #=> [{:aws_instance_id=>"i-cceb49a4", :aws_current_state_code=>32, :aws_current_state_name=>"shutting-down", :aws_prev_state_code=>16, :aws_prev_state_name=>"running"}]
Create a snapshot of specified volume, but with the normal retry algorithms disabled. This method will return immediately upon error. The user can specify connect and read timeouts (in s) for the connection to AWS. If the user does not specify timeouts, try_create_snapshot uses the default values in Rightscale::HttpConnection.
ec2.try_create_snapshot('vol-898a6fe0', 'KD: WooHoo!!') #=> {:aws_volume_id=>"vol-e429db8d", :aws_started_at=>"2009-10-01T09:23:38.000Z", :aws_description=>"KD: WooHoo!!", :aws_owner=>"648770000000", :aws_progress=>"", :aws_status=>"pending", :aws_volume_size=>1, :aws_id=>"snap-3df54854"}