top of page
Writer's pictureChristoffer Windahl Madsen

Terraform weekly tips (1)

If you and your team is just starting on using Terraform, start simple. By that I mean:


- A root level folder structur with no subfolders. E.g. main.tf, variables.tf and outputs.tf


- Define the first environment as your learning Terraform by directly calling resource providers in the root main.tf file.


- Do not use any local variables or loops in main.tf, e.g. if you want to create 2 Azure Virtual Machines, define a resource definition for each.


- Do not define every single value in main.tf as a variable reference from variables.tf. Due to starting simple, it is easier to not obfuscate your Terraform code too much, instead only reference the most reused variables. E.g. like an Azure location instead of a VM's specific name.


Lets get started with some code to illustrate some concepts. To follow along with execution the following prerequisites must be met:

  1. Terraform to be installed, for help with installing visit this blog post -> <Insert link>

  2. Azure cli to be installed, for help with installing visit -> How to install the Azure CLI | Microsoft Learn

  3. A folder created to put the terraform main.tf file in

  4. Have access to a subscription in Azure with credits so we can create resources. Get a free 1000 dollar credit free of charge here -> Create Your Azure Free Account Today | Microsoft Azure ( You will need to provide a credit card but they will NOT charge anything)

I personally work on Windows and use VScode as my IDE. Furthermore, I have simply installed the Powershell7 extension to get access to a PS session directly in VScode.


Before copying in the code below:

  1. Open your favorite terminal

  2. Navigate to the created folder

  3. Create a file called main.tf and open it in your favorite IDE

Example 1 (terraform script): Our first ever resource creation in Azure, can be copied directly:

//Defining the provider 'azurerm' that we are going to use
//By not defining the version attribute terraform will always pull down the latest
terraform {
  required_providers {
    azurerm = {
        source = "hashicorp/azurerm"
    }
  }
}

//Because we are using the most simpel form of authentication, only the features attribute (required) is added
provider "azurerm" {
  features {
  }
}

//The most simple resource to create is a resource group. We need to specify name and location as minimum
//Because we are just getting started no loops or variables are used for simplicity's sake
resource "azurerm_resource_group" "my_first_rg" {
  name = "my-first-rg"
  location = "West Europe"
}

//If we dont provide an output definition terraform will not return any output after the plan has been provided other than a status of the deployment
output "rg" {
  value = azurerm_resource_group.my_first_rg
}

Using azure cli to authenticate to Azure

In the terminal run:

az login --tenant "<id of your tenant>" //will prompt to login in the browser

Result in terminal:


Terraform init in terminal and results:

In the terminal run:

terraform init

Notice the installed version. At the time of writing I am running provider version '3.57.0' (latest) of 'azurerm'


Terraform plan in terminal and results:

In the terminal run:

terraform plan

Terraform apply in terminal and results

In the terminal run:

terraform apply --auto-approve=true

Because we have defined a direct output we will be printed the return object that represents the just created Azure resource group 'my-first-rg'


Congratulations! We have just build our first Azure resource using Terraform, how exicting :)


As a final note on this post - As we can see in the output of the resource group, an object with attributes are returned. This actually means that the resource defintion of 'azurerm_resource_group.my_first_rg' Will contain information we can reuse.


To prove this, under the "Output" Dot out directly to the exact attribute 'name' Like so:

output "rg" {
  value = azurerm_resource_group.my_first_rg.name
}

Run terraform apply --auto-approve=true and notice how the output changes:

Notice how Terraform lets us know that it will be removing the output we saw before. Furthermore, the output has changed to rg = "my-first-resource-group" Whats important to note about that is, the output "wraps" The string of "my-first-rg" Which is the string name into an attribute called "rg" This is simply due to the fact that we called the output section "rg" And does indicate that the output is a variable, rather its a litteral string.


As we go more in dept with Terraform behaviour, we will begin to use the power of return values to our advantage, but for now this is all for today.


Cheers!


PS. Want to learn more about Terraform? Click here -> terraform (codeterraform.com)

Want to learn more about other cool stuff like Automation or Powershell -> powershell (codeterraform.com) / automation (codeterraform.com)

42 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page