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

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

<p>So mostly, what you have is the same as:</p>

<pre><code>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 }
</code></pre>

<p>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)</p>

<p>More details in <a href="https://stackoverflow.com/questions/5587264/do-end-vs-curly-braces-for-blocks-in-ruby">this SO quastion</a> about ruby syntax.</p>

<p>Concerning Chef itself I highly suggest you follow the tutorials at <a href="https://learn.chef.io" rel="nofollow noreferrer">https://learn.chef.io</a> for a better overview of what is what in cookbooks.</p>

CC BY-SA 4.0
2018-09-17T08:25:03.607Z
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
CC BY-SA 4.0
2018-09-17T08:34:49.523Z
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
CC BY-SA 4.0
2018-09-17T07:39:56.337Z
2018-09-17T07:39:56.337Z
Tensibai
<p>Following Tensiblai's guidance, I found this documentation explaining the parts comprising a resource:</p>

<p>see: <a href="https://docs.chef.io/resource.html" rel="nofollow noreferrer">https://docs.chef.io/resource.html</a></p>

<blockquote>
  <p>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:</p>
</blockquote>

<pre><code>type 'name' do
   attribute 'value'
   action :type_of_action
end
</code></pre>

CC BY-SA 4.0
2018-09-17T08:52:36.597Z
2018-09-17T08:52:36.597Z
orberkov
<p>While reviewing a chef.io <a href="https://pages.chef.io/rs/255-VFB-268/images/DevOps_AWS_Chef_Whitepaper_2017.pdf" rel="nofollow noreferrer">whitepaper</a>, I encountered the following syntax: </p>

<pre><code># 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
end
</code></pre>

<p>Can you explain or refer to documentation about the "do" statement? </p>

<p>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.. ?</p>

CC BY-SA 4.0
2018-09-17T19:56:05.693Z
This is a pure Ruby question, not really related to Chef.
user229044
CC BY-SA 4.0
2018-09-18T04:27:24.640Z
The answer to the question is VITAL to understanding chef cookbooks. The subject is thoroughly covered in Chef's own documentation here: https://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
CC BY-SA 4.0
2018-09-18T12:39:05.100Z
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
<chef>
Chef syntax with "do" statement
orberkov
# Chef syntax with "do" statement

(Asked by: username_0 on 2018-09-17)

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
end

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.. ?

**Tags:** chef

> **Comments:**
> **username_1**: This is a pure Ruby question, not really related to Chef.  
> **username_0**: The answer to the question is VITAL to understanding chef cookbooks. The subject is thoroughly covered in Chef's own documentation here: https://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.  
> **username_1**: 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.  

---

## 2 answers

### Answer by username_2 on 2018-09-17. Score: 0 | Accepted answer

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.

> **Comments:**
> **username_0**: 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?  
> **username_2**: 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.  

---

### Answer by username_0 on 2018-09-17. Score: 0

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
end


---

