While reviewing a chef.io whitepaper, I encountered the following syntax:

# Load your AWS credentials databag
include_recipe ‘aws’
aws = data_bag_item(‘aws’, ‘main’)
aws_elastic_lb ‘elb_qa’ do
  aws_access_key aws[‘aws_access_key_id’]
  aws_secret_access_key aws[‘aws_secret_access_key’]
  name ‘elb_qa’
  action :register

Can you explain or refer to documentation about the "do" statement?

The common example for the Ruby "do" statement is using "each", which cannot be found in the example above. What does the syntax mean? what here is a parameter, variable, assignment, function, etc.. ?

  • This is a pure Ruby question, not really related to Chef.
    – user229044
    Commented Sep 17, 2018 at 19:56
  • The answer to the question is VITAL to understanding chef cookbooks. The subject is thoroughly covered in Chef's own documentation here: docs.chef.io/resource.html ; This contributes greatly to the complete understanding of the required configuration of Chef. Otherwise, the subject wouldn't have been covered by Chef's OWN documentation, as the link herein shows.
    – orberkov
    Commented Sep 18, 2018 at 4:27
  • Obviously understanding Ruby is crucial to understanding Ruby, which is what Chef cookbooks are written in. Notice that the link you shared is not a Ruby tutorial, and it makes no attempt to explain the syntax, simply referring to it as a "Ruby block", which is all it is. They leave it up to you to go learn Ruby if you want to understand what is actually happening here.
    – user229044
    Commented Sep 18, 2018 at 12:39

2 Answers 2


what you're looking at is a resource declaration, mostly a class taking a block as parameter.

In ruby the do code end syntax is the prefered way over the curly braces syntax { code } which is prefered for inline code.

So mostly, what you have is the same as:

aws_elastic_lb('elb_qa'){ aws_access_key aws[‘aws_access_key_id’]; aws_secret_access_key aws[‘aws_secret_access_key’]; name ‘elb_qa’; action :register }

In recent version of chef (14.X) the name attribute should be the name of the block (what is just after the resource type (aws_elastic_lb)

More details in this SO quastion about ruby syntax.

Concerning Chef itself I highly suggest you follow the tutorials at https://learn.chef.io for a better overview of what is what in cookbooks.

  • can you please clarify: how is the code inside the "do" (or the curly brackets) related to the aws_elastic_lb('elb_qa') call? are they like variables passed to the aws_elastic_lb function? or are they lines that are executred in some context?
    – orberkov
    Commented Sep 17, 2018 at 8:25
  • 1
    They are a block of code, so this code is run in the context of the resource, what it does is quite chef specific here, in plain rubby it's dependent on the method itself.
    – Tensibai
    Commented Sep 17, 2018 at 8:34

Following Tensiblai's guidance, I found this documentation explaining the parts comprising a resource:

see: https://docs.chef.io/resource.html

A resource is a Ruby block with four components: a type, a name, one (or more) properties (with values), and one (or more) actions. The syntax for a resource is like this:

type 'name' do
   attribute 'value'
   action :type_of_action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.