<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: nrxr</title>
    <description>The latest articles on Forem by nrxr (@nrxr).</description>
    <link>https://forem.com/nrxr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F138549%2F8510f0fc-bc19-4621-b02b-93e8da756c3c.png</url>
      <title>Forem: nrxr</title>
      <link>https://forem.com/nrxr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nrxr"/>
    <language>en</language>
    <item>
      <title>Versioning Terraform modules without a registry, but Git</title>
      <dc:creator>nrxr</dc:creator>
      <pubDate>Sat, 17 Feb 2024 13:56:38 +0000</pubDate>
      <link>https://forem.com/nrxr/versioning-terraform-modules-without-a-registry-but-git-18o</link>
      <guid>https://forem.com/nrxr/versioning-terraform-modules-without-a-registry-but-git-18o</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Original post from &lt;a href="https://nrxr.org/post/versioning-terraform-modules-without-registry/"&gt;my blog at nrxr.org&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's say your organization made the sane decision to keep re-usable Terraform modules in the same Git repository and now developers are expected to reference them. Yet, your organization is not expecting to use a Terraform registry, but instead, the expectation is to use the Git repository directly as the source, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"git@github.com/org/mods.git//mod-a"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a repository structure like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;tree -L 1
&lt;/span&gt;&lt;span class="c"&gt;.
&lt;/span&gt;&lt;span class="go"&gt;├── mod-a
├── mod-b
└── mod-c
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shipping re-usable things in organizations can be troublesome. Different teams re-use the resource and then you are left with pieces of a platform using different versions of the same resource. Without proper versioning, it's a train-crash awaiting to happen. And native Terraform modules versioning is only available when the source is a registry.&lt;/p&gt;

&lt;p&gt;Git &lt;code&gt;ref&lt;/code&gt;s to the rescue.&lt;/p&gt;

&lt;p&gt;Terraform is implicitly including the &lt;code&gt;ref&lt;/code&gt; in your source address when using Git, with the name of your default branch. This value can be found by doing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;git remote show origin | grep 'HEAD branch'
  HEAD branch: main
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your results may vary, depending on what's the name of your default branch.&lt;/p&gt;

&lt;p&gt;Now, this means we can use tags for our versioning, which may come useful since now sources could be very specific, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"git@github.com/org/mods.git//mod-a?ref=v1.0.0"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But then you'll be missing out on future updates and fixes.&lt;/p&gt;

&lt;p&gt;The workaround is to combine tags and branches, since branches are references just like tags are.&lt;/p&gt;

&lt;p&gt;The main requirement is to enforce a strict semantic versioning policy in your repository and making base branches where tags are merged to.&lt;/p&gt;

&lt;p&gt;In order to reduce manual-labor, I would suggest to only keep branches for the major version, it would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
  tags           v1.0.0/v1.0.1      v1.1.0       v2.0.0       v2.1.0
┌──────┐
│ main ├───▪────▪───▫──▪───▫────▪───▪──▫──▪──▪──▪───▫──▪────▪────▫─────▶
└──────┘            │      │           │            │            │
                    │      │           │            │            │
┌──────┐            ▼      ▼           ▼            │            │
│  v1  ├────────────▪──────▪───────────▪────────────┼────────────┼─────▶
└──────┘                                            │            │
                                                    │            │
┌──────┐                                            ▼            ▼
│  v2  ├────────────────────────────────────────────▪────────────▪─────▶
└──────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when defining the source of the module, looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"git@github.com/org/mods.git//mod-a?ref=v1"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, each time &lt;code&gt;mod-a&lt;/code&gt; is applied, it'll be having the latest version of the module release.&lt;/p&gt;

&lt;p&gt;Although there's a &lt;code&gt;v2&lt;/code&gt; branch in the diagram above, it's simpler to create major breaking versions as a new module, like &lt;code&gt;mod-a-v2&lt;/code&gt;. This reduces friction during development.&lt;/p&gt;

&lt;p&gt;This approach also helps to ease development, by referencing branches and using it for experimental features. Be warned though, the &lt;code&gt;ref&lt;/code&gt; argument gets lost when the value has &lt;code&gt;/&lt;/code&gt; in it. Using names of branches like &lt;code&gt;feat/name&lt;/code&gt; is not possible, it'll try &lt;code&gt;feat&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>git</category>
    </item>
  </channel>
</rss>
