<?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: Morten Christensen</title>
    <description>The latest articles on Forem by Morten Christensen (@sitereactor).</description>
    <link>https://forem.com/sitereactor</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%2F1041705%2F0aff46f9-4c41-43e5-b0bc-c15ffe07ddfe.jpeg</url>
      <title>Forem: Morten Christensen</title>
      <link>https://forem.com/sitereactor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sitereactor"/>
    <language>en</language>
    <item>
      <title>One approach to structuring terraform</title>
      <dc:creator>Morten Christensen</dc:creator>
      <pubDate>Sun, 19 Mar 2023 14:59:42 +0000</pubDate>
      <link>https://forem.com/sitereactor/one-approach-to-structuring-terraform-57fh</link>
      <guid>https://forem.com/sitereactor/one-approach-to-structuring-terraform-57fh</guid>
      <description>&lt;p&gt;For this post I wanted to share how I prefer to structure my terraform code for use with multiple environments, modules and how I can work with it locally. If you have a different approach then please feel free to leave a comment - I'm always happy to learn and improve.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First off, the folder structure&lt;/strong&gt;. The infrastructure folder sits in the root along side src, test, build and whatever else goes in the git repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── infrastructure
│   │── environments
│   │   ├── dev
│   │   ├── live
│   │   ├── main
│   └── modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within an environment folder I would put &lt;code&gt;local.backend.tfvars&lt;/code&gt; and &lt;code&gt;local.secrets.tfvars&lt;/code&gt; files for local development purposes. These files should NOT be committed to git, so I always make sure to include them in the .gitignore file of the repository.&lt;br&gt;
Then there is the &lt;code&gt;backend.tfvars&lt;/code&gt; file, which is used to configure the backend used for the terraform state. Finally the (maybe?) most important file for the environment &lt;code&gt;variables.tfvars&lt;/code&gt;, which contains all the environment specific variables. A good example of this is using different SKUs of Azure App Service in Development versus Production. It could also be the subscription used for the environment - maybe you want to use an Azure Dev/Test Subscription for Development and a CSP/PAYG subscription for Production.&lt;/p&gt;

&lt;p&gt;The main folder contains the &lt;code&gt;main.tf&lt;/code&gt;, &lt;code&gt;secrets.tf&lt;/code&gt;, &lt;code&gt;outputs.tf&lt;/code&gt; and &lt;code&gt;variables.tf&lt;/code&gt; files. Depending on the size and content of the &lt;code&gt;main.tf&lt;/code&gt; file I tend to split it up into multiple files, so I don't end up having to scroll thousands of lines of code. The structure and naming of files can vary depending on what makes sense, but one simple approach is to divide the different types of resources into different files. There will likely be things like KeyVault or Networking, which ties into other resources, so it really depends.&lt;/p&gt;

&lt;p&gt;The modules folder is used for reusable modules. It's not always relevant, but I tend to keep the modules outside of the environments. The structure highlights that its reusable across environments.&lt;/p&gt;
&lt;h2&gt;
  
  
  Working locally
&lt;/h2&gt;

&lt;p&gt;When you need to run Terraform locally against the Development environment, use the steps listed below.&lt;/p&gt;

&lt;p&gt;Ensure that you have both &lt;code&gt;local.backend.tfvars&lt;/code&gt; and &lt;code&gt;local.secrets.tfvars&lt;/code&gt; in your environment folder with the actual keys in place.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;cd infrastructure/environments/main&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;terraform init -backend-config="../dev/local.backend.tfvars"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;terraform validate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;terraform plan -var-file="../dev/local.secrets.tfvars" -detailed-exitcode -out=tfplan&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's have a look at the content of the &lt;code&gt;local.backend.tfvars&lt;/code&gt; when using Azure Blob Storage as the backend for the terraform state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource_group_name     = "rg-terraform-state-we-dev"
storage_account_name    = "terraformwestatedev"
container_name          = "terraform-state-dev"
key                     = "azure/terraform.tfstate"
access_key              = "&amp;lt;actual access key goes here&amp;gt;"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The content of the &lt;code&gt;local.secrets.tfvars&lt;/code&gt; should correspond to what you put in the &lt;code&gt;secrets.tf&lt;/code&gt; from within the main folder, and what is needed to create resources within Azure/AWS/GCP. For Azure it would be Service Principal details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tenant_id       = "&amp;lt;actual tenant id goes here&amp;gt;"
subscription_id = "&amp;lt;actual subscription id goes here&amp;gt;"
client_id       = "&amp;lt;actual client id goes here&amp;gt;"
client_secret   = "&amp;lt;actual client secret goes here&amp;gt;"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When working locally you could connect to the development environment or use a different subscription / account setup. If you use the same subscription and terraform state locally as when deploying to development, you should be aware of the changes between working locally versus deploying via CI/CD. The deployed changes might revert what you are testing locally, and other deployments might fail because the state is expecting resources, which are not part of what is being deployed.&lt;/p&gt;

&lt;h2&gt;
  
  
  main.tf
&lt;/h2&gt;

&lt;p&gt;With the backend and secrets setup as mentioned above there is one important thing to include in the &lt;code&gt;main.tf&lt;/code&gt; file - the required providers and something like &lt;code&gt;azurerm&lt;/code&gt; as the backend like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform {
  backend "azurerm" {}
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "= 2.29.0"
    }
    cloudflare = {
      source  = "terraform-providers/cloudflare"
      version = "~&amp;gt; 2.9.0"
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The backend part is what will be "filled" with the &lt;code&gt;*backend.tfvars&lt;/code&gt; files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Azure DevOps, Terraform and Secrets
&lt;/h2&gt;

&lt;p&gt;When running infrastructure as code (IaC) as part of a pipeline in something like Azure DevOps (same applies to Github Actions and other CI/CD setups), you want to keep your secrets safe and inject them when needed as part of the pipeline.&lt;br&gt;
In order to run the terraform init, plan and apply commands there needs to be a backend for storing state and there needs to be secrets (like a Service Principal) for creating the actual resources in Azure. We also need to know, which environment we are deploying to, so we can select the right one (using the &lt;code&gt;variables.tfvars&lt;/code&gt; file that corresponds to the environment).&lt;/p&gt;

&lt;p&gt;The secrets can be added as a variable group for the environment under Pipelines/Library in Azure DevOps. You could also store the secrets in KeyVault and connect KeyVault to Azure DevOps, so they are not visible by anyone.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;backend.tfvars&lt;/code&gt; for the development environment would typically look 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;resource_group_name     = "rg-terraform-state-we-dev"
storage_account_name    = "terraformwestatedev"
container_name          = "terraform-state-dev"
key                     = "azure/terraform.tfstate"
access_key              = "#{terraform_access_key}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see its pretty much the same as the &lt;code&gt;local.backend.tfvars&lt;/code&gt; where the only difference is the &lt;code&gt;access_key&lt;/code&gt; property, which will be replaced within the pipeline.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;secrets.tf&lt;/code&gt; file within the main folder will be variable definitions for the secrets needed to run the terraform. These variables will need to be set in the pipeline, so the IaC can be initialized.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;variable "tenant_id" {
  description = "Azure subscription tenant id."
}

variable "subscription_id" {
  description = "Azure subscription id."
}

variable "client_secret" {
  description = "Azure provider client secret"
}

variable "client_id" {
  description = "Azure provider Azure AD client id."
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the variables.tfvars file for an environment I keep the variable secrets as tokens, which can later be replaced:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tenant_id       = "#{tenant_id}"
subscription_id = "#{subscription_id}"
client_id       = "#{client_id}"
client_secret   = "#{client_secret}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When doing this in Azure DevOps with a YAML pipeline I use the following approach for replacing the tokens with secrets and generating - here its the validation stage of the pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;parameters:
  variable_group_name:
  environment_name:
  working_directory:
  source_branch:

jobs:
  - job: Validate
    displayName: Terraform Validate Plan
    continueOnError: false
    variables:
      - name: tf_work_dir
        value: "$(working_directory)/environments/main"
      - group: ${{ format('telemetry-{0}', parameters.environment_name) }}
      - ${{ if ne(parameters.variable_group_name, '') }}:
          - group: ${{ format('{0}-{1}', parameters.variable_group_name, parameters.environment_name) }}
    steps:
      - task: qetza.replacetokens.replacetokens-task.replacetokens@3
        displayName: "Replace tokens in *.tfvars with variables from the desired Variable Group"
        inputs:
          targetFiles: "$(working_directory)/environments/$(environment_name)/*.tfvars =&amp;gt; *.env.tfvars"
          encoding: "auto"
          writeBOM: true
          actionOnMissing: "warn"
          keepToken: false
          tokenPrefix: "#{"
          tokenSuffix: "}"
      - bash: |
          terraform init -backend-config="../$(environment_name)/backend.env.tfvars" -input=false
        displayName: Initialize configuration
        workingDirectory: $(tf_work_dir)
      - bash: terraform validate
        displayName: Validate configuration
        workingDirectory: $(tf_work_dir)

      - bash: |
          terraform plan -var-file="../$(environment_name)/variables.env.tfvars" -input=false
        displayName: Create execution plan
        workingDirectory: $(tf_work_dir)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup I can work with terraform locally, and I can choose to create a throw-away environment for trying out different things locally without interfering with Development. Alternatively, I can run it against the Development environment, which can be handy if resources needs to be imported or deleted without having to involve a pipeline.&lt;br&gt;
I can create any number of environments, which can have each their own subscription and resource configuration.&lt;/p&gt;

&lt;p&gt;Here is the file structure with all of the mentioned files&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsygn2wg3rk546z34etr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsygn2wg3rk546z34etr0.png" alt="Image description" width="343" height="679"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The screenshot is from a real world telemetry service, which is composed of a Cloudflare Worker (point of entry / reverse proxy) and an Azure backend (API and Storage).&lt;/p&gt;

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