<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Josh Pattison on Medium]]></title>
        <description><![CDATA[Stories by Josh Pattison on Medium]]></description>
        <link>https://medium.com/@joshpattison23?source=rss-3e9c3afe1aa3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ryedbO7lh6-AfhqnpuUUdg.jpeg</url>
            <title>Stories by Josh Pattison on Medium</title>
            <link>https://medium.com/@joshpattison23?source=rss-3e9c3afe1aa3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 14:31:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joshpattison23/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A better way of using AFT in a multi-region CT Landing Zone]]></title>
            <link>https://medium.com/@joshpattison23/a-better-way-of-using-aft-in-a-multi-region-ct-landing-zone-e8482ef72ea?source=rss-3e9c3afe1aa3------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8482ef72ea</guid>
            <category><![CDATA[platform-engineering]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[aft]]></category>
            <dc:creator><![CDATA[Josh Pattison]]></dc:creator>
            <pubDate>Mon, 24 Jul 2023 08:06:39 GMT</pubDate>
            <atom:updated>2023-07-25T11:28:49.723Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Hashicorp Terraform and AWS logos" src="https://cdn-images-1.medium.com/max/1024/1*Kmhh3ydIEjwad1cLPs84Ww.jpeg" /><figcaption>Photo from the <a href="https://www.hashicorp.com/blog"><strong>HashiCorp Blog</strong></a></figcaption></figure><p>Whilst working on my current project the team began to notice our multi-region AFT Landing Zone files were getting a little complex, unorganised and bloated. We had been following AWS’s own multi-region configuration example and felt we needed to make a change. Myself and a colleague took on the task of making some improvements to our current solution utilising the powers of Jinja2. We iterated to the point where we created a central configuration file which allowed us to reduce the amount of code required in each file by 75%, when compared to the AWS example.</p><p>In this blog entry I am going to detail the simple changes we made to make our code base much more efficient, so you can do the same.</p><p><em>More examples for this blog post can be found on my </em><a href="https://github.com/pattisoj/aft-multi-region-ct-landing-zone-examples"><em>GitHub</em></a><em>.</em></p><p>First, I think it is important to touch on how AFT takes template Jinja code and turns it into useable Terraform. In a quick and simple way AFT uses AWS Codebuild pipelines supplied with yaml files to create and deploy Terraform code from Jinja templates. The line of code that does the .jinja to .tf conversion is a Bash command that can be found <a href="https://github.com/aws-ia/terraform-aws-control_tower_account_factory/blob/05992d65433d3d2b49b011dd63942b36ce09c249/modules/aft-customizations/buildspecs/aft-global-customizations-terraform.yml#L94">here</a> and looks like this -</p><pre>for f in *.jinja; do jinja2 $f -D timestamp=&quot;$TIMESTAMP&quot; -D tf_distribution_type=$TF_DISTRIBUTION -D provider_region=$CT_MGMT_REGION -D region=$TF_BACKEND_REGION -D aft_admin_role_arn=$AFT_EXEC_ROLE_ARN -D target_admin_role_arn=$VENDED_EXEC_ROLE_ARN -D bucket=$TF_S3_BUCKET -D key=$TF_S3_KEY -D dynamodb_table=$TF_DDB_TABLE -D kms_key_id=$TF_KMS_KEY_ID &gt;&gt; ./$(basename $f .jinja).tf; done</pre><p>The variables (-D) in this line come from SSM Parameters when running in AWS. However if you want to test your Jinja templates locally you can convert this command and run it in a terminal. When I was testing my changes locally I would open a terminal and cd into my working directory, then I would use mkdir tmp to create a tmp directory and use the following altered command to test the Jinja to Terraform conversion-</p><pre>for f in *.jinja; do jinja2 $f -D timestamp=&quot;TIMESTAMP&quot; -D tf_distribution_type=&quot;TF_DISTRIBUTION&quot; -D provider_region=&quot;CT_MGMT_REGION&quot; -D region=&quot;TF_BACKEND_REGION&quot; -D aft_admin_role_arn=&quot;AFT_EXEC_ROLE_ARN&quot; -D target_admin_role_arn=&quot;VENDED_EXEC_ROLE_ARN&quot; -D bucket=&quot;TF_S3_BUCKET&quot; -D key=&quot;TF_S3_KEY&quot; -D dynamodb_table=&quot;TF_DDB_TABLE&quot; -D kms_key_id=&quot;TF_KMS_KEY_ID&quot; &gt;&gt; ./tmp/$(basename $f .jinja).tf; done</pre><p>The changes I made to the command include changing the variable names to strings (-D timestamp=$TIMESTAMP became -D timestamp=”TIMESTAMP”) . You can put whatever string you like, but I kept the original variable names so I could check they were being used in the correct places. The second change I made was toward the end of the command where I changed the output path to use the tmp directory I had created earlier ( ./$(basename $f.jinja).tf became ./tmp/$(basename $f.jinja).tf).</p><p>I then went into the newly created Terraform files and confirmed they were what I was expecting to see and would clean everything up with the rm -rf tmp command.</p><p>Now that you have at least a high level understanding of how AFT makes it’s Jinja to Terraform conversions and how you can test those conversions locally, let’s take a look at how AWS structure their multi-region code. This is also how the original repository I worked in was structured -</p><pre>.<br>├── README.md<br>├── api_helpers<br>└── terraform<br>    ├── aft-providers.jinja<br>    └── backend.jinja</pre><p>Now, lets take a look at how AWS suggests we should structure the files themselves -</p><pre>## Auto generated providers.tf ##<br>## Updated on: {{ timestamp }} ##<br><br>provider &quot;aws&quot; {<br>  region = &quot;{{ provider_region }}&quot;<br>  assume_role {<br>    role_arn    = &quot;{{ target_admin_role_arn }}&quot;<br>  }<br>  default_tags {<br>    tags = {<br>      managed_by = &quot;AFT&quot;<br>    }<br>  }<br>}<br><br># Custom provider for &#39;us-east-2&#39; region<br>provider &quot;aws&quot; {<br>  alias  = &quot;us_east_2&quot;<br>  region = &quot;us-east-2&quot;<br>  assume_role {<br>    role_arn    = &quot;{{ target_admin_role_arn }}&quot;<br>  }<br>  default_tags {<br>    tags = {<br>      managed_by = &quot;AFT&quot;<br>    }<br>  }<br>}<br><br># Custom provider for &#39;us-west-1&#39; region<br>provider &quot;aws&quot; {<br>  alias  = &quot;us_west_1&quot;<br>  region = &quot;us-west-1&quot;<br>  assume_role {<br>    role_arn    = &quot;{{ target_admin_role_arn }}&quot;<br>  }<br>  default_tags {<br>    tags = {<br>      managed_by = &quot;AFT&quot;<br>    }<br>  }<br>}</pre><blockquote>Note: I only showcased <em>aft_providers.jinja</em> here as it is the only file that is multi-region specific in AWS’s example.</blockquote><p>As you can see, even in this rather small example, we are already repeating almost all of the code. If you take this structure to scale and want to make it more complex it is easy to see how you end up with some bloat and messy code.</p><p>Imagine you have several files using multi-region configuration and for one reason or another you have to make a change. Maybe you’re no longer going to be using a region, or you’re looking to add another. You will have to go in and manually remove/add chunks of several files to ensure you remove/add all the region specific code. This is not only tedious but is primed for human error. <em>This is where our changes come in.</em></p><p>The first iteration for improvement didn’t come from myself. Another member of the team took on the challenging task of going into the code base at peak mess and bloat and did an excellent job making it much more readable and useable. Which in turn, made the later improvements much easier to foresee and implement.</p><p>The first step involved taking the mass of resources and converting them using a Jinja for loop. This meant we could reduce the number of resources in each file massively. If we mimicked this same change on the example aft_providers.jinja above it would have looked like this -</p><pre>## Auto generated providers.tf ##<br>## Updated on: {{ timestamp }} ##<br><br>provider &quot;aws&quot; {<br>  region = &quot;{{ provider_region }}&quot;<br>  assume_role {<br>    role_arn    = &quot;{{ target_admin_role_arn }}&quot;<br>  }<br>  default_tags {<br>    tags = local.tags<br>  }<br>}<br><br><strong>{% for region in [&#39;us-east-2&#39;, &#39;us-west-1&#39;] %}</strong><br>provider &quot;aws&quot; {<br>  region = &quot;{{ region }}&quot;<br>  alias  = &quot;{{ region }}&quot;<br><br>  assume_role {<br>    role_arn    = &quot;{{ target_admin_role_arn }}&quot;<br>  }<br>  default_tags {<br>    tags = {<br>      managed_by = &quot;AFT&quot;<br>    }<br>  }<br>}<br><strong>{% endfor %}</strong></pre><blockquote>Note: I go into more detail on all things Jinja in the later step-by-step example so don’t worry about fully understanding how this change works. Just note the reduced amount of code.</blockquote><p>Already the improvement is easy to spot. We can instantly reduce the amount of code required to do the same thing. This means our files aren’t massive and unmanageable, plus the code’s readability is greatly improved.</p><p>So why didn’t we stop here? Surely this is all that we needed to change? Well, it seemed that way to begin with until we came to make region changes…</p><p>We still had to go into every file and change every for loop ({% for region in [...] %}) to specify the regions we wanted and this led to the odd mistake here and there (we had some recourses absent from regions we wanted them in for example). We wanted to do more, so we began investigating.</p><p>The idea for the next iteration was to create a central configuration file that we could not only use within the current repository, but in other repositories for dev environments etc. After some wider reading we found something that we thought could work.</p><p>For this next example I am going to go into more detail about each step so you can easily follow along and understand how you can add additional configurations for you own AFT multi-region CT Landing Zone.</p><p>To start, we’re going to create an additional file jinja_config -</p><pre>.<br>├── README.md<br>├── api_helpers<br>└── terraform<br>    ├── <strong>jinja_config</strong><br>    ├── aft-providers.jinja<br>    └── backend.jinja</pre><p>Within that file we are going to configure any variables we would like. <em>In this specific example we are going to look at regions only.</em></p><pre><strong># ./terraform/jinja_config<br><br>{% set enabled_regions = [&#39;us-east-2&#39;, &#39;us-west-1&#39;] %}</strong></pre><p>Within the jinja_config file we create a new variable using the {% set var_name = value %} syntax and supply a list of regions we would like to enable.</p><blockquote>Note: This is the file where you can add additional configurations. To do so add another variable like so: {% set var_name = value %}</blockquote><p>Then within the aft_providers.jinja we make the following changes -</p><pre>## Auto generated providers.tf ##<br>## Updated on: {{ timestamp }} ##<br><br><strong>{% import &#39;jinja_config&#39; as settings %}</strong><br><br>provider &quot;aws&quot; {<br>  region = &quot;{{ provider_region }}&quot;<br>  assume_role {<br>    role_arn    = &quot;{{ target_admin_role_arn }}&quot;<br>  }<br>  default_tags {<br>    tags = local.tags<br>  }<br>}<br><br><strong>{% for region in settings.enabled_regions %}</strong><br>provider &quot;aws&quot; {<br>  region = &quot;{{ <strong>region </strong>}}&quot;<br>  alias  = &quot;{{ <strong>region </strong>}}&quot;<br><br>  assume_role {<br>    role_arn    = &quot;{{ target_admin_role_arn }}&quot;<br>  }<br>  default_tags {<br>    tags = {<br>      managed_by = &quot;AFT&quot;<br>    }<br>  }<br>}<br><strong>{% endfor %}</strong></pre><p>First, we import our config file using the statements structure ({% %}) and give it a referenceable name, in this case settings. Then we wrap the content we want to exist in multiple regions with a for loop. Jinja2 uses syntax very similar to Python so we can use {% for region in settings.enabled_regions %} where region is the chosen variable name we will reference within the for loop and settings.enabled_regions is the name we chose when we imported the our jinja_config file (settings) and the name of the variable we want to reference (enabled_regions) separated by a dot (.). Finally we have to close our for loop with {% endfor %}.</p><blockquote>Note: More information around Jinja2 templating can be found in the documentation <a href="https://jinja.palletsprojects.com/en/3.1.x/templates/">here</a>. This will be super helpful in your own configurations.</blockquote><p>If I now wanted to create new multi-region files I would only need to import the jinja_config file and wrap the content in a for loop like so -</p><pre># example_file.jinja<br><br>{% import &#39;jinja_config&#39; as settings %}<br><br>{% for region in settings.enabled_regions %}<br>  <br>  [Terraform Code]<br>  <br>{% endfor %}</pre><p>This can also be used for multi-region modules -</p><pre># example_module.jinja<br><br>{% import &#39;jinja_config&#39; as settings %}<br><br>{% for region in settings.enabled_regions %}<br>module &quot;example_module_{{ region }}&quot; {<br>  source = &quot;./modules/example&quot;<br>  providers = {<br>    aws = aws.{{ region }}<br>  }<br>}<br>{% endfor %}</pre><blockquote>Note: I found it quite beneficial to decouple the resources and the providers by using separate variables for each as this allows for easier region removal. A specific example for this is on my <a href="https://github.com/pattisoj/aft-multi-region-ct-landing-zone-examples/tree/main/examples/decoupling_example">GitHub</a>.</blockquote><p>These changes still allow the AFT CodeBuild pipelines to create multiple blocks of Terraform code (<em>use the test command I mentioned earlier to see this in action</em>), but what we see and write is much cleaner. It also means that future changes are much easier to make. If I wanted to deploy my CT Landing zone in three more regions the only thing that I would need to change is my variable in the jinja_config file and all of my configured files and modules would be deployed in the new regions. It would be as simple as this -</p><pre># ./terraform/jinja_config<br><br>{% set enabled_regions = [&#39;us-east-2&#39;, &#39;us-west-1&#39;, <strong>&#39;us-west-2&#39;, &#39;eu-west-1&#39;, &#39;ap-south-1&#39;</strong>] %}</pre><p>Whereas, with the AWS example the undertaking in just the aft-providers.jinja would add 39 more lines of code, a lot of repetition, complexity, bloat and chance for error and I would have to manually do this within <em>every </em>file.</p><p>This is just a very basic example of what we can do with a central config file and the first steps we took toward improving our production code. This can be taken so much further to allow for a more re-usable, efficient, manageable and configurable AFT multi-region CT Landing Zone.</p><p><strong>Thank you for reading!</strong></p><p><em>More examples for this blog post can be found on my </em><a href="https://github.com/pattisoj/aft-multi-region-ct-landing-zone-examples"><em>GitHub</em></a><em>.</em></p><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><p><em>I would like to thank </em><strong><em>Mike Peachy</em></strong><em> and </em><strong><em>Steven Trotter</em></strong><em> for their contributions to the original coding work that led to this article and also </em><strong><em>Andy Scott </em></strong><em>for his feedback in this post’s creation.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8482ef72ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>