Azure Bicep – 索引 ‘0’ 超出范围

huangapple go评论58阅读模式
英文:

Azure Bicep - index '0' is out of bounds

问题

Here is the translation of your provided code without the code comments and additional explanations:

I am trying to create a Private Endpoint, DNS and the DNS Zone Group and have all the code as a "one stop shop" and re-usable for other resources - Key Vault, Storage etc. In certain situations, a policy will create the DNS Zone Group, so therefore that functionality is not required, otherwise the deployment will error due to trying to provision more than one zone.

My code:

@description('Array: DNZ Zones')
param dnsZones array
@description('Array: Private Endpoints')
param privateEndPoints array

@description('Object: Tags')
param tags object

@description('String: Fully qualified ID of the resource in question')
param resourceId string

// Obtain existing Vnet
resource vnetExisting 'Microsoft.Network/virtualNetworks@2021-02-01' existing = [for vn in privateEndPoints: {
  name: vn.virtualNetworkName
  scope: resourceGroup(vn.virtualNetworkSubId, vn.virtualNetworkRG)
}]

// Obtain Resource ID for VNet/SubNet
resource subnetExisting 'Microsoft.Network/virtualnetworks/subnets@2021-02-01' existing = [for sn in privateEndPoints: {
  name: '${sn.virtualNetworkName}/${sn.subnetName}'
  scope: resourceGroup(sn.virtualNetworkSubId, sn.virtualNetworkRG)
}]

module privateEndPoint 'privateendpoint.bicep' = [for pe in privateEndPoints: if (!(empty(privateEndPoints))) {
  name: '${pe.peName}-privateendpoint-deploy'
  scope: resourceGroup(pe.peSubId, pe.peRG)
  params: {
    name: pe.peName
    serviceId: resourceId
    subnetId: subnetExisting[pe.indexValue].id
    resource: pe.resource
    tags: tags
  }
}]

module privateDNSZone 'privatednszone.bicep' = [for dz in dnsZones: /*if (!(empty(dnsZones)))*/ {
  dependsOn: [privateEndPoint]
  name: '${dz.dnsName}-dnszone-deploy'
  scope: resourceGroup(dz.dnsSubId, dz.dnsRG)
  params: {
    name: dz.dnsName
    tags: tags
  }
}]

// Link endpoint to dns
module privateDnsZoneGroup 'privatednszonegroup.bicep' = [for pe in privateEndPoints: /*if (!(empty(privateEndPoints)) && !(empty(dnsZones)))*/ {
  dependsOn: [privateDNSZone]
  name: '${pe.peName}-zonegroup-deploy'
  scope: resourceGroup(pe.peSubId, pe.peRG)
  params: {
    endpointName: pe.peName
    dnsZoneName: privateDNSZone[pe.indexValue].outputs.privateDnsZoneName
    dnsZoneId: privateDNSZone[pe.indexValue].outputs.privateDnsZoneID
  }
}]

// Link endpoint to resource and subnet
module privateDNSLink 'virtualnetworklink.bicep' = [for dz in dnsZones: /*if (!(empty(dnsZones))) */{
  dependsOn: [privateDnsZoneGroup]
  name:  '${dz.dnsName}-dnslink'
  scope: resourceGroup(dz.dnsSubId, dz.dnsRG)
  params: {
    name: '${dz.dnsName}/${vnetExisting[dz.indexValue].name}-link'
    tags: tags
    vnetId: vnetExisting[dz.indexValue].id
  }
}]

Please note that the code includes placeholders such as ${} that are typically used for variable interpolation. Ensure that you replace these placeholders with actual values or variables in your code.

英文:

I am trying to create a Private Endpoint, DNS and the DNS Zone Group and have all the code as a "one stop shop" and re-usable for other resources - Key Vault, Storage etc. In certain situations, a policy will create the DNS Zone Group, so therefore that functionality is not required, otherwise the deployment will error due to trying to provision more than one zone.

I am trying to capture an empty array for DNS but I get the out of bounds error.

My code:

@description('Array: DNZ Zones')
param dnsZones array
@description('Array: Private Endpoints')
param privateEndPoints array

@description('Object: Tags')
param tags object

@description('String: Fully qualified ID of the resource in question')
param resourceId string




// Obtain existing Vnet
resource vnetExisting 'Microsoft.Network/virtualNetworks@2021-02-01' existing = [for vn in privateEndPoints: {
  name: vn.virtualNetworkName
  scope: resourceGroup(vn.virtualNetworkSubId, vn.virtualNetworkRG)
}]

// Obtain Resource ID for VNet/SubNet
resource subnetExisting 'Microsoft.Network/virtualnetworks/subnets@2021-02-01' existing = [for sn in privateEndPoints: {
  name: '${sn.virtualNetworkName}/${sn.subnetName}'
  scope: resourceGroup(sn.virtualNetworkSubId, sn.virtualNetworkRG)
}]

module privateEndPoint 'privateendpoint.bicep' = [for pe in privateEndPoints: if (!(empty(privateEndPoints))) {
  name: '${pe.peName}-privateendpoint-deploy'
  scope: resourceGroup(pe.peSubId, pe.peRG)
  params: {
    name: pe.peName
    serviceId: resourceId
    subnetId: subnetExisting[pe.indexValue].id
    resource: pe.resource
    tags: tags
  }
}]

module privateDNSZone 'privatednszone.bicep' = [for dz in dnsZones: /*if (!(empty(dnsZones)))*/ {
  dependsOn: [privateEndPoint]
  name: '${dz.dnsName}-dnszone-deploy'
  scope: resourceGroup(dz.dnsSubId, dz.dnsRG)
  params: {
    name: dz.dnsName
    tags: tags
  }
}]


// Link endpoint to dns
module privateDnsZoneGroup 'privatednszonegroup.bicep' = [for pe in privateEndPoints: /*if (!(empty(privateEndPoints)) && !(empty(dnsZones)))*/ {
  dependsOn: [privateDNSZone]
  name: '${pe.peName}-zonegroup-deploy'
  scope: resourceGroup(pe.peSubId, pe.peRG)
  params: {
    endpointName: pe.peName
    dnsZoneName: privateDNSZone[pe.indexValue].outputs.privateDnsZoneName
    dnsZoneId: privateDNSZone[pe.indexValue].outputs.privateDnsZoneID
  }
}]

// Link endpoint to resource and subnet
module privateDNSLink 'virtualnetworklink.bicep' = [for dz in dnsZones: /*if (!(empty(dnsZones))) */{
  dependsOn: [privateDnsZoneGroup]
  name:  '${dz.dnsName}-dnslink'
  scope: resourceGroup(dz.dnsSubId, dz.dnsRG)
  params: {
    name: '${dz.dnsName}/${vnetExisting[dz.indexValue].name}-link'
    tags: tags
    vnetId: vnetExisting[dz.indexValue].id
  }
}]

Which fails on name: '${pe.peName}-zonegroup-deploy'

Json that works:

			"privateEndPoints": [
				{
					"peName": "my-pe",
					"peRG": "my-rg",	
					"peSubId": "123",							
					"resource": "namespace",							
					"virtualNetworkName": "my-vnet01",
					"virtualNetworkRG": "my-vnet01-rg",
					"virtualNetworkSubId": "123",							
					"subnetName": "my-sn",
					"indexValue": 0
				}
			],
			"dnsZones": [
				{
					"dnsName": "my-evhns.servicebus.windows.net",
					"dnsRG": "ev-rg",
					"dnsSubId": "123",	
					"indexValue": 0
				}
			],

Json that fails:

			"privateEndPoints": [
				{
					"peName": "my-pe",
					"peRG": "my-rg",	
					"peSubId": "123",							
					"resource": "namespace",							
					"virtualNetworkName": "my-vnet01",
					"virtualNetworkRG": "my-vnet01-rg",
					"virtualNetworkSubId": "123",							
					"subnetName": "my-sn",
					"indexValue": 0
				}
			],
			"dnsZones": [
			],

NB. In the json I have tried both checks on the objects using if and also with them commented out. Both yield the same result

答案1

得分: 1

我认为这是问题:

name: {dz.dnsName}

您正在引用数组中为空的dnsName。您可以尝试在资源的名称中使用随机字符串吗?

英文:

I think this is the problem:

name: {dz.dnsName}

You are referring to the dnsName in the array which is empty. Can you try with a random string in the name of the resource?

huangapple
  • 本文由 发表于 2023年5月26日 14:07:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定