Search code examples
arraysjsonazurefor-loopazure-bicep

Pass array of connection strings in parameters file to bicep file to be used for loop for application service


I am trying to create an application service to deploy via a CI/CD pipeline, however I am struggling create a For loop for the connection strings.

Im my parameters file I have the following:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        // Required Parameters
        ...
        // Optional Parameters
        "connectionStrings": [
            {
                "name": "Tracking",
                "connectionString": "...",
                "type": "SQLServer"
            }
            {
                "name": "Logs",
                "connectionString": "...",
                "type": "SQLServer"
            }
        ],
        ...
    }
}

I am passing this into the main.bicep file and the appService.bicep module file as a param connectionStrings array.

In the appService.bicep file I have the following for loop.

resource appServiceWeb 'Microsoft.Web/sites@2022-03-01' = {
  name: nameWeb
  location: location
  tags: tags
  kind: kind
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: appServicePlan_id
    siteConfig: {
      appSettings: [
        ...
      ]
      connectionStrings: [for connectionString in connectionStrings: {
        name: connectionString.name
        connectionString: connectionString.connectionString
        type: connectionString.type
      }]
    }
  }
}

I am getting the following error:

{
  "code": "InvalidRequestContent",
  "message": "The request content was invalid and could not be deserialized: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Azure.Deployments.Core.Definitions.DeploymentParameterDefinition' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath 'properties.parameters.connectionStrings', line 1, position 280.'."
}

Solution

  • I have managed to fix my issue. While my Json structure was legal, it wasn't the correct structure for bicep files.

    I needed to add the value part below:

    "connectionStrings": {
      "value": [
        {
          "name": "TrackingEntities",
          "connectionString": "...",
          "type": "SQLServer"
        }
      ]
    },