|
NAMEVM::EC2::REST::instance - VM::EC2 methods for controlling instancesSYNOPSISuse VM::EC2 ':standard'; METHODSThe methods in this section allow you to retrieve information about EC2 instances, launch new instances, control the instance lifecycle (e.g. starting and stopping them), and fetching the console output from instances.Implemented: ConfirmProductInstance DescribeInstanceAttribute DescribeInstances DescribeInstanceStatus ModifyInstanceAttribute RebootInstances ResetInstanceAttribute RunInstances StartInstances StopInstances TerminateInstances Unimplemented: (none) The primary object manipulated by these methods is VM::EC2::Instance. Please see the VM::EC2::Instance manual page for additional methods that allow you to attach and detach volumes, modify an instance's attributes, and convert instances into images. @instances = $ec2->describe_instances(@instance_ids)@instances = $ec2->describe_instances(\%filters)@instances = $ec2->describe_instances(-instance_id=>\@ids,-filter=>\%filters)Return a series of VM::EC2::Instance objects. Optional arguments are:-instance_id ID of the instance(s) to return information on. This can be a string scalar, or an arrayref. -filter Tags and other filters to apply. The filter argument is a hashreference in which the keys are the filter names, and the values are the match strings. Some filters accept wildcards. A typical filter example: $ec2->describe_instances( -filter => {'block-device-mapping.device-name'=>'/dev/sdh', 'architecture' => 'i386', 'tag:Role' => 'Server' }); You may omit the -filter argument name if there are no other arguments: $ec2->describe_instances({'block-device-mapping.device-name'=>'/dev/sdh', 'architecture' => 'i386', 'tag:Role' => 'Server'}); There are a large number of filters, which are listed in full at http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html. Here is a alpha-sorted list of filter names: architecture, availability-zone, block-device-mapping.attach-time, block-device-mapping.delete-on-termination, block-device-mapping.device-name, block-device-mapping.status, block-device-mapping.volume-id, client-token, dns-name, group-id, group-name, hypervisor, image-id, instance-id, instance-lifecycle, instance-state-code, instance-state-name, instance-type, instance.group-id, instance.group-name, ip-address, kernel-id, key-name, launch-index, launch-time, monitoring-state, owner-id, placement-group-name, platform, private-dns-name, private-ip-address, product-code, ramdisk-id, reason, requester-id, reservation-id, root-device-name, root-device-type, source-dest-check, spot-instance-request-id, state-reason-code, state-reason-message, subnet-id, tag-key, tag-value, tag:key, virtualization-type, vpc-id. Note that the objects returned from this method are the instances themselves, and not a reservation set. The reservation ID can be retrieved from each instance by calling its reservationId() method. @i = $ec2->run_instances($ami_id)@i = $ec2->run_instances(-image_id=>$id,%other_args)This method will provision and launch one or more instances given an AMI ID. If successful, the method returns a series of VM::EC2::Instance objects.If called with a single argument this will be interpreted as the AMI to launch, and all other arguments will take their defaults. Otherwise, the arguments will be taken as a -parameter=>$argument list.
@s = $ec2->start_instances(@instance_ids)@s = $ec2->start_instances(-instance_id=>\@instance_ids)Start the instances named by @instance_ids and return one or more VM::EC2::Instance::State::Change objects.To wait for the all the instance ids to reach their final state ("running" unless an error occurs), call wait_for_instances(). Example: # find all stopped instances @instances = $ec2->describe_instances(-filter=>{'instance-state-name'=>'stopped'}); # start them $ec2->start_instances(@instances) # pause till they are running (or crashed) $ec2->wait_for_instances(@instances) You can also start an instance by calling the object's start() method: $instances[0]->start('wait'); # start instance and wait for it to # be running The objects returned by calling start_instances() indicate the current and previous states of the instance. The previous state is typically "stopped" and the current state is usually "pending." This information is only current to the time that the start_instances() method was called. To get the current run state of the instance, call its status() method: die "ouch!" unless $instances[0]->current_status eq 'running'; @s = $ec2->stop_instances(@instance_ids)@s = $ec2->stop_instances(-instance_id=>\@instance_ids,-force=>1)Stop the instances named by @instance_ids and return one or more VM::EC2::Instance::State::Change objects. In the named parameter version of this method, you may optionally provide a -force argument, which if true, forces the instance to halt without giving it a chance to run its shutdown procedure (the equivalent of pulling a physical machine's plug).To wait for instances to reach their final state, call wait_for_instances(). Example: # find all running instances @instances = $ec2->describe_instances(-filter=>{'instance-state-name'=>'running'}); # stop them immediately and wait for confirmation $ec2->stop_instances(-instance_id=>\@instances,-force=>1); $ec2->wait_for_instances(@instances); You can also stop an instance by calling the object's start() method: $instances[0]->stop('wait'); # stop first instance and wait for it to # stop completely @s = $ec2->terminate_instances(@instance_ids)@s = $ec2->terminate_instances(-instance_id=>\@instance_ids)Terminate the instances named by @instance_ids and return one or more VM::EC2::Instance::State::Change objects. This method will fail for any instances whose termination protection field is set.To wait for the all the instances to reach their final state, call wait_for_instances(). Example: # find all instances tagged as "Version 0.5" @instances = $ec2->describe_instances({'tag:Version'=>'0.5'}); # terminate them $ec2->terminate_instances(@instances); You can also terminate an instance by calling its terminate() method: $instances[0]->terminate; @s = $ec2->reboot_instances(@instance_ids)@s = $ec2->reboot_instances(-instance_id=>\@instance_ids)Reboot the instances named by @instance_ids and return one or more VM::EC2::Instance::State::Change objects.To wait for the all the instances to reach their final state, call wait_for_instances(). You can also reboot an instance by calling its terminate() method: $instances[0]->reboot; $boolean = $ec2->confirm_product_instance($instance_id,$product_code,$callback)$boolean = $ec2->confirm_product_instance(-instance_id=>$instance_id,-product_code=>$product_code,-cb=>$callback)Return "true" if the instance indicated by $instance_id is associated with the given product code.$meta = VM::EC2->instance_metadata$meta = $ec2->instance_metadataFor use on running EC2 instances only: This method returns a VM::EC2::Instance::Metadata object that will return information about the currently running instance using the HTTP:// metadata fields described at http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instancedata-data-categories.html. This is usually fastest way to get runtime information on the current instance.Note that this method can be called as either an instance or a class method. @data = $ec2->describe_instance_attribute($instance_id,$attribute,$callback)This method returns instance attributes. Only one attribute can be retrieved at a time. The following is the list of attributes that can be retrieved:instanceType -- scalar kernel -- scalar ramdisk -- scalar userData -- scalar disableApiTermination -- scalar instanceInitiatedShutdownBehavior -- scalar rootDeviceName -- scalar blockDeviceMapping -- list of hashref sourceDestCheck -- scalar groupSet -- list of scalar productCodes -- list of hashref ebsOptimized -- scalar sriovNetSupport -- scalar All of these values can be retrieved more conveniently from the VM::EC2::Instance object returned from describe_instances(), so there is no attempt to parse the results of this call into Perl objects. Therefore, some of the attributes, in particular 'blockDeviceMapping' will be returned as raw hashrefs. $boolean = $ec2->modify_instance_attribute($instance_id,-$attribute_name=>$value)This method changes instance attributes. It can only be applied to stopped instances. The following is the list of attributes that can be set:-instance_type -- type of instance, e.g. "m1.small" -kernel -- kernel id -ramdisk -- ramdisk id -user_data -- user data -termination_protection -- true to prevent termination from the console -disable_api_termination -- same as the above -shutdown_behavior -- "stop" or "terminate" -instance_initiated_shutdown_behavior -- same as above -root_device_name -- root device name -source_dest_check -- enable NAT (VPC only) -group_id -- VPC security group -block_devices -- Specify block devices to change deleteOnTermination flag -block_device_mapping -- Alias for -block_devices -ebs_optimization -- EBS Optmization -sriov_net_support -- Enhanced networking support Only one attribute can be changed in a single request. For example: $ec2->modify_instance_attribute('i-12345',-kernel=>'aki-f70657b2'); The result code is true if the attribute was successfully modified, false otherwise. In the latter case, $ec2->error() will provide the error message. The ability to change the deleteOnTermination flag for attached block devices is not documented in the official Amazon API documentation, but appears to work. The syntax is: # turn on deleteOnTermination $ec2->modify_instance_attribute(-block_devices=>'/dev/sdf=v-12345') # turn off deleteOnTermination $ec2->modify_instance_attribute(-block_devices=>'/dev/sdf=v-12345') The syntax is slightly different from what is used by -block_devices in run_instances(), and is "device=volumeId:boolean". Multiple block devices can be specified using an arrayref. $boolean = $ec2->reset_instance_attribute($instance_id,$attribute [,$callback])This method resets an attribute of the given instance to its default value. Valid attributes are "kernel", "ramdisk" and "sourceDestCheck". The result code is true if the reset was successful.@status_list = $ec2->describe_instance_status(@instance_ids);@status_list = $ec2->describe_instance_status(-instance_id=>\@ids,-filter=>\%filters,%other_args);@status_list = $ec2->describe_instance_status(\%filters);This method returns a list of VM::EC2::Instance::Status objects corresponding to status checks and scheduled maintenance events on the instances of interest. You may provide a list of instances to return information on, a set of filters, or both.The filters are described at http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstanceStatus.html. The brief list is: availability-zone, event.code, event.description, event.not-after, event.not-before, instance-state-name, instance-state-code, system-status.status, system-status.reachability, instance-status.status, instance-status.reachability. Request arguments are: -instance_id Scalar or array ref containing the instance ID(s) to return information about (optional). -filter Filters to apply (optional). -include_all_instances If true, include all instances, including those that are stopped, pending and shutting down. Otherwise, returns the status of running instances only. -max_results An integer corresponding to the number of instance items per response (must be greater than 5). If -max_results is specified, then the call will return at most the number of instances you requested. You may see whether there are additional results by calling more_instance_status(), and then retrieve the next set of results with additional call(s) to describe_instance_status(): my @results = $ec2->describe_instance_status(-max_results => 10); do_something(\@results); while ($ec2->more_instance_status) { @results = $ec2->describe_instance_status; do_something(\@results); } NOTE: As of 29 July 2012, passing -include_all_instances causes an EC2 "unknown parameter" error, indicating some mismatch between the documented API and the actual one. $t = $ec2->tokenReturn a client token for use with start_instances().$ec2->wait_for_instances(@instances)Wait for all members of the provided list of instances to reach some terminal state ("running", "stopped" or "terminated"), and then return a hash reference that maps each instance ID to its final state.Typical usage: my @instances = $image->run_instances(-key_name =>'My_key', -security_group=>'default', -min_count =>2, -instance_type => 't1.micro') or die $ec2->error_str; my $status = $ec2->wait_for_instances(@instances); my @failed = grep {$status->{$_} ne 'running'} @instances; print "The following failed: @failed\n"; If no terminal state is reached within a set timeout, then this method returns undef and sets $ec2->error_str() to a suitable message. The timeout, which defaults to 10 minutes (600 seconds), can be get or set with $ec2->wait_for_timeout(). SEE ALSOVM::EC2AUTHORLincoln Stein <lincoln.stein@gmail.com>.Copyright (c) 2011 Ontario Institute for Cancer Research This package and its accompanying libraries is free software; you can redistribute it and/or modify it under the terms of the GPL (either version 1, or at your option, any later version) or the Artistic License 2.0. Refer to LICENSE for the full license text. In addition, please see DISCLAIMER.txt for disclaimers of warranty.
Visit the GSP FreeBSD Man Page Interface. |