AzureRM Terraform Provider

repository·main·Indexed 26 days ago

https://github.com/hashicorp/terraform-provider-azurerm

The AzureRM Terraform provider manages the lifecycle of Azure resources, including Azure NetApp Files (ANF) volumes, capacity pools, and network features. It includes an internal SDK for strongly-typed resources and a preflight validation system that wraps the Azure Preflight Validation API to validate resource configurations during the terraform plan phase.

Tokens
375.7K
Snippets
650
Records
2K
Agent score
89%

What's inside terraform-provider-azurerm

  1. Review AzureRM v3.0 Behavioral Updates

    main

    The following behavioral changes were introduced in version 3.0:

    • Resource ID Validation: Resource IDs are validated at import time to ensure correct formats (e.g., preventing a VM Extension ID from being used where a VM ID is expected).
    • Minimum TLS Version: The default min_tls_version for resources supporting it is now 1.2.
    • Managed Identity: The presence of an identity block now explicitly dictates whether a Managed Identity is assigned. Omitting the block or setting it to null means no identity is assigned.
    • Application Gateway: Nested items are now treated as Sets instead of Lists (order no longer matters).
    • Firewall: Nested items are now treated as Lists instead of Sets (order now matters).
    • API Management: Terraform now removes the Default API and Products when creating a new instance.
    • Log Analytics & Recovery Services: The tags field has been removed from various resources.
    • Database Migration Service: The provider will now delete azurerm_database_migration_service even if running tasks exist.
    • IoT Hub: A Fallback Route is enabled by default on azurerm_iothub.
    • MSSQL Database: transparent_data_encryption_enabled is set to true by default and cannot be disabled on non-DW SKUs.
  2. Use the azurerm_virtual_machine resource

    main

    The azurerm_virtual_machine resource manages a Virtual Machine in Azure.

    Important Migration Note: This resource has been superseded by azurerm_linux_virtual_machine and azurerm_windows_virtual_machine. While azurerm_virtual_machine remains available for compatibility, it is in a feature-frozen state. New functionality is only added to the Linux and Windows specific resources.

    Disk Attachment Warning: You can attach data disks either directly within the azurerm_virtual_machine resource or by using the azurerm_virtual_machine_data_disk_attachment resource. Do not use both methods on the same Virtual Machine, as this will cause spurious changes in your Terraform state.

  3. Manage a Stream Analytics Job Storage Account with azurerm_stream_analytics_job_storage_account

    main

    Use the azurerm_stream_analytics_job_storage_account resource to manage the storage account associated with a Stream Analytics Job. This is particularly useful when using Msi (Managed Service Identity) authentication.

    Important Note on Resource Conflicts: A Stream Analytics Job's storage account can also be managed directly within the azurerm_stream_analytics_job resource using a job_storage_account block. Do not manage the storage account through both resources, as this will cause configuration conflicts. If you use this standalone resource, it is recommended to add a lifecycle block with ignore_changes = [job_storage_account] to the azurerm_stream_analytics_job resource.

    resource "azurerm_resource_group" "example" {
      name     = "example-resources"
      location = "West Europe"
    }
    
    resource "azurerm_stream_analytics_job" "example" {
      name                                     = "example-job"
      resource_group_name                      = azurerm_resource_group.example.name
      location                                 = azurerm_resource_group.example.location
      compatibility_level                      = "1.2"
      data_locale                              = "en-GB"
      events_late_arrival_max_delay_in_seconds = 60
      events_out_of_order_max_delay_in_seconds = 50
      events_out_of_order_policy               = "Adjust"
      output_error_policy                      = "Drop"
      streaming_units                          = 3
      sku_name                                 = "StandardV2"
    
      identity {
        type = "SystemAssigned"
      }
    
      tags = {
        environment = "Example"
      }
    
      transformation_query = <<QUERY
        SELECT *
        INTO [YourOutputAlias]
        FROM [YourInputAlias]
    QUERY
    
      lifecycle {
        ignore_changes = [job_storage_account]
      }
    }
    
    resource "azurerm_storage_account" "example" {
      name                     = "exampleaccount"
      resource_group_name      = azurerm_resource_group.example.name
      location                 = azurerm_resource_group.example.location
      account_tier             = "Standard"
      account_replication_type = "LRS"
    }
    
    resource "azurerm_stream_analytics_job_storage_account" "example" {
      stream_analytics_job_id = azurerm_stream_analytics_job.example.id
      storage_account_name    = azurerm_storage_account.example.name
      authentication_mode     = "Msi"
    }
  4. Use the SDK for Strongly-Typed Resources

    main

    The internal/sdk package is used to implement strongly-typed Data Sources and Resources within the AzureRM provider. Using this SDK instead of untyped resources provides several safety guarantees:

    • Context Safety: The Context object passed into each method always includes an attached deadline/timeout.
    • Automated Read: The Read function is automatically invoked at the conclusion of Create and Update functions, reducing boilerplate.
    • Mandatory Implementation: Every Resource is required to implement an ID Formatter and a Validation Function.
    • Schema Validation: Model objects are validated via unit tests to ensure all necessary tfschema struct tags are present.

    This approach shifts error detection from Provider Initialization to the Compilation or Unit Testing phase, significantly shortening the feedback loop for developers.

  5. Manage Azure Front Door (classic) with azurerm_frontdoor

    main

    The azurerm_frontdoor resource manages an Azure Front Door (classic) instance.

    Important Deprecation Notice:

    • Azure Front Door (classic) is deprecated. New resource creation is no longer supported as of April 1, 2025.
    • Modifications to existing resources are supported until the API reaches full retirement on March 31, 2027.
    • It is highly recommended to migrate to Azure Front Door (standard/premium) resources using the official migration tool.

    Provider Version Note: As of provider version v2.58.0, the custom_https_provisioning_enabled field and custom_https_configuration block have been removed. To enable custom HTTPS functionality, you must now define a separate azurerm_frontdoor_custom_https_configuration block.

    resource "azurerm_resource_group" "example" {
      name     = "FrontDoorExampleResourceGroup"
      location = "West Europe"
    }
    
    resource "azurerm_frontdoor" "example" {
      name                = "example-FrontDoor"
      resource_group_name = azurerm_resource_group.example.name
    
      routing_rule {
        name               = "exampleRoutingRule1"
        accepted_protocols = ["Http", "Https"]
        patterns_to_match  = ["/*"]
        frontend_endpoints = ["exampleFrontendEndpoint1"]
        forwarding_configuration {
          forwarding_protocol = "MatchRequest"
          backend_pool_name   = "exampleBackendBing"
        }
      }
    
      backend_pool_load_balancing {
        name = "exampleLoadBalancingSettings1"
      }
    
      backend_pool_health_probe {
        name = "exampleHealthProbeSetting1"
      }
    
      backend_pool {
        name = "exampleBackendBing"
        backend {
          host_header = "www.bing.com"
          address     = "www.bing.com"
          http_port   = 80
          https_port  = 443
        }
    
        load_balancing_name = "exampleLoadBalancingSettings1"
        health_probe_name   = "exampleHealthProbeSetting1"
      }
    
      frontend_endpoint {
        name      = "exampleFrontendEndpoint1"
        host_name = "example-FrontDoor.azurefd.net"
      }
    }
  6. Manage Azure Resource Group Template Deployments with azurerm_resource_group_template_deployment

    main

    The azurerm_resource_group_template_deployment resource manages the deployment of Azure Resource Manager (ARM) templates into a specific Resource Group.

    Important Behavior: Nested Resource Deletion

    By default, when this resource is deleted, the provider will attempt to automatically delete all resources deployed by the ARM Template. To disable this behavior, set delete_nested_items_during_deletion to false within the provider's features block inside the template_deployment block.

    Deployment Modes

    • Incremental: Resources are additive. Existing resources in the Resource Group not specified in the template are left untouched.
    • Complete: Resources in the Resource Group that are not defined in the ARM Template will be destroyed.
  7. Enable Preflight Validation

    main

    Preflight validation uses the Azure Preflight Validation API at terraform plan time to surface errors like policy violations, quota breaches, and invalid property values before terraform apply.

    Requirements & Limitations:

    • Requires valid Azure credentials at terraform plan time.
    • Only supported for a subset of resource types.
    • Cannot validate arguments that are (known after apply) (computed values).

    Configuration

    Enable it within the enhanced_validation block:

    provider "azurerm" {
      features {
        enhanced_validation {
          preflight_enabled           = true
          preflight_location_fallback = "eastus2"
        }
      }
    }

    Alternatively, use the environment variable: ARM_PROVIDER_ENHANCED_VALIDATION_PREFLIGHT_ENABLED.

    Supported Resources

    • azurerm_app_service_environment_v3
    • azurerm_service_plan
    • azurerm_dashboard_grafana
    • azurerm_eventgrid_namespace
    • azurerm_managed_redis
    • azurerm_nginx_deployment
  8. Import an existing API Management Diagnostic

    main

    You can import an existing API Management Diagnostic using its resource ID via the Terraform CLI.

    Command Format: terraform import azurerm_api_management_diagnostic.example /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RG_NAME>/providers/Microsoft.ApiManagement/service/<SERVICE_NAME>/diagnostics/<IDENTIFIER>

    terraform import azurerm_api_management_diagnostic.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.ApiManagement/service/instance1/diagnostics/applicationinsights
  9. Import an existing API Management API Diagnostic

    main

    You can import an existing API Management Service API Diagnostics configuration into your Terraform state using the resource ID.

    Command Syntax: terraform import azurerm_api_management_api_diagnostic.example <resource_id>

    terraform import azurerm_api_management_api_diagnostic.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1/apis/api1/diagnostics/diagnostic1
  10. Migrate azurerm_cdn_frontdoor_rule to Version 5.0

    main

    The azurerm_cdn_frontdoor_rule resource has been significantly restructured to match azurerm_cdn_frontdoor_batch_rule_set.

    Key Property Changes:

    • behavior_on_match $\rightarrow$ behaviour_on_match
    • Header Actions: request_header_action and response_header_action are replaced by modify_request_header and modify_response_header. The sub-properties header_action and value are now operator and header_value respectively.
    • Route Overrides: route_configuration_override_action $\rightarrow$ route_configuration_override. Caching and origin group settings are now nested under caching and origin_group blocks.
    • Redirects: url_redirect_action $\rightarrow$ url_redirect. Note that destination_host_name, destination_path, destination_fragment, and query_string no longer accept literal empty strings; to preserve the incoming value, omit the argument entirely.
    • Rewrites: url_rewrite_action $\rightarrow$ url_rewrite. The destination property is now url_redirect.destination_path and preserve_unmatched_path is now url_redirect.preserve_unmatched_path_enabled.

    Condition Changes: Most condition blocks (e.g., client_port_condition, cookies_condition, host_name_condition, http_version_condition, is_device_condition, post_args_condition, query_string_condition) have been renamed to simpler forms (e.g., client_port, request_cookies, host_name, http_version, device_type, post_argument, query_string).

    For these conditions:

    • match_values is replaced by values.
    • negate_condition is replaced by an operator field using Not{Operator} values.
    • http_version.operator and device_type.operator are now required.
    • device_type.values and post_argument.values are now required.