Darksheer
Darksheer

Reputation: 53

Ruby aws-sk: undefined method

Trying to get ruby working with the ruby AWS sdk, but keep getting the following error:

aws.rb:10:in <main>': undefined methodinstances' for # (NoMethodError)

From other postings on StackOverflow, v2 of the aws-sdk changed from AWS to Aws. I also tried Aws::EC2.new

require 'aws-sdk'
ec2 = Aws::EC2::Client.new(
    access_key_id: 'mudd',
    secret_access_key: 'butt',
    region: 'us-east-1'
)

ec2.instances

Upvotes: 1

Views: 935

Answers (1)

Trevor Rowe
Trevor Rowe

Reputation: 6528

V1 and v2 of the Ruby SDK have different approaches here. In v2 you decide between using the client APIs or the resource APIs. The client APIs provide a 1-to-1 mapping of methods to API operations. The resource apis provide an object oriented interface similar to the V1 SDK. You appear to be looking for this latter interface.

Resource interface return objects that have actions/methods defined, such as Aws::EC2::Instance#terminate.

ec2 = Aws::EC2::Resource.new
ec2.instances.each do |instance|
  puts instance.id
end

Alternatively, you can use the Client API. Clients return data-only objects that have similar attributes, but no other actions/methods.

ec2 = Aws::EC2::Client.new
ec2.describe_instances.each do |page|
  page.reservations.each do |reservation|
    reservation.instances.each do |instance|
      puts instance.instance_id
    end
  end
end

Upvotes: 3

Related Questions