ruby/example_code/ec2/ec2-ruby-example-manage-instances.rb
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
# 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-west-2'),
#     'i-033c48ef067af3dEX'
#   )

Attempts to enabled detailed monitoring for an
Amazon Elastic Compute Cloud (Amazon EC2) instance.



The Amazon EC2 instance.
Attempts to enabled detailed monitoring for an Amazon Elastic Compute Cloud ( Amazon EC2 ) instance . The Amazon EC2 instance .
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
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
Attempts to enabled detailed monitoring for an
Amazon Elastic Compute Cloud (Amazon EC2) instance.
Attempts to enabled detailed monitoring for an Amazon Elastic Compute Cloud ( Amazon EC2 ) instance .
true if detailed monitoring was enabled; otherwise, false.
true if detailed monitoring was enabled ; otherwise false .
Boolean
ec2_client
An initialized EC2 client.
An initialized EC2 client .
instance_id
The ID of the instance.
The ID of the instance .
example
exit 1 unless instance_detailed_monitoring_enabled?( Aws::EC2::Client.new(region: 'us-west-2'), 'i-033c48ef067af3dEX' )
exit 1 unless instance_detailed_monitoring_enabled? ( Aws :: EC2 :: Client . new ( region : ' us - west - 2 ' ) ' i - 033c48ef067af3dEX ' )