英文:
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论