user1781472
user1781472

Reputation: 517

get ec2 instances with a certain tag using aws sdk

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.

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.

Upvotes: 6

Views: 6339

Answers (5)

lutix
lutix

Reputation: 158

None of the above worked, but this one worked for me:

ec2.instances.with_tag("Environment","Integration")

Upvotes: 1

Raghvendra Singh
Raghvendra Singh

Reputation: 1140

I am used to doing it this way

require 'aws-sdk'

ec2 = AWS::EC2.new(
        :region => cluster_region,
        :access_key_id => key_id,
        :secret_access_key => access_key)

instances = ec2.instances
instances = instances.filter("filter-key", "filter-value")

One can find the available filters at http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeInstances.html

Upvotes: 0

Christopher Bradford
Christopher Bradford

Reputation: 2260

If you want the tag "Name" with the value of "testing" use:

instances = resource.instances(
  filters: [
    { 
      name: 'tag:Name',
      values: ["testing"] 
    }
  ]
)

For all instances with a tag key of "testing" the following is used.

instances = resource.instances(
  filters: [
    { 
      name: 'tag:Key',
      values: ["testing"] 
    }
  ]
)

See the #instances docs for more filter options.

Upvotes: 7

cal
cal

Reputation: 870

This worked for me:

ec2.instances.tagged("testing")

Upvotes: 1

LukeS
LukeS

Reputation: 481

Hi I think you could get what you want using filter on the tags of the instances:

ec2 = AWS::EC2.new(:access_key_id => "XXXXXXXXXXXXX", :secret_access_key => "YYYYYYYYY")
ec2list = ec2.instances.tags.filter("Name", "testing)

CF: http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/AutoScaling/TagCollection.html#filter-instance_method http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2.html#tags-instance_method

Upvotes: 0

Related Questions