Skip to content

Terraform Input Variables with Structural Type tuple

Step-01: Introduction

  • Learn about Terraform Variables Structural Types
  • Structural types in Terraform allow multiple values of different types to be grouped together as a single value.
  • Using structural types requires a data schema to be defined for the Input Variables type so that Terraform knows what a valid value is.
  • Implement Input Variable Structural Type object
  • object(): A collection of values each with their own type.
# Sample Object
variable "os_configs" {
  type = object({
    location       = string
    size           = string
    instance_count = number
  })
}
  • tuple(): A sequence of values each with their own type.
# Sample tuple()
variable "tuple_sample" {
  type = tuple([string, number, bool])
}

Step-02: c2-variables.tf

  • We are going to enable Threat Detection Policy in Azure MySQL Database.
  • For that threat_detection_policy block we are going to implement the Input Variable Structural Type tuple()
  • Review documentation azurerm_mysql_server
# 12. Azure MySQL DB Threat Detection Policy (Variable Type: tuple)
variable "tdpolicy" {
    description = "Azure MySQL DB Threat Detection Policy"
    type = tuple([bool, number, bool, list(string)])
}

Step-03: Update Azure MySQL Server sku_name Tier

  • Threat Detection Policy is not supported for Basic Tier
  • We need to Update that to General Purpose Tier
  • c4-azure-mysql-database.tf
# Before
 sku_name   = "B_Gen5_2" # Basic Tier

# After
 sku_name = "GP_Gen5_2"   # General Purpose Tier

# Supported Values (as on today)
[B_Gen4_1 B_Gen4_2 B_Gen5_1 B_Gen5_2 GP_Gen4_2 GP_Gen4_4 GP_Gen4_8 GP_Gen4_16 GP_Gen4_32 GP_Gen5_2 GP_Gen5_4 GP_Gen5_8 GP_Gen5_16 GP_Gen5_32 GP_Gen5_64 MO_Gen5_2 MO_Gen5_4 MO_Gen5_8 MO_Gen5_16 MO_Gen5_32]

Step-04: Update terraform.tfvars

# DB Variables
db_name = "mydb101"
db_storage_mb = 5120
db_auto_grow_enabled = true
tdpolicy = [true, 10, true, [ "dkalyanreddy@gmail.com", "stacksimplify@gmail.com" ]]

Step-05: Add the Threat Detection Policy Block in c4-azure-mysql-database.tf

  • Refer both types below
# With Hard Coded Values
  threat_detection_policy {
    enabled = true
    retention_days = 10
    email_account_admins = true
    email_addresses = [ "dkalyanreddy@gmail.com", "stacksimplify@gmail.com" ]
  }  

# With Structural Type tuple() defined in Variables
  threat_detection_policy {
    enabled = var.tdpolicy[0]
    retention_days = var.tdpolicy[1]
    email_account_admins = var.tdpolicy[2]
    email_addresses = var.tdpolicy[3]
  }

Step-06: Execute Terraform Command

# Initialize Terraform
terraform init

# Validate Terraform configuration files
terraform validate

# Format Terraform configuration files
terraform fmt

# Review the terraform plan
terraform plan -var-file="secrets.tfvars"
Observation:
1. Review the values for Threat Detection Policy
2. All the values defined in "terraform.tfvars", tdpolicy variable should be replaced and shown in terraform execution plan. 

# Terraform Apply (Optional)
terraform apply -var-file="secrets.tfvars"

Step-07: Verify Azure MySQL DB Threat Detection Policy Settings

  • Go to Azure MySQL Database -> it-dev-mydb101 -> Security -> Azure Defender for MySQL
  • Verify the settings

Step-08: Clean-Up

# Destroy Resources
terraform destroy -var-file="secrets.tfvars"

# Clean-Up
rm -rf .terraform*
rm -rf terraform.tfstate*

References