Skip to main content

Terraform Basics

Display the current version:

terraform –version

Update Terraform:

terraform init -upgrade

Apply a configruation file tfvars:

terraform apply -var-file="terraform.tfvars"

Auto Approve:

terraform apply -auto-approve

Format

 terraform fmt -recursive

Validate the current configuration

 terraform validate

State reconciliation

terraform refresh

terraform state list

terraform state list

Create Azure remote TFState​

$RESOURCE_GROUP_NAME='rg-terraform'
$STORAGE_ACCOUNT_NAME="stotfbga001"
$CONTAINER_NAME='tfstate'
$LOCATION = "westeurope"

# Create resource group
New-AzResourceGroup -Name $RESOURCE_GROUP_NAME -Location $LOCATION

# Create storage account
$storageAccount = New-AzStorageAccount -ResourceGroupName $RESOURCE_GROUP_NAME -Name $STORAGE_ACCOUNT_NAME -SkuName Standard_LRS -Location westeurope -AllowBlobPublicAccess $true

# Create blob container
New-AzStorageContainer -Name $CONTAINER_NAME -Context $storageAccount.context -Permission blob

$ACCOUNT_KEY=(Get-AzStorageAccountKey -ResourceGroupName $RESOURCE_GROUP_NAME -Name $STORAGE_ACCOUNT_NAME)[0].value
$env:ARM_ACCESS_KEY=$ACCOUNT_KEY

Terraform version repository:

https://developer.hashicorp.com/terraform/downloads

https://releases.hashicorp.com/terraform/

Create RG with tags

# // Create resource group
# resource "azurerm_resource_group" "advisor-recommendations" {
# name = "rg-${var.project_name}"
# location = var.location
# tags = {
# "Environment" = "Prd",
# "ManagedBy" = "Benoit",
# "CreatedBy" = "Benoit",
# "Usage" = "Export advisor recommendations power bi dashboard",
# "IaC" = "Terraform",
# "GitRepo" = "https://github.com/advisor-recommendations",
# }
# }

Calculate date

 // Calculate and format tomorrow date and time
// "Year-Month-DayTHour:Minute:Second+TimeZoneOffset" (UTC +2h)
locals {
tomorrow = timeadd(timestamp(), "24h")
//tomorrow_at_7h30 = "${formatdate("YYYY-MM-DD", local.tomorrow)}T07:30:00+02:00"
}