Table of contents
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
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.
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.
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 apply
just builds out or creates your resources in Azure.
Now letās go ahead and apply.
Again it will look different to your output because I applied before adding more code to the files.
Terraform Destroy
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!