AWS SDK for Ruby
Developer Guide

Managing Amazon EC2 Instances

In this example, you use the AWS SDK for Ruby with Amazon EC2 to:

  1. Stop an existing Amazon EC2 instance by using Aws::EC2::Client#stop_instances.

  2. Restart the instance by using Aws::EC2::Client#start_instances.

  3. Reboot the instance by using Aws::EC2::Client#reboot_instances.

  4. Enable detailed monitoring for the instance by using Aws::EC2::Client#monitor_instances.

  5. Get information about available instances by using Aws::EC2::Client#describe_instances.

Prerequisites

Before running the example code, you need to install and configure the AWS SDK for Ruby, as described in:

You also need to replace INSTANCE-ID in the code with the instance ID of an existing EC2 instance.

Example

require 'aws-sdk-ec2' # v2: require 'aws-sdk' # Uncomment for Windows. # Aws.use_bundled_cert! def wait_for_instances(ec2, state, ids) begin ec2.wait_until(state, instance_ids: ids) puts "Success: #{state}." rescue Aws::Waiters::Errors::WaiterFailed => error puts "Failed: #{error.message}" end end ec2 = Aws::EC2::Client.new(region: 'us-east-1') instance_id = "INSTANCE-ID" # For example, "i-0a123456b7c8defg9" puts "Attempting to stop instance '#{instance_id}'. This may take a few minutes..." ec2.stop_instances({ instance_ids: [instance_id] }) wait_for_instances(ec2, :instance_stopped, [instance_id]) puts "\nAttempting to restart instance '#{instance_id}'. This may take a few minutes..." ec2.start_instances({ instance_ids: [instance_id] }) wait_for_instances(ec2, :instance_running, [instance_id]) puts "\nAttempting to reboot instance '#{instance_id}'. This may take a few minutes..." ec2.reboot_instances({ instance_ids: [instance_id] }) wait_for_instances(ec2, :instance_status_ok, [instance_id]) # Enable detailed monitoring for the instance. puts "\nAttempting to enable detailed monitoring for instance '#{instance_id}'..." begin monitor_instances_result = ec2.monitor_instances({ instance_ids: [instance_id] }) puts "Detailed monitoring state for instance '#{instance_id}': #{monitor_instances_result.instance_monitorings[0].monitoring.state}" rescue Aws::EC2::Errors::InvalidState puts "Instance '#{instance_id}' is not in a monitorable state. Continuing on..." end # Get information about available instances. puts "\nAvailable instances:" describe_instances_result = ec2.describe_instances describe_instances_result.reservations.each do |reservation| if reservation.instances.count > 0 reservation.instances.each do |instance| puts "=" * (instance.instance_id.length + 13) puts "Instance ID: #{instance.instance_id}" puts "State: #{instance.state.name}" puts "Image ID: #{instance.image_id}" puts "Instance Type: #{instance.instance_type}" puts "Architecure: #{instance.architecture}" puts "IAM Instance Profile: #{instance.iam_instance_profile}" puts "Key Name: #{instance.key_name}" puts "Launch Time: #{instance.launch_time}" puts "Detailed Monitoring State: #{instance.monitoring.state}" puts "Public IP Address: #{instance.public_ip_address}" puts "Public DNS Name: #{instance.public_dns_name}" puts "VPC ID: #{instance.vpc_id}" puts "Subnet ID: #{instance.subnet_id}" if instance.tags.count > 0 puts "Tags:" instance.tags.each do |tag| puts " #{tag.key} = #{tag.value}" end end end end end

On this page: