You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Module: AWS::EC2::TaggedItem

Instance Method Summary collapse

Instance Method Details

#add_tag(key, options = {}) ⇒ Tag Also known as: tag

Adds a single tag with an optional tag value.

# adds a tag with the key production
resource.tag('production')

# adds a tag with the optional value set to production
resource.tag('role', :value => 'webserver')

Parameters:

  • key (String)

    The name of the tag to add.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :value (String)

    An optional tag value.

Returns:

  • (Tag)

    The tag that was created.



30
31
32
33
34
35
36
# File 'lib/aws/ec2/tagged_item.rb', line 30

def add_tag key, options = {}
  client.create_tags({
    :resources => [id],
    :tags => [{ :key => key, :value => options[:value].to_s }],
  })
  Tag.new(self, key, options.merge(:config => config))
end

#clear_tagsnil

Deletes all tags associated with this EC2 resource.

Returns:

  • (nil)


41
42
43
44
# File 'lib/aws/ec2/tagged_item.rb', line 41

def clear_tags
  client.delete_tags(:resources => [self.id])
  nil
end

#tagsResourceTagCollection

Returns a collection that represents only tags belonging to this resource.

Examples:

Manipulating the tags of an EC2 instance

i = ec2.instances["i-123"]
i.tags.to_h                  # => { "foo" => "bar", ... }
i.tags.clear
i.tags.stage = "production"
i.tags.stage                 # => "production"

Returns:



59
60
61
# File 'lib/aws/ec2/tagged_item.rb', line 59

def tags
  ResourceTagCollection.new(self, :config => config)
end