jets icon indicating copy to clipboard operation
jets copied to clipboard

Adding common tags to all Jets resources

Open prashcr opened this issue 5 years ago • 2 comments

Summary

Provide an easy way to add AWS tags (or other common properties) to every resource created by Jets.

Motivation

Some users care about keeping all their AWS resources tagged accurately and consistently

  • Makes searching for AWS resources in the console easier
  • Break down spending by tag in AWS cost reports

Reference-level explanation

I came up with this monkeypatch solution for my own project. Would appreciate ideas on how this could be moved into Jets and exposed to Jets projects.

# config/initializers/resource_tagger.rb
module ResourceTagger
  TAGS = [
    { Key: 'Project', Value: 'Demo' },
    { Key: 'Env', Value: ENV['JETS_ENV'] },
  ]
  UNTAGGABLE = /Permission|GemLayer|EventsRule/

  def template
    hash = super
    hash.each do |id, resource|
      next if UNTAGGABLE.match?(id)
      resource['Properties']['Tags'] ||= []
      resource['Properties']['Tags'].concat(TAGS)
    end
    hash
  end
end
Jets::Resource.prepend(ResourceTagger)

Perhaps this could be exposed to Jets project as a config option?

Jets.application.configure do
  config.resource_tags = [{Key: 'Foo', Value: 'Bar'}]
  ...
end

prashcr avatar Feb 05 '20 02:02 prashcr

This is pretty neat. Also wondering if tagging the CloudFormation stacks themselves is another approach. Believe that stack tags result in tags propagating to all the resources in the stack. Have to dig into it.

tongueroo avatar Mar 13 '20 17:03 tongueroo

@tongueroo that is correct. CloudFormation stacks propagate tags to the items they create.

timlawrenz avatar Dec 16 '20 00:12 timlawrenz

@tongueroo Im going to work on this

jeremiahlukus avatar Nov 02 '23 14:11 jeremiahlukus

Fixed in #675

tongueroo avatar Dec 05 '23 21:12 tongueroo

Hello, I am having the same issue with the tags,

Tried with https://github.com/rubyonjets/jets/pull/675 PR, I updated my ruby on jets to last version but adding the tags on application.rb or even be environment in production.rb, the resources are still created without tags. is there something else that is needed?

module MyApp
  class Application < Jets::Application
    ...
    config.resource_tags = {
      'Product' => 'xcq',
      'Name' => "xyz-#{ENV['JETS_ENV']}",
      'Project' => 'Application',
      'Comment' => 'qwerty',
      'test' => 'test'
    }
    ....
  end
end
Jets.application.configure do
  config.cache_classes = true
  config.eager_load = true
  config.log_level = :info
  config.logging.event = false # can be useful for CloudWatch
  config.resource_tags = {
      'Product' => 'xcq',
      'Name' => "xyz-#{ENV['JETS_ENV']}",
      'Project' => 'Application',
      'Comment' => 'qwerty',
      'test' => 'test'
    }
end 

None of those worked in my case

xploshioOn avatar Feb 26 '24 14:02 xploshioOn