简体   繁体   English

资源未在模板中定义 - Azure ARM

[英]The resource is not defined in the template - Azure ARM

I am trying to create a deployment template that creates a resource group and a VM with the necessary resources within it.我正在尝试创建一个部署模板,该模板创建一个资源组和一个包含必要资源的 VM。 The resources in their own deploymentTemplate deploy just fine.自己的deploymentTemplate中的资源部署就好了。 But in the subscriptionDeploymentTemplate I get the following error (in validation):但在 subscriptionDeploymentTemplate 我收到以下错误(验证中):

{
  "code": "InvalidTemplate",
  "message": "Deployment template validation failed: 'The resource 'Microsoft.Network/networkInterfaces/Assessment-NetInterfacescrobp34x4564' is not defined in the template. Please see https://aka.ms/arm-template for usage details.'."
}

After a lot of googling I have not found a solution that works in my case.经过大量谷歌搜索后,我还没有找到适合我的解决方案。 I would appreciate any help.我将不胜感激任何帮助。

The template:模板:

    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "rgName": {
            "type": "string"
        }
    },
    "variables": {
        "rgLocation": "westeurope",
        "vm_id": "[concat('AssessmentVM', uniquestring(deployment().name))]",
        "location": "westeurope",
        "vnet_id": "[concat('Assessment-Vnet', uniquestring(deployment().name))]",
        "nic_id": "[concat('Assessment-NetInterface', uniquestring(deployment().name))]",
        "publicIP_id": "[concat('AssessmentVM-ip', uniquestring(deployment().name))]",
        "nsg_id": "[concat('AssessmentVM-nsg', uniquestring(deployment().name))]",
        "vmImage_id": "/subscriptions/x/resourceGroups/AssessmentCase_Snapshot/providers/Microsoft.Compute/galleries/AssessmentVM_Images/images/AssessmentVM/versions/0.0.1"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2021-04-01",
            "name": "[parameters('rgName')]",
            "location": "[variables('rgLocation')]",
            "properties": {}
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "assessmentDeployment",
            "resourceGroup": "[parameters('rgName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nsg_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "securityRules": [
                                    {
                                        "name": "RDP",
                                        "properties": {
                                            "protocol": "TCP",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "3389",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "*",
                                            "access": "Allow",
                                            "priority": 300,
                                            "direction": "Inbound",
                                            "sourcePortRanges": [],
                                            "destinationPortRanges": [],
                                            "sourceAddressPrefixes": [],
                                            "destinationAddressPrefixes": []
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "type": "Microsoft.Network/publicIPAddresses",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('publicIP_id')]",
                            "location": "[variables('location')]",
                            "sku": {
                                "name": "Basic"
                            },
                            "properties": {
                                "publicIPAddressVersion": "IPv4",
                                "publicIPAllocationMethod": "Dynamic",
                                "idleTimeoutInMinutes": 4,
                                "ipTags": []
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('vnet_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "10.1.4.0/24"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "default",
                                        "properties": {
                                            "addressPrefix": "10.1.4.0/24",
                                            "delegations": [],
                                            "privateEndpointNetworkPolicies": "Enabled",
                                            "privateLinkServiceNetworkPolicies": "Enabled"
                                        }
                                    }
                                ],
                                "virtualNetworkPeerings": [],
                                "enableDdosProtection": false
                            }
                        },
                        {
                            "type": "Microsoft.Compute/virtualMachines",
                            "apiVersion": "2019-07-01",
                            "name": "[variables('vm_id')]",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                            ],
                            "properties": {
                                "hardwareProfile": {
                                    "vmSize": "Standard_D4s_v3"
                                },
                                "storageProfile": {
                                    "imageReference": {
                                        "id": "[variables('vmImage_id')]"
                                    },
                                    "osDisk": {
                                        "createOption": "FromImage"
                                    }
                                },
                                "networkProfile": {
                                    "networkInterfaces": [
                                        {
                                            "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                                        }
                                    ]
                                },
                                "diagnosticsProfile": {
                                    "bootDiagnostics": {
                                        "enabled": false
                                    }
                                },
                                "licenseType": "Windows_Client"
                            }
                        },
                        {
                            "type": "Microsoft.Network/networkSecurityGroups/securityRules",
                            "apiVersion": "2020-05-01",
                            "name": "[concat(variables('nsg_id'), '/RDP')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                            ],
                            "properties": {
                                "protocol": "TCP",
                                "sourcePortRange": "*",
                                "destinationPortRange": "3389",
                                "sourceAddressPrefix": "*",
                                "destinationAddressPrefix": "*",
                                "access": "Allow",
                                "priority": 300,
                                "direction": "Inbound",
                                "sourcePortRanges": [],
                                "destinationPortRanges": [],
                                "sourceAddressPrefixes": [],
                                "destinationAddressPrefixes": []
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks/subnets",
                            "apiVersion": "2020-05-01",
                            "name": "[concat(variables('vnet_id'), '/default')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnet_id'))]"
                            ],
                            "properties": {
                                "addressPrefix": "10.1.4.0/24",
                                "delegations": [],
                                "privateEndpointNetworkPolicies": "Enabled",
                                "privateLinkServiceNetworkPolicies": "Enabled"
                            }
                        },
                        {
                            "type": "Microsoft.Network/networkInterfaces",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nic_id')]",
                            "location": "[variables('location')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]",
                                "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]",
                                "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                            ],
                            "properties": {
                                "ipConfigurations": [
                                    {
                                        "name": "ipconfig1",
                                        "properties": {
                                            "privateIPAddress": "10.1.4.4",
                                            "privateIPAllocationMethod": "Dynamic",
                                            "publicIPAddress": {
                                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]"
                                            },
                                            "subnet": {
                                                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]"
                                            },
                                            "primary": true,
                                            "privateIPAddressVersion": "IPv4"
                                        }
                                    }
                                ],
                                "dnsSettings": {
                                    "dnsServers": []
                                },
                                "enableAcceleratedNetworking": false,
                                "enableIPForwarding": false,
                                "networkSecurityGroup": {
                                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                                }
                            }
                        }
                    ]
                }
            }
        }
    ],
    "outputs": {}
}

Please find the below template which I have modified -请找到我已修改的以下模板 -

  1. There were few resources which were same but you were creating them 2 times, so modified that.几乎没有相同的资源,但您创建了 2 次,因此对其进行了修改。
  2. use of dependsOn - This error usually comes from having a dependency on a resource and we are not using dependsOn correctly, so modified the order of the resources that you were creating.使用dependsOn - 此错误通常来自对资源的依赖,并且我们没有正确使用dependsOn,因此修改了您创建的资源的顺序。
{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "rgName": {
            "type": "string"
        }
    },
    "variables": {
        "rgLocation": "westeurope",
        "vm_id": "[concat('AssessmentVM', uniquestring(deployment().name))]",
        "location": "westeurope",
        "vnet_id": "[concat('Assessment-Vnet', uniquestring(deployment().name))]",
        "nic_id": "[concat('Assessment-NetInterface', uniquestring(deployment().name))]",
        "publicIP_id": "[concat('AssessmentVM-ip', uniquestring(deployment().name))]",
        "nsg_id": "[concat('AssessmentVM-nsg', uniquestring(deployment().name))]",
        "vmImage_id": "/subscriptions/x/resourceGroups/AssessmentCase_Snapshot/providers/Microsoft.Compute/galleries/AssessmentVM_Images/images/AssessmentVM/versions/0.0.1"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2021-04-01",
            "name": "[parameters('rgName')]",
            "location": "[variables('rgLocation')]",
            "properties": {}
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "assessmentDeployment",
            "resourceGroup": "[parameters('rgName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "variables": {},
                    "resources": [
                        {
                            "apiVersion": "2020-05-01",
                            "type": "Microsoft.Network/publicIPAddresses",
                            "name": "[variables('publicIP_id')]",
                            "location": "[parameters('location')]",
                            "properties": {
                                "publicIPAddressVersion": "IPv4",
                                "publicIPAllocationMethod": "Dynamic",
                                "idleTimeoutInMinutes": 4,
                                "ipTags": []
                            }
                        },
                        {
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nsg_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "securityRules": [
                                    {
                                        "name": "RDP",
                                        "properties": {
                                            "protocol": "TCP",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "3389",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "*",
                                            "access": "Allow",
                                            "priority": 300,
                                            "direction": "Inbound",
                                            "sourcePortRanges": [],
                                            "destinationPortRanges": [],
                                            "sourceAddressPrefixes": [],
                                            "destinationAddressPrefixes": []
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('vnet_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "10.1.4.0/24"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "default",
                                        "properties": {
                                            "addressPrefix": "10.1.4.0/24",
                                            "delegations": [],
                                            "privateEndpointNetworkPolicies": "Enabled",
                                            "privateLinkServiceNetworkPolicies": "Enabled"
                                        }
                                    }
                                ],
                                "virtualNetworkPeerings": [],
                                "enableDdosProtection": false
                            }
                        },
                        {
                            "type": "Microsoft.Network/networkInterfaces",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nic_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "ipConfigurations": [
                                    {
                                        "name": "ipconfig1",
                                        "properties": {
                                            "privateIPAddress": "10.1.4.4",
                                            "privateIPAllocationMethod": "Dynamic",
                                            "publicIPAddress": {
                                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]"
                                            },
                                            "subnet": {
                                                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]"
                                            },
                                            "primary": true,
                                            "privateIPAddressVersion": "IPv4"
                                        }
                                    }
                                ],
                                "dnsSettings": {
                                    "dnsServers": []
                                },
                                "enableAcceleratedNetworking": false,
                                "enableIPForwarding": false,
                                "networkSecurityGroup": {
                                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                                }
                            },
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]",
                                "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]",
                                "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                            ]
                        },
                        {
                            "type": "Microsoft.Compute/virtualMachines",
                            "apiVersion": "2019-07-01",
                            "name": "[variables('vm_id')]",
                            "location": "[variables('location')]",
                            "properties": {
                                "hardwareProfile": {
                                    "vmSize": "Standard_D4s_v3"
                                },
                                "storageProfile": {
                                    "imageReference": {
                                        "id": "[variables('vmImage_id')]"
                                    },
                                    "osDisk": {
                                        "createOption": "FromImage"
                                    }
                                },
                                "networkProfile": {
                                    "networkInterfaces": [
                                        {
                                            "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                                        }
                                    ]
                                },
                                "diagnosticsProfile": {
                                    "bootDiagnostics": {
                                        "enabled": false
                                    }
                                },
                                "licenseType": "Windows_Client"
                            },
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                            ]
                        }   
                    ]
                }
            }
        }
    ],
    "outputs": {}
}

The problem was the resourceId function which was in the wrong scope and therefore need to be additionally supplied with the subscription id and resource group name to return the correct ids.问题是在错误范围内的 resourceId 函数,因此需要另外提供订阅 ID 和资源组名称以返回正确的 ID。 This template is now working as expected此模板现在按预期工作

{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "rg_name": {
            "type": "string"
        },
        "location": {
            "type": "string",
            "defaultValue": "westeurope"
        }
    },
    "variables": {
        "vm_id": "[concat('AssessmentVM', uniquestring(deployment().name))]",
        "vnet_id": "[concat('Assessment-Vnet', uniquestring(deployment().name))]",
        "nic_id": "[concat('Assessment-NetInterface', uniquestring(deployment().name))]",
        "publicIP_id": "[concat('AssessmentVM-ip', uniquestring(deployment().name))]",
        "nsg_id": "[concat('AssessmentVM-nsg', uniquestring(deployment().name))]",
        "subscription_id": "x",
        "vmImage_id": "/subscriptions/x/resourceGroups/AssessmentCase_Snapshot/providers/Microsoft.Compute/galleries/AssessmentVM_Images/images/AssessmentVM/versions/0.0.1"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2021-04-01",
            "name": "[parameters('rg_name')]",
            "location": "[parameters('location')]",
            "properties": {}
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "assessment_vm_deployment",
            "resourceGroup": "[parameters('rg_name')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rg_name'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nsg_id')]",
                            "location": "[parameters('location')]",
                            "properties": {
                                "securityRules": [
                                    {
                                        "name": "RDP",
                                        "properties": {
                                            "protocol": "TCP",
                                            "sourcePortRange": "*",
                                            "destinationPortRange": "3389",
                                            "sourceAddressPrefix": "*",
                                            "destinationAddressPrefix": "*",
                                            "access": "Allow",
                                            "priority": 300,
                                            "direction": "Inbound",
                                            "sourcePortRanges": [],
                                            "destinationPortRanges": [],
                                            "sourceAddressPrefixes": [],
                                            "destinationAddressPrefixes": []
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "type": "Microsoft.Network/publicIPAddresses",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('publicIP_id')]",
                            "location": "[parameters('location')]",
                            "sku": {
                                "name": "Basic"
                            },
                            "properties": {
                                "publicIPAddressVersion": "IPv4",
                                "publicIPAllocationMethod": "Dynamic",
                                "idleTimeoutInMinutes": 4,
                                "ipTags": []
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('vnet_id')]",
                            "location": "[parameters('location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "10.1.4.0/24"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "default",
                                        "properties": {
                                            "addressPrefix": "10.1.4.0/24",
                                            "delegations": [],
                                            "privateEndpointNetworkPolicies": "Enabled",
                                            "privateLinkServiceNetworkPolicies": "Enabled"
                                        }
                                    }
                                ],
                                "virtualNetworkPeerings": [],
                                "enableDdosProtection": false
                            }
                        },
                        {
                            "type": "Microsoft.Compute/virtualMachines",
                            "apiVersion": "2019-07-01",
                            "name": "[variables('vm_id')]",
                            "location": "[parameters('location')]",
                            "dependsOn": [
                                "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                            ],
                            "properties": {
                                "hardwareProfile": {
                                    "vmSize": "Standard_D4s_v3"
                                },
                                "storageProfile": {
                                    "imageReference": {
                                        "id": "[variables('vmImage_id')]"
                                    },
                                    "osDisk": {
                                        "createOption": "FromImage"
                                    }
                                },
                                "networkProfile": {
                                    "networkInterfaces": [
                                        {
                                            "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkInterfaces', variables('nic_id'))]"
                                        }
                                    ]
                                },
                                "diagnosticsProfile": {
                                    "bootDiagnostics": {
                                        "enabled": false
                                    }
                                },
                                "licenseType": "Windows_Client"
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks/subnets",
                            "apiVersion": "2020-05-01",
                            "name": "[concat(variables('vnet_id'), '/default')]",
                            "dependsOn": [
                                "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/virtualNetworks', variables('vnet_id'))]"
                            ],
                            "properties": {
                                "addressPrefix": "10.1.4.0/24",
                                "delegations": [],
                                "privateEndpointNetworkPolicies": "Enabled",
                                "privateLinkServiceNetworkPolicies": "Enabled"
                            }
                        },
                        {
                            "type": "Microsoft.Network/networkInterfaces",
                            "apiVersion": "2020-05-01",
                            "name": "[variables('nic_id')]",
                            "location": "[parameters('location')]",
                            "dependsOn": [
                                "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]",
                                "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]",
                                "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                            ],
                            "properties": {
                                "ipConfigurations": [
                                    {
                                        "name": "ipconfig1",
                                        "properties": {
                                            "privateIPAddress": "10.1.4.4",
                                            "privateIPAllocationMethod": "Dynamic",
                                            "publicIPAddress": {
                                                "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/publicIPAddresses', variables('publicIP_id'))]"
                                            },
                                            "subnet": {
                                                "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/virtualNetworks/subnets', variables('vnet_id'), 'default')]"
                                            },
                                            "primary": true,
                                            "privateIPAddressVersion": "IPv4"
                                        }
                                    }
                                ],
                                "dnsSettings": {
                                    "dnsServers": []
                                },
                                "enableAcceleratedNetworking": false,
                                "enableIPForwarding": false,
                                "networkSecurityGroup": {
                                    "id": "[resourceId(variables('subscription_id'), parameters('rg_name'), 'Microsoft.Network/networkSecurityGroups', variables('nsg_id'))]"
                                }
                            }
                        }
                    ]
                }
            }
        }
    ],
    "outputs": {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM