ТЕМА: New Orleans : ops automation - Кабринский Эдуард

New Orleans : ops automation - Кабринский Эдуард 2 років 10 місяців тому #29994

Kabrinskiy Eduard - Pivotal devops - Kabrinskiy Eduard


<h1>Pivotal devops</h1>
<p>[youtube]</p>
Pivotal devops <a href="remmont.com">Current news stories</a> Pivotal devops
<h1>Category Archive DevOps</h1>

<h3>Terraform Modules and Code Structure</h3>
<h1>Applying Structure to your Resource Definitions</h1>
<p>In this article I’ll be going over how best to structure your Terraform resource code into modules. This draws on the practices outlined in the site www.terraform-best-practices.com and the accompanying GitHub at github.com/antonbabenko/terraform-best-practices. It is intended to act as a summary of that content together with some of my own observations and suggestions thrown in for good measure. Although not technically Azure related, it is a subject central to your best Infrastructure as Code endeavours with Terraform.</p>
<h2>Modules</h2>
<p>Structuring your resource code into modules makes them reusable and easily maintainable. I guess you could say it makes them, well, modular. You can find out all about modules from the Terraform docs at www.terraform.io/docs/modules/composition.html so I won’t go into them too much here.</p>
<h3>Registries</h3>
<p>Modules become particularly powerful when you start to publish them centrally. Terraform supports a number of repositories for these, such as file shares, GitHub, BitBucket and Terraform Registry. Users can then reference the repository modules for use within their own deployments.</p>
<h3>Module Definitions</h3>
<p>How you determine what constitutes a module is really down to you. It will depend on how your deployments are structured and how you reuse resource definitions. Terraform recommend dividing into natural groupings such as networking, databases, virtual machines, etc. However you decide to chunk up your infrastructure deployment definitions, there are some guidelines on what to include.</p>
<p>Each module is contained in its own folder and should contain a file for each of the following:</p>
<p><ul>
<li>The resource configuration definitions. This is typically named <strong>main.tf</strong></li>
<li>The outputs to be consumed outside of the module. Again, no surprises at being called <strong>outputs.tf</strong></li>
<li>The variables that are used within the module. Hardly surprisingly this is typically called <strong>variables.tf</strong></li>
</ul>
</p>
<p>Some teams go a little further and split up certain resource types within the module, such as security or network resources, into their own separate .tf files to be used as well as <strong>main.tf</strong>. This may make sense where the module contains a large number of resources, and managing them in a single main.tf file becomes unwieldy.</p>
<h4>Module ‘Contracts’</h4>
<p>In Object Oriented terms, you can loosely equate the variables to class method parameters required for the module. Similarly the Outputs are like the returns from methods and the main as the class itself. I’m sure there are plenty of purists that would point out floors in this comparison. However, conceptually it is good enough when thinking about how to encapsulate things (if you squint a bit). The variables and the outputs should form a sort of contract for use of the module. As such these definitions should try and remain relatively constant like the best library interfaces try to.</p>
<p>You can of course nest module folders within other module folders. However, generally speaking, it is not recommended to have very deep nested module hierarchies as this can make development difficult. Typically one level of modules, usually in a folder called ‘modules’ (again no prizes for originality here) is the accepted standard. You may of course opt for calling your folder ‘Bernard’, or ‘marzipan’ or whatever you like. Let’s face it though ‘modules’ is probably a lot more self-explanatory.</p>
<p>A basic module might look like the following:</p>
<p style="clear: both"><img src="i2.wp.com/pivotalbi.com/wp-content/uploa...0/11/ModuleFiles.png" /></p>
<h3>Referencing Modules</h3>
<p>With your modules nicely encapsulated for potential reuse and standards and all that loveliness, you need to make use of them. In your root module, being the top level entry point of your Terraform configuration code, you add references such as shown below:</p>
<p>This then defines a resource using the module. Simply add your variable assignments that will be used within the module as required and you’re good to go.</p>
<p>Some teams like prefixes (mod-, m- etc.) on these files in order to distinguish them from resources that are standalone, single-file definitions (in turn perhaps prefixed res-, r-). I’m not a big fan of prefixing by subtypes (remember Hungarian Notation. ) as this tends to get in the way of writing code. For me, simple naming that aligns with other resource file naming makes more sense.</p>
<h3>Variables Overload?</h3>
<p>One area to be mindful of is to not introduce variables for every attribute of your module’s resources. If an attribute is not going to be subject to change then it won’t need a variable. Remembering the maintenance of your code is a key consideration of any good ‘Coding Citizen’. Too many variables will quickly overwhelm those not familiar.</p>
<p>There is of course a balance to be struck here. Too few variables and you can’t really reuse your module as it is too specific for others’ needs. It may make sense to have variations on modules that have various attributes preset for a specific workload. For example a certain Virtual Machine role type will ordinarily have a bunch of attributes that don’t differ. The standard advice of using your best judgement and a little forethought applies as with most things. Personally I’d rather work with two modules that are specific than one that is vague and requires supplying many more variables.</p>
<h2>Summing Up</h2>
<p>So that just about covers the main points I have to share on Terraform modules and resource code structure. I hope this has provided some insight and guidance of value based on my adventures in Terraform module land. They’re definitely worth getting familiar with early on to simplify and structure your efforts. As your organisation’s deployments grow, maturity in this area will soon pay off for all involved.</p>
<p>The last post in this series (I know, gutting right?) coming up soon will cover Tips and Tricks with Terraform and Azure DevOps that I’ve picked up on my travels. Thanks for reading and stay safe.</p>
<h2>Pivotal devops</h2>

<h3>Pivotal devops</h3>
<p>[youtube]</p>
Pivotal devops <a href="remmont.com">Latest hot news</a> Pivotal devops
<h4>Pivotal devops</h4>
Category Archive DevOps Terraform Modules and Code Structure Applying Structure to your Resource Definitions In this article I’ll be going over how best to structure your Terraform
<h5>Pivotal devops</h5>
Pivotal devops <a href="remmont.com">Pivotal devops</a> Pivotal devops
SOURCE: <h6>Pivotal devops</h6> <a href="dev-ops.engineer/">Pivotal devops</a> Pivotal devops
#tags#[replace: -,-Pivotal devops] Pivotal devops#tags#

Эдуард Кабринский
world news
  • EUROBoolo
  • EUROBoolo аватар
  • Немає на сайті
  • Платиновий учасник
  • Дописи: 2122
  • Репутація: 0
Адміністратор заборонив доступ на запис.
Час відкриття сторінки: 0.048 секунд