Question:
I am trying to figure out what is the best way to get a list of ec2 instances with a certain tag for example “testing” using the ruby aws sdk.
1 2 3 |
ec2 = AWS::EC2.new(:access_key_id => "XXXXXXXXXXXXX", :secret_access_key => "YYYYYYYYY") ec2list = ec2.instances.filter("Name", "testing) |
This does not seem to work for some reason. It was thinking it will filter out the collection and just give me instances with tag testing. Is there a way to do this using the ruby sdk? thank you.
Answer:
If you want the tag “Name” with the value of “testing” use:
1 2 3 4 5 6 7 8 9 |
instances = resource.instances( filters: [ { name: 'tag:Name', values: ["testing"] } ] ) |
For all instances with a tag key of “testing” the following is used.
1 2 3 4 5 6 7 8 9 |
instances = resource.instances( filters: [ { name: 'tag:Key', values: ["testing"] } ] ) |
See the #instances docs for more filter options.