管理 Amazon EC2 实例 - 适用于 Ruby 的 Amazon 开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

管理 Amazon EC2 实例

下面的代码示例:

  1. 停止 Amazon EC2 实例。

  2. 重新启动实例。

  3. 重新启动实例。

  4. 启用实例的详细监控。

  5. 显示有关可用实例的信息。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 # This code example does the following: # 1. Stops an Amazon Elastic Compute Cloud (Amazon EC2) instance. # 2. Restarts the instance. # 3. Reboots the instance. # 4. Enables detailed monitoring for the instance. # 5. Displays information about available instances. require 'aws-sdk-ec2' # Waits for an Amazon Elastic Compute Cloud (Amazon EC2) instance # to reach the specified state. # # Prerequisites: # # - The Amazon EC2 instance. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param instance_state [Symbol] The desired instance state. # @param instance_id [String] The ID of the instance. # @example # wait_for_instance( # Aws::EC2::Client.new(region: 'us-east-1'), # :instance_stopped, # 'i-033c48ef067af3dEX' # ) def wait_for_instance(ec2_client, instance_state, instance_id) ec2_client.wait_until(instance_state, instance_ids: [instance_id]) puts "Success: #{instance_state}." rescue Aws::Waiters::Errors::WaiterFailed => e puts "Failed: #{e.message}" end # 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-033c48ef067af3dEX' # ) def instance_stopped?(ec2_client, instance_id) ec2_client.stop_instances(instance_ids: [instance_id]) wait_for_instance(ec2_client, :instance_stopped, instance_id) return true rescue StandardError => e puts "Error stopping instance: #{e.message}" return false end # Attempts to restart 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 restarted; otherwise, false. # @example # exit 1 unless instance_restarted?( # Aws::EC2::Client.new(region: 'us-east-1'), # 'i-033c48ef067af3dEX' # ) def instance_restarted?(ec2_client, instance_id) ec2_client.start_instances(instance_ids: [instance_id]) wait_for_instance(ec2_client, :instance_running, instance_id) return true rescue StandardError => e puts "Error restarting instance: #{e.message}" return false end # Attempts to reboot 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 rebooted; otherwise, false. # @example # exit 1 unless instance_rebooted?( # Aws::EC2::Client.new(region: 'us-east-1'), # 'i-033c48ef067af3dEX' # ) def instance_rebooted?(ec2_client, instance_id) ec2_client.reboot_instances(instance_ids: [instance_id]) wait_for_instance(ec2_client, :instance_status_ok, instance_id) return true rescue StandardError => e puts "Error rebooting instance: #{e.message}" return false end # Attempts to enabled detailed monitoring for 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 detailed monitoring was enabled; otherwise, false. # @example # exit 1 unless instance_detailed_monitoring_enabled?( # Aws::EC2::Client.new(region: 'us-east-1'), # 'i-033c48ef067af3dEX' # ) def instance_detailed_monitoring_enabled?(ec2_client, instance_id) result = ec2_client.monitor_instances(instance_ids: [instance_id]) puts "Detailed monitoring state: #{result.instance_monitorings[0].monitoring.state}" return true rescue Aws::EC2::Errors::InvalidState puts "The instance is not in a monitorable state. Skipping this step." return false rescue StandardError => e puts "Error enabling detailed monitoring: #{e.message}" return false end # Displays information about available # Amazon Elastic Compute Cloud (Amazon EC2) instances. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @example # list_instances_information(Aws::EC2::Client.new(region: 'us-east-1')) def list_instances_information(ec2_client) result = ec2_client.describe_instances result.reservations.each do |reservation| if reservation.instances.count.positive? reservation.instances.each do |instance| puts '-' * 12 puts "Instance ID: #{instance.instance_id}" puts "State: #{instance.state.name}" puts "Image ID: #{instance.image_id}" puts "Instance type: #{instance.instance_type}" puts "Architecture: #{instance.architecture}" puts "IAM instance profile ARN: #{instance.iam_instance_profile.arn}" 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.positive? puts 'Tags:' instance.tags.each do |tag| puts " #{tag.key}/#{tag.value}" end end end end end 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-manage-instances.rb ' \ 'INSTANCE_ID REGION' puts 'Example: ruby ec2-ruby-example-manage-instances.rb ' \ 'i-033c48ef067af3dEX 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-033c48ef067af3dEX' 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 the instance. ' \ 'This might take a few minutes...' unless instance_stopped?(ec2_client, instance_id) puts 'Cannot stop the instance. Skipping this step.' end puts "\nAttempting to restart the instance. " \ 'This might take a few minutes...' unless instance_restarted?(ec2_client, instance_id) puts 'Cannot restart the instance. Skipping this step.' end puts "\nAttempting to reboot the instance. " \ 'This might take a few minutes...' unless instance_rebooted?(ec2_client, instance_id) puts 'Cannot reboot the instance. Skipping this step.' end puts "\nAttempting to enable detailed monitoring for the instance..." unless instance_detailed_monitoring_enabled?(ec2_client, instance_id) puts 'Cannot enable detailed monitoring for the instance. ' \ 'Skipping this step.' end puts "\nInformation about available instances:" list_instances_information(ec2_client) end run_me if $PROGRAM_NAME == __FILE__