Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Q: Can I insert integer/number in parameter code for Azure's JSON template code in Terraform?

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?

like image 312
U.Durk Avatar asked Mar 28 '18 12:03

U.Durk


1 Answers

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')]"
        }
    }
}
like image 91
Draco Ater Avatar answered Oct 22 '22 08:10

Draco Ater