Skip to content
This repository was archived by the owner on Feb 4, 2026. It is now read-only.

Example listing and filtering EC2 instances and using stubs - #3

Open
jkanywhere wants to merge 6 commits into
aws-samples:masterfrom
jkanywhere:stub-example
Open

Example listing and filtering EC2 instances and using stubs#3
jkanywhere wants to merge 6 commits into
aws-samples:masterfrom
jkanywhere:stub-example

Conversation

@jkanywhere

Copy link
Copy Markdown

The API reference for AWS Ruby SDK v2 is quite good and detailed, but I struggle to find more complex examples showing how to put it together to achieve common functionality.

This example code shows

  • Using Resource interface to list EC2 instances
  • Client side filtering using regular expression match on Name tag
  • Using :stub_responses to mock API return values within RSpec

I hope this will help provide an even easier introduction to AWS Ruby SDK v2.

jkanywhereand others added 5 commits April 30, 2015 15:13
Client side filter for EC2 instances based on a Name tag pattern.
Use of RSpec to test that code using stubs.
RSpec example currently fails, and I do not know why.
Failures:
1) InstanceManager#instances returns only EC2 instances with matching Name tag
Failure/Error: expect(subject.instances.map(&:id)).to eq ["i-1", "i-2"]
expected: ["i-1", "i-2"]
got: ["i-1", "i-2", "i-1", "i-2"]
(compared using ==)
# ./spec/instance_manager_spec.rb:54:in `block (3 levels) in <top (required)>'
A very simple RSpec example showing how stub_responses affects the
Aws::EC2::Client interface correctly, but results in doubled resonses from the
Aws::EC2::Resource interface.
The default stub returns non-empty strings for all possible string
values. This causes the next token that trigger paging to appear
like there is a next response.
I updated your stubbs to use nil next tokens to avoid this issue.
Please feel free to open this as an issue against aws/aws-sdk-ruby as
this is a poor default experience.
Fix broken tests by using `nil` next tokens to avoid erroneous paging.
@jkanywhere

Copy link
Copy Markdown
Author

cc @trevorrowe , and thanks for your help.

And describe the `list_instances.rb`/`InstanceManager` example in the README.
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@jkanywhere@trevorrowe