You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在Terraform的Azure JSON模板参数代码中插入整数/数字?

在Terraform部署的Azure JSON模板中使用整数参数

Absolutely! You can absolutely use integer/numeric parameters in your Azure JSON (ARM) template when deploying via Terraform—here's how to do it smoothly:

  • Step 1: Define integer parameters in your ARM template
    First, declare your integer parameter directly in the JSON template's parameters block. You can set a default value and description for clarity:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "workerNodeCount": {
          "type": "int",
          "defaultValue": 2,
          "metadata": {
            "description": "Number of Docker Swarm worker nodes to deploy"
          }
        }
      },
      "resources": [
        // Use the parameter in your resource definitions, e.g., for loops or resource properties:
        // "[parameters('workerNodeCount')]"
      ]
    }
    
  • Step 2: Pass integer values from Terraform
    In your azurerm_template_deployment resource, you can either use the template's default integer value or override it via the parameters block. Terraform handles numeric values natively here, no extra formatting needed:

    resource "azurerm_resource_group" "docker" {
      name     = "CSI-DockerSwarm"
      location = "West Europe"
    }
    
    resource "azurerm_template_deployment" "Docker" {
      name                = "Example-Deployment"
      resource_group_name = azurerm_resource_group.docker.name
      template_body       = <<DEPLOY
    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "workerNodeCount": {
          "type": "int",
          "defaultValue": 2
        }
      },
      "resources": [
        // Your resource definitions using the integer parameter go here
      ]
    }
    DEPLOY
    
      // Override the default integer value if needed
      parameters = {
        workerNodeCount = 4 // Directly pass an integer here
      }
    }
    
  • Bonus: Use Terraform variables for dynamic integers
    If you want to make the value configurable in your Terraform setup, define a numeric variable and reference it:

    variable "swarm_worker_count" {
      type        = number
      description = "Number of Docker Swarm worker nodes to provision"
      default     = 2
    }
    
    // Then in your template deployment:
    parameters = {
      workerNodeCount = var.swarm_worker_count
    }
    

This approach keeps your setup flexible, follows ARM template best practices, and plays nicely with Terraform's type system.

内容的提问来源于stack exchange,提问作者U.Durk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:30:53