pythonjsonazureazure-rm-templateazure-sdk

Azure SDK ARM Template deployment: Could not find member 'id'


I'm trying to deploy a vm through the python azure sdk with an arm template. I'm using the code provided by microsoft from here: https://learn.microsoft.com/en-us/samples/azure-samples/resource-manager-python-template-deployment/resource-manager-python-template-deployment/

But I get an error when trying to use the template.

parameters = my parameters as a python dict
       
parameters = {k: {'value': v} for k, v in parameters.items()}
template = self.ts_client.template_specs.get('test-rg', 'deploy-vm.test').as_dict()

deployment_properties = {'mode': DeploymentMode.incremental,
                       'template': template,
                       'parameters': parameters}
    
self.client.deployments.create_or_update(self.resource_group,'azure-sample', {'properties': deployment_properties, 'tags': []})

The only part thats different from the example code, is that I'm not reading the template from a file but I'm getting it through the sdk and converting it into a dictonary and I pass the deployment_properties into the begin_create_or_update method as a dict. If I don't pass it like this it gives the exception: Parameter 'Deployment.properties' can not be None.

However I get this error:

azure.core.exceptions.HttpResponseError: (InvalidRequestContent) The request content was invalid and could not be deserialized: 'Could not find member 'id' on object of type 'Template'. Path 'properties.template.id', line 1, position 34.'.

Any idea what this could be?


Solution

  • I tried in my environment and got same type of error.

    Console: enter image description here

    Make sure you are passing correct Arm template template.json and also check it is in correct state. Provide the valid id or correct the templates according to Azure-VM templates using this MS-Docs.

    After I validated my templates using document the virtual machine is deployed successfully using python with Arm templates.

    Code:

    from  azure.mgmt.resource  import  ResourceManagementClient
    from  azure.mgmt.resource.resources.models  import  DeploymentMode
    from  azure.identity  import  DefaultAzureCredential
    import  json
    
    
    subscription_id = '<subscription id>'
    resource_group = '<your rg name >'
    creds = DefaultAzureCredential()
    client = ResourceManagementClient( creds, '<sub id>')
    
    parameters = {
    'virtualMachineName': '',
    'location': '',
    'virtualMachineRG':''
    }
    
    parameters = {k: {'value': v} for  k, v  in  parameters.items()}
    with  open(r'<path of file >', 'r') as  template_file_fd:
    template = json.load(template_file_fd)
    deployment_properties = {
    'mode': DeploymentMode.incremental,
    'template': template,
    'parameters': parameters
    }
    deployment_async_operation = client.deployments.begin_create_or_update(
    resource_group, 'azure-sample', {'properties': deployment_properties, 'tags': []})
    deployment_async_operation.wait()
    

    Console:

    enter image description here

    Portal:

    enter image description here

    Reference: Microsoft.Compute/virtualMachines - Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn