skip to Main Content

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.rnTo 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.rnPath 'properties.parameters.connectionStrings', line 1, position 280.'."
}

2

Answers


  1. Chosen as BEST ANSWER

    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"
        }
      ]
    },
    
    

  2. Instead of passing connectionStrings in parameters.json file, Include it with an array type in main.bicep file. If you pass in parameters.json it is expecting an object type and that is why for loop is throwing deserialized object error.

    Usually to avoid this error, Pass for loop as

    [for (connectionStrings, i) in connectionStrings: {
    connectionStrings : <ResourceName>.properties.connectionStrings[i]
    }
    

    Alternatively, I would suggest you add in main.bicep file with param keyword as detailed above.

    By referring to the sample bicep template, I made a few changes to your code and was able to deploy it successfully.

    main.bicep:

    param webAppName string = <AppNAme>
    param sku string = '<SKU>'
    param linuxFxVersion string = '' 
    param location string = resourceGroup().location 
    var appServicePlanName = <appServicePlanName>
    var webSiteName = <webSiteName>
    param connectionStrings array = [
                {
                    name: 'Tracking'
                    connectionString: ''
                    type: 'SQLServer'
                }
                {
                    name: 'Logs'
                    connectionString: ''
                    type: 'SQLServer'
                }
            ]
    resource appServicePlan 'Microsoft.Web/serverfarms@2020-06-01' = {
      name: appServicePlanName
      location: location
      properties: {
        reserved: true
      }
      sku: {
        name: sku
      }
      kind: 'linux'
    }
    
    resource appService 'Microsoft.Web/sites@2020-06-01' = {
      name: webSiteName
      location: location
      properties: {
        serverFarmId: appServicePlan.id
        siteConfig: {
          linuxFxVersion: linuxFxVersion
        connectionStrings: [for connectionString in connectionStrings: {
            name: connectionString.name
            connectionString: connectionString.connectionString
      }]
    }
      }
    }
    

    Build & deployment got succeeded:

    enter image description here

    Deployed a WebApp successfully:

    enter image description here

    Added connection Strings in WebApp -> Configuration:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search