Create an Azure Managed Lustre file system by using Terraform

In this article, you use Terraform to create an Azure Managed Lustre file system.

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

In this article, you learn how to:

Note

The code examples in this article use the random_pet and random_string resources to generate unique values for the resource group name and the Managed Lustre file system name. You can replace these values with your own resource names in the variables.tf and main.tf files.

Prerequisites

Implement the Terraform code

Note

The sample code for this article comes from the Azure Terraform GitHub repo. You can view the log file that contains the test results from current and previous versions of Terraform.

For more articles and sample code that show how to use Terraform to manage Azure resources, see the main page for Terraform on Azure documentation.

  1. Create a directory in which to test the sample Terraform code, and make it the current directory.

  2. Create a file named providers.tf and insert the following code:

    terraform {
      required_version = ">=1.0"
    
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
  3. Create a file named main.tf and insert the following code:

    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    resource "random_string" "azurerm_virtual_network_name" {
      length  = 13
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_virtual_network" "example" { 
      name = coalesce(var.virtual_network_name, "vnet-${random_string.azurerm_virtual_network_name.result}")
      resource_group_name = azurerm_resource_group.rg.name
      address_space = ["10.0.0.0/16"] 
      location = azurerm_resource_group.rg.location 
    }
    
    resource "random_string" "azurerm_subnet_name" {
      length  = 13
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_subnet" "example" { 
      name = coalesce(var.subnet_name, "subnet-${random_string.azurerm_subnet_name.result}")
      resource_group_name = azurerm_resource_group.rg.name 
      virtual_network_name = azurerm_virtual_network.example.name 
      address_prefixes = ["10.0.2.0/24"]
    }
    
    resource "random_string" "azurerm_amlfs_name" {
      length  = 13
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_managed_lustre_file_system" "example" { 
      name = coalesce(var.amlfs_name, "amlfs-${random_string.azurerm_amlfs_name.result}")
      resource_group_name = azurerm_resource_group.rg.name
      location = azurerm_resource_group.rg.location 
      sku_name = var.amlfs_sku_name
      subnet_id = azurerm_subnet.example.id 
      storage_capacity_in_tb = var.amlfs_storage_capacity_in_tb 
      zones = ["1"] 
      maintenance_window {
        day_of_week = var.amlfs_maintenance_day_of_week
        time_of_day_in_utc = var.amlfs_maintenance_time_of_day
      }
    }
    
  4. Create a file named variables.tf and insert the following code:

    variable "resource_group_name_prefix" {
      type        = string
      default     = "rg"
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
    }
    
    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "virtual_network_name" {
      type        = string
      description = "The name of the virtual network resource. The value will be randomly generated if blank."
      default     = ""
    }
    
    variable "subnet_name" {
      type        = string
      description = "The name of the virtual network subnet. The value will be randomly generated if blank."
      default     = ""
    }
    
    variable "amlfs_name" {
      type        = string
      description = "The name of the Manage Lustre file system resource. The value will be randomly generated if blank."
      default     = ""
    }
    
    variable "amlfs_sku_name" {
      type        = string
      default     = "AMLFS-Durable-Premium-40"
      validation {
        condition     = contains(["AMLFS-Durable-Premium-40", "AMLFS-Durable-Premium-125", "AMLFS-Durable-Premium-250", "AMLFS-Durable-Premium-500"], var.amlfs_sku_name)
        error_message = "The SKU value must be one of the following: AMLFS-Durable-Premium-40, AMLFS-Durable-Premium-125, AMLFS-Durable-Premium-250, AMLFS-Durable-Premium-500."
      }
      description = "SKU name for the Azure Managed Lustre file system."
    }
    
    variable "amlfs_storage_capacity_in_tb" {
      type        = number
      default     = 48
      description = "The size of the Managed Lustre file system, in TiB. This might be rounded up."
    }
    
    variable "amlfs_maintenance_day_of_week" {
      type        = string
      default     = "Saturday"
      validation {
        condition     = contains(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], var.amlfs_maintenance_day_of_week)
        error_message = "The maintenance day of week value must be one of the following: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday."
      }
      description = "Day of the week on which the maintenance window will occur."
    }
    
    variable "amlfs_maintenance_time_of_day" {
      type        = string
      default     = "02:00"
      description = "The time of day (in UTC) to start the maintenance window."
    }
    
  5. Create a file named outputs.tf and insert the following code:

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "virtual_network_name" {
      value = azurerm_virtual_network.example.name
    }
    
    output "subnet_name" {
      value = azurerm_subnet.example.name
    }
    
    output "managed_lustre_file_system_name" {
      value = azurerm_managed_lustre_file_system.example.name
    }
    
    output "amlfs_sku_name" {
      value = azurerm_managed_lustre_file_system.example.sku_name
    }
    
    output "amlfs_storage_capacity_in_tb" {
      value = azurerm_managed_lustre_file_system.example.storage_capacity_in_tb
    }
    

Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.

terraform init -upgrade

Key points:

  • The -upgrade parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.

Create a Terraform execution plan

Run terraform plan to create an execution plan.

terraform plan -out main.tfplan

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.

Apply a Terraform execution plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

terraform apply main.tfplan

Key points:

  • The example terraform apply command assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, call terraform apply without any parameters.

Verify the results

  1. Get the name of the Azure resource group:

    resource_group_name=$(terraform output -raw resource_group_name)
    
  2. Get the name of the Managed Lustre file system:

    managed_lustre_file_system_name=$(terraform output -raw managed_lustre_file_system_name)
    
  3. Run az amlfs show to display the name of the Managed Lustre file system:

    az amlfs show --resource-group $resource_group_name \
                  --name $managed_lustre_file_system_name \
    
    

Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

    terraform plan -destroy -out main.destroy.tfplan
    

    Key points:

    • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
    • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  2. Run terraform apply to apply the execution plan.

    terraform apply main.destroy.tfplan
    

Troubleshoot Terraform on Azure

See Troubleshoot common problems when using Terraform on Azure.