Stopping an Amazon EC2 Instance - Amazon SDK for Ruby
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Stopping an Amazon EC2 Instance

The following example attempts to stop the specified Amazon EC2 instance.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 require 'aws-sdk-ec2' # Attempts to stop an Amazon Elastic Compute Cloud (Amazon EC2) instance. # # Prerequisites: # # - The Amazon EC2 instance. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param instance_id [String] The ID of the instance. # @return [Boolean] true if the instance was stopped; otherwise, false. # @example # exit 1 unless instance_stopped?( # Aws::EC2::Client.new(region: 'us-east-1'), # 'i-123abc' # ) def instance_stopped?(ec2_client, instance_id) response = ec2_client.describe_instance_status(instance_ids: [instance_id]) if response.instance_statuses.count.positive? state = response.instance_statuses[0].instance_state.name case state when 'stopping' puts 'The instance is already stopping.' return true when 'stopped' puts 'The instance is already stopped.' return true when 'terminated' puts 'Error stopping instance: ' \ 'the instance is terminated, so you cannot stop it.' return false end end ec2_client.stop_instances(instance_ids: [instance_id]) ec2_client.wait_until(:instance_stopped, instance_ids: [instance_id]) puts 'Instance stopped.' return true rescue StandardError => e puts "Error stopping instance: #{e.message}" return false end # Full example call: def run_me instance_id = '' region = '' # Print usage information and then stop. if ARGV[0] == '--help' || ARGV[0] == '-h' puts 'Usage: ruby ec2-ruby-example-stop-instance-i-123abc.rb ' \ 'INSTANCE_ID REGION ' puts 'Example: ruby ec2-ruby-example-start-instance-i-123abc.rb ' \ 'i-123abc us-east-1' exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? instance_id = 'i-123abc' region = 'us-east-1' # Otherwise, use the values as specified at the command prompt. else instance_id = ARGV[0] region = ARGV[1] end ec2_client = Aws::EC2::Client.new(region: region) puts "Attempting to stop instance '#{instance_id}' " \ '(this might take a few minutes)...' unless instance_stopped?(ec2_client, instance_id) puts 'Could not stop instance.' end end run_me if $PROGRAM_NAME == __FILE__