6

We want to deploy our infrastructure through Terraform on Azure cloud. The code I want to apply uses JSON template code made by Azure itself.

Code (scrubbed and removed unimportant JSON code):

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        # JSON beginning
    {
     "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
     "contentVersion": "1.0.0.0",
     "parameters": {
       "agentCount": {
         "allowedValues": [
           1,
           2,
           3,
           4,
         ],
         "defaultValue": 2,
         "metadata": {
            "description": "The number of agents for the cluster. This value can be from 1 to 100"
         },
         "type": "int"
        },

    DEPLOY      # JSON ending

      # these key-value pairs are passed into the ARM Template's `parameters` block
      parameters {
        "agentCount" = "3"          # Should be a integer/number?
        "agentEndpointDNSNamePrefix" = "-secret-"
        "agentSubnet" = "10.0.0.0/16"
        "agentVMSize" = "Standard_D2_v2"
        "firstConsecutiveStaticIP" = "172.16.0.5"
        "linuxAdminUsername" = "-secret-"
        "masterEndpointDNSNamePrefix" = "-secret-"
        "masterSubnet" = "172.16.0.0/24"
        "masterVMSize" = "Standard_D2_v2"
        "sshRSAPublicKey" = "-secret-"
        "targetEnvironment" = "AzurePublicCloud"
      }

      deployment_mode = "Incremental"
    }

The problem

agentCount parameter value doesn't work.

The error

Error: Error applying plan:

1 error(s) occurred:

* azurerm_template_deployment.Docker: 1 error(s) occurred:

* azurerm_template_deployment.Docker: Error creating deployment: resources.DeploymentsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidTemplate" Message="Deployment template validation failed: 'The provided value for the template parameter 'agentCount' at line '1' and column '494' is not valid.'."

Q: How can I make the "agentCount" JSON parameter an integer?

U.Durk
  • 73
  • 1
  • 6
  • well, from the arm template standpoint this is correct – 4c74356b41 Mar 28 '18 at 12:42
  • Heredocs require you to remove all indentation relative to them so your `template_body` that follows the `< – ydaetskcoR Mar 28 '18 at 13:18
  • @4c74356b41 - Can you explain this? This `agentCount` parameter does not work like the other parameters do. I want to edit the `agentCount` parameter to the _n_ amount of agents/vm's I want to deploy. I understand that the Azure made template is perfect, but why can I not change the parameters which are in the Terraform part written in HCL? – U.Durk Mar 28 '18 at 14:28
  • @ydaetskcoR - I tried fixing it exactly like you said and also a couple of other combinations, but it did not work and I got the same error. Appreciate the help tho! – U.Durk Mar 28 '18 at 14:28
  • @U.Durk i have no idea about crappyform. I'm only telling you that ARM Template part is correct – 4c74356b41 Mar 28 '18 at 14:31
  • @4c74356b41 Ok. Well, thanks for the info and thank you for your time! :) – U.Durk Mar 28 '18 at 14:50

1 Answers1

10

Unfortunately terraform cannot pass integer parameters. We pass all parameters as strings, and then convert those to integer variables like that:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringToConvert": { 
            "type": "string",
            "defaultValue": "4"
        }
    },
    "variables": {
        "integerFromString": "[int(parameters('stringToConvert'))]"
    }
    "resources": [],
    "outputs": {
        "intResult": {
            "type": "int",
            "value": "[variables('integerFromString')]"
        }
    }
}
Draco Ater
  • 20,820
  • 8
  • 62
  • 86