Alex Cohen Alex Cohen - 2 months ago 27
Ruby Question

Ruby aws-sdk describe-instance-status only for instances with events

I would like to use the ruby aws-sdk client function

to only return a list of instances with Scheduled Events. Here is my current attempt at this:

ec2 = Aws::EC2::Client.new(
region: ENV['REGION'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
)

ec2_events = ec2.describe_instance_status({
dry_run: false,
filters: [
{
name: "events",
values: ["event.description"],
},
],
})


Here is my resulting error message:

/Users/myusername/.gems/gems/aws-sdk-core-2.5.11/lib/seahorse/client/plugins/raise_response_errors.rb:15:in `call': The filter 'events' is invalid (Aws::EC2::Errors::InvalidParameterValue)
from /Users/myusername/.gems/gems/aws-sdk-core-2.5.11/lib/aws-sdk-core/plugins/param_converter.rb:20:in `call'
from /Users/myusername/.gems/gems/aws-sdk-core-2.5.11/lib/aws-sdk-core/plugins/response_paging.rb:26:in `call'
from /Users/myusername/.gems/gems/aws-sdk-core-2.5.11/lib/seahorse/client/plugins/response_target.rb:21:in `call'
from /Users/myusername/.gems/gems/aws-sdk-core-2.5.11/lib/seahorse/client/request.rb:70:in `send_request'
from /Users/myusername/.gems/gems/aws-sdk-core-2.5.11/lib/seahorse/client/base.rb:207:in `block (2 levels) in define_operation_methods'
from test.rb:30:in `<main>'


Is there a simple way to have aws-sdk praise out only the instances with Scheduled Events? I know that the regular aws cli tool has this option, but I really want to use the aws-sdk gem instead.

Answer

You have confused Name and Values in your code - it should be read as

ec2_events = ec2.describe_instance_status({
  dry_run: false,
  filters: [
    {
      name: "event.description",
      values: ["events"],
    },
  ],
})

You can check the full documentation http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstanceStatus.html

Filter (Name) possible values are

  • availability-zone - The Availability Zone of the instance.
  • event.code - The code for the scheduled event (instance-reboot | system-reboot | system-maintenance | instance-retirement | instance-stop).
  • event.description - A description of the event.
  • event.not-after - The latest end time for the scheduled event (for example, 2014-09-15T17:15:20.000Z).
  • event.not-before - The earliest start time for the scheduled event (for example, 2014-09-15T17:15:20.000Z).
  • instance-state-code - The code for the instance state, as a 16-bit unsigned integer. The high byte is an opaque internal value and should be ignored. The low byte is set based on the state represented. The valid values are 0 (pending), 16 (running), 32 (shutting-down), 48 (terminated), 64 (stopping), and 80 (stopped).
  • instance-state-name - The state of the instance (pending | running | shutting-down | terminated | stopping | stopped).
  • instance-status.reachability - Filters on instance status where the name is reachability (passed | failed | initializing | insufficient-data).
  • instance-status.status - The status of the instance (ok | impaired | initializing | insufficient-data | not-applicable).
  • system-status.reachability - Filters on system status where the name is reachability (passed | failed | initializing | insufficient-data).
  • system-status.status - The system status of the instance (ok | impaired | initializing | insufficient-data | not-applicable).