Terraform in Azure

Terraform in Azure

Ā·

4 min read

Table of contents

No heading

No headings in the article.

Terraform Initialize

The command terraform init initialize a terraform environment, and downloading providers and modules.

I used cloud shell to make run terraform on. First thing I did was make a directory just for terraform using the mkdir command and then changing into that directory.

I then used the command code provider.tf to make a file called provider.tf and opened it using the Azure IDE called Monaco.

Thirdly, I copied the provider code from the terraform registry and paste it in the file.

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.14.0"
    }
  }
}

provider "azurerm" {
  features {}

  storage_use_azuread = true
}

Afterwards I ran terraform init

image_2022-07-18_195652607.png

Terraform Format

The terraform fmt command automatically format the files your coding in.

So first thing I did is make a variables.tf and main.tf file using the touch command. Once I did that I open all the files in the directory with code . command.

I wanted to make a resource group first so I looked for the template in the registry and come across this.

resource "azurerm_resource_group" "example" {
  name     = "example"
  location = "West Europe"
}

I edited it with my location name which is ā€œEast USā€. Now letā€™s go to out variables file and add a variable. Hereā€™s what I put in my variable file.

variable "resource_group_name" {
  description = "Name for the resource group"
  type        = string
  default     = "terraform-basics"
}

Now letā€™s format to see if thereā€™s any format changes.

image_2022-07-18_200546703.png

Terraform validate

Command terraform validate confirms if the files are syntactically and logically sound. Basically making sure everything makes sense.

Terraform Plan

terraform plan displays the actions Terraform will take.

image_2022-07-18_201138304-1024x624.png

I know some of your like ā€œwait Artist my output doesnā€™t look like thatā€, thatā€™s because I add more to my files.

In my main.tf file I add a container group and put it in my resource group.

resource "azurerm_resource_group" "basics" {
  name     = var.resource_group_name
  location = var.location
}
resource "azurerm_container_group" "example" {
  name                = var.container_group_name
  location            = azurerm_resource_group.basics.location
  resource_group_name = azurerm_resource_group.basics.name
  ip_address_type     = "Public"
  dns_name_label      = "${var.prefix}-${var.container_group_name}"
  os_type             = "Linux"

  container {
    name   = "inspectorgadget"
    image  = "jelledruyts/inspectorgadget:latest"
    cpu    = "0.5"
    memory = "1.0"

    ports {
      port     = 80
      protocol = "TCP"
    }
  }
}

Before I continue on I just want to explain my resource group name and location in the resource ā€œazure container groupā€. So I wanted to add the container to a resource group right? I had to specific the name, I told terraform to look in the resource ā€œazurerm_resource_groupā€ with ā€œbasicsā€ and under name you should get the name of the resource group. Same thing with the location.

And in my variables.tf file I just declared the variables needed.

variable "resource_group_name" {
  description = "Name for the resource group"
  type        = string
  default     = "terraform-basics"
}
variable "location" {
  description = "Azure region"
  type        = string
  default     = "East US"
}
variable "container_group_name" {
  description = "name of the container group"
  type        = string
  default     = "terraform-basics"
}
variable "prefix" {
  description = "Prefix string to ensure FQDNs are globally unique"
  type        = string
}

Even made a terraform.tfvars file to specific my variables more.

location = "eastus"
prefix   = "Artist"

Terraform Apply

terraform applyjust builds out or creates your resources in Azure.

Now letā€™s go ahead and apply.

image_2022-07-18_201930978.png

Again it will look different to your output because I applied before adding more code to the files.

Terraform Destroy

image_2022-07-18_203533818-1024x362.png

And now to destroy all that I created šŸ™ with the terraform destroy command. Bye Bye hard work šŸ™ lol.

Morale of the story: Nothing to extravagant, just wanted to brush up on some Terraform in Azure. To me Azure is a lot to learn then AWS. Just the other day I didnā€™t know what a cloud shell was and when I finally figured out I canā€™t stop using it lol. Doing different things to see how they all flow together is how I learn! Hope this article give you some insight.

P.S I was delighted to receive some feedback from a reader via email! He lead me to an article that goes more in-depth with Terraform commands and was written by Jack Roper, you can check it out here. Thank you Mariusz Michalowski!

Ā