Chef Custom Resources - Library vs Resource

I have beet trying to brush up on some more advanced Chef cookbook techniques and have come across a paradigm in the Chef maintained community cookbooks that I would like to get some clarification on.

I was looking through the etcd cookbook and noticed that custom resources were being written the libraries directory with the “new” format rather than being written in the resources directory.

Does anyone know if there is any benefit in writing custom resources within library files rather than resource directory files? I can not seem to see why new style custom resources would be defined in the libraries directory.

Here is one example of what I am referring to https://github.com/chef-cookbooks/etcd/blob/master/libraries/etcd_service.rb

Thanks!

Unless you have a good reason, use Custom Resources as they are described in the documentation. The fact of the matter is that as Chef is still Ruby under the hood there are often various ways to achieve the same outcome. Prior to the current iteration of what we call “Custom Resources” it was popular in some circles to write the Resources are pure library code - this worked and still does but requires a significantly more contextual knowledge of Chef on the average.

Writing resources in a library is anything but new but also we don’t really recommend anyone start there or use that as guidance. It’s not really better or advanced so much as it might allow you to do more flexibly Ruby things and usually the people you will see doing that have 6+ years of Chef under their belt - I can count most of them with the fingers of one hand.

In short: nothing inherently better about it and not something we teach, just a knowledgeable bending of long existing paradigms.

1 Like