Using include_recipe in Chef LWRP

A big part of Chef’s resources internal nuts and bolts has been change over the last year. One of the most welcome changes has been recipe_eval - a mini Chef run that can be used inside Chef LWRP. This mechanism has been put to good use in use_inline_resources dsl directive in LWRP, in chef-stage and in inline_recipe.

Now that LWRPs have become so powerful, there aren’t many reasons to use definitions. The most common reason people use definitions and not LWRP is the absence of include_recipe - which now is available in LRWPs. Lets explore this:

action :send do
    require 'aws'
  rescue LoadError
    recipe_eval do
      run_context.include_recipe "xml::ruby"

      chef_gem "aws-sdk"
    require 'aws'

  converge_by(block_name) do
# Actually sending by SNS now that we have the aws-sdk gem

As you can see, the trick is to use run_context.include_recipe instead of just include_recipe since it isn’t directly available in recipe_eval (alternatively one could include Chef::DSL::InclueRecipe mixin).