最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure resource manager - arm-ttk: build arm from bicep. Reuse of modules leads to linter problem 'Undefined parameter re

programmeradmin1浏览0评论

lets take this sample bicep:

param apiStorageAccountName string
param uiStorageAccountName string
param tags object = {}

module apiStorage './storage.bicep' = {
  name: 'deploy-api-storage'
  params: {
    storageAccountName: apiStorageAccountName
    tags: tags
  }
}

module uiStorage './storage.bicep' = {
  name: 'deploy-ui-storage'
  params: {
    storageAccountName: uiStorageAccountName
    tags: tags
  }
}
param storageAccountName string
param tags object

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
  name: storageAccountName
  location: resourceGroup().location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }
  tags: tags
}

when I build az bicep build --file test.bicep --outfile test.json

the linter throws errors and does not pass the armttk, which is our requirement.

I run the armttk task in an azure pipeline. the actual template is a lot bigger and i get tons of these linter errors

- task: RunARMTTKTestsXPlat@1
  inputs:
    templatelocation: './SampleProject/marketplace/*'
    recurse: false
    resultLocation: '$(System.DefaultWorkingDirectory)/results'
    allTemplatesMain: false
    cliOutputResults: true

The problem seems to be that it looks for the outer template parameter instead of the inner template parameter and does not find a matching parameter.

How can the problem be solved?

lets take this sample bicep:

param apiStorageAccountName string
param uiStorageAccountName string
param tags object = {}

module apiStorage './storage.bicep' = {
  name: 'deploy-api-storage'
  params: {
    storageAccountName: apiStorageAccountName
    tags: tags
  }
}

module uiStorage './storage.bicep' = {
  name: 'deploy-ui-storage'
  params: {
    storageAccountName: uiStorageAccountName
    tags: tags
  }
}
param storageAccountName string
param tags object

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
  name: storageAccountName
  location: resourceGroup().location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }
  tags: tags
}

when I build az bicep build --file test.bicep --outfile test.json

the linter throws errors and does not pass the armttk, which is our requirement.

I run the armttk task in an azure pipeline. the actual template is a lot bigger and i get tons of these linter errors

- task: RunARMTTKTestsXPlat@1
  inputs:
    templatelocation: './SampleProject/marketplace/*'
    recurse: false
    resultLocation: '$(System.DefaultWorkingDirectory)/results'
    allTemplatesMain: false
    cliOutputResults: true

The problem seems to be that it looks for the outer template parameter instead of the inner template parameter and does not find a matching parameter.

How can the problem be solved?

Share Improve this question edited Mar 28 at 14:52 artless-noise-bye-due2AI 22.5k6 gold badges73 silver badges110 bronze badges asked Mar 28 at 14:15 DaveVenturaDaveVentura 7701 gold badge11 silver badges24 bronze badges 3
  • I think your bicep and compile-out Json will work well. I have just copy and build your bicep file. not throwing warning or error msg with vscode arm extension build-in grammar check – wenbo Commented Mar 31 at 2:07
  • yes the bicep and the arm works. but it does not pass the armttk validation. what is currently our requirement. – DaveVentura Commented Mar 31 at 9:47
  • Try passing location as a parameter instead of hardcoding resourceGroup().location and update the API version to 2023-04-01 for Microsoft.Storage/storageAccounts, and explicitly pass parameters in test.bicep to align with storage.bicep. Alone with this, ensure all parameters are properly referenced to avoid ARM TTK validation errors and maintain a modular approach by correctly passing values between templates. @DaveVentura – Vinay B Commented Mar 31 at 17:13
Add a comment  | 

1 Answer 1

Reset to default 0

arm-ttk: build arm from bicep. Reuse of modules leads to linter problem 'Undefined parameter reference

The issue seems to be with the way test.bicep was passing storageAccountName to the storage.bicep module, but the ARM TTK was expecting a parameter in the outer template that matched it.

To overcome this issue you can try need to make two major changes update the API version to 2023-04-01 for Microsoft.Storage/storageAccount & declared parameters like apiStorageAccountName, uiStorageAccountName, tags …etc. in test.bicep & module calls to pass the correct parameters to storage.bicep.

I tried the sample configuration as per the requirement, It works in my case.

test.bicep:

param apiStorageAccountName string
param uiStorageAccountName string
param tags object = {}
param location string = resourceGroup().location

module apiStorage './storage.bicep' = {
  name: 'deploy-api-storage'
  params: {
    storageAccountName: apiStorageAccountName
    tags: tags
    location: location
  }
}

module uiStorage './storage.bicep' = {
  name: 'deploy-ui-storage'
  params: {
    storageAccountName: uiStorageAccountName
    tags: tags
    location: location
  }
}

storage.bicep:

param storageAccountName string
param tags object
param location string

resource storageAccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
  name: storageAccountName
  location: location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }
  tags: tags
}

Now run the command

az bicep build --file test.bicep --outfile test.json

this will create test.json as mentioned below

{
  "$schema": "https://schema.management.azure/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.34.44.8038",
      "templateHash": "2858183920885186607"
    }
  },
  "parameters": {
    "apiStorageAccountName": {
      "type": "string"
    },
    "uiStorageAccountName": {
      "type": "string"
    },
    "tags": {
      "type": "object",
      "defaultValue": {}
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "deploy-api-storage",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "inner"
        },
        "mode": "Incremental",
        "parameters": {
          "storageAccountName": {
            "value": "[parameters('apiStorageAccountName')]"
          },
          "tags": {
            "value": "[parameters('tags')]"
          },
          "location": {
            "value": "[parameters('location')]"
          }
        },
        "template": {
          "$schema": "https://schema.management.azure/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "metadata": {
            "_generator": {
              "name": "bicep",
              "version": "0.34.44.8038",
              "templateHash": "13996871031537357373"
            }
          },
          "parameters": {
            "storageAccountName": {
              "type": "string"
            },
            "tags": {
              "type": "object"
            },
            "location": {
              "type": "string"
            }
          },
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2023-04-01",
              "name": "[parameters('storageAccountName')]",
              "location": "[parameters('location')]",
              "kind": "StorageV2",
              "sku": {
                "name": "Standard_LRS"
              },
              "tags": "[parameters('tags')]"
            }
          ]
        }
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "deploy-ui-storage",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "inner"
        },
        "mode": "Incremental",
        "parameters": {
          "storageAccountName": {
            "value": "[parameters('uiStorageAccountName')]"
          },
          "tags": {
            "value": "[parameters('tags')]"
          },
          "location": {
            "value": "[parameters('location')]"
          }
        },
        "template": {
          "$schema": "https://schema.management.azure/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "metadata": {
            "_generator": {
              "name": "bicep",
              "version": "0.34.44.8038",
              "templateHash": "13996871031537357373"
            }
          },
          "parameters": {
            "storageAccountName": {
              "type": "string"
            },
            "tags": {
              "type": "object"
            },
            "location": {
              "type": "string"
            }
          },
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2023-04-01",
              "name": "[parameters('storageAccountName')]",
              "location": "[parameters('location')]",
              "kind": "StorageV2",
              "sku": {
                "name": "Standard_LRS"
              },
              "tags": "[parameters('tags')]"
            }
          ]
        }
      }
    }
  ]
}

now run the Run ARM TTK validation

Test-AzTemplate -TemplatePath "path to the directory\test.json"

Now run the command

az deployment group create --resource-group MyResourceGroup --template-file test.bicep --parameters apiStorageAccountName="apistorage123" uiStorageAccountName="uistorage123"

Now run the command below to check and list the existing accounts

az storage account list --resource-group Myresourcegroup --query "[].{Name:name, Location:primaryLocation, Status:statusOfPrimary}" --output table

Refer:

https://learn.microsoft/en-us/azure/templates/microsoft.storage/storageaccounts?view=azure-resource-manager-2023-04-01&pivots=deployment-language-bicep

https://github/Azure/arm-ttk

https://learn.microsoft/en-us/azure/azure-resource-manager/templates/deploy-portal

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论