Error when trying to create a cluster Availability Test using ARM Template

102 Views Asked by At

I'm trying to develop a feature in my app that creates an Availability Test and I'm running a lab that using Python SDK I'm able to create a resource group, AKS cluster, log analytics workspace and application insights. After it does all that, this comes in, trying to create an Availability Test.

availability_test = {
    "$schema": "ttps://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "apiVersion": "2019-04-01",
            "name": app_insights_name,
            "type": "microsoft.insights/webtests",
            "location": "eastus2",
            "properties": {
                "SyntheticMonitorId": None,
                "Enabled": True,
                "Locations": [
                    {
                        "Id": "us-ca-sjc-azr"
                    }
                ],
                "Configuration": {
                    "WebTest": {
                        "Timeout": "60",
                        "ParseDependentRequests": False,
                        "FollowRedirects": True
                    }
                }
            }
        }
    ]
}

deployment_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourcegroups/{resource_group_name}/providers/microsoft.resources/deployments/{app_insights_name}?api-version=2023-07-01"
headers = {
    "Authorization": f"Bearer {credentials.get_token('https://management.azure.com/.default').token}",
    "Content-Type": "application/json"
}

response = requests.put(deployment_url, data=json.dumps(availability_test), headers=headers)

if response.status_code == 201:
    print("Availability test deployed successfully!")
else:
    print(f"Failed to deploy availability test. Status code: {response.status_code}")
    print(response.text)

The error that persists is this one:

Failed to deploy availability test. Status code: 400
{"error":{"code":"InvalidRequestContent","message":"The request content was invalid and could not be deserialized: 'Could not find member '$schema' on object of type 'DeploymentContent'. Path '$schema', line 1, position 11.'."}}

Any ideas? Or maybe another method of doing this?

1

There are 1 best solutions below

4
Suresh Chikkam On

{"error":{"code":"InvalidRequestContent","message":"The request content was invalid and could not be deserialized: 'Could not find member '$schema' on object of type 'DeploymentContent'. Path $schema', line 1, position 11.'."}}

The Azure API is not recognizing the $schema field within the deployment content. In this context, the $schema field is not expected, and the ARM deployment should only contain the resource definitions.

It's better to keep your ARM templates in separate files, as this makes your code cleaner and easier to manage.

Deploy ARM Template with py script:

from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
from azure.mgmt.monitor.models import (
    WebTestKind,
    SyntheticMonitorConfiguration,
    WebTestProperties,
    WebTestLocation,
    WebTestGeolocation,
    WebTestPropertiesFrequency,
    WebTestPropertiesRetryPolicy,
)

# Replace these with your actual values
subscription_id = "your-subscription-id"
resource_group_name = "your-resource-group"
app_insights_name = "your-app-insights-name"

# Initialize your Azure credentials
credential = DefaultAzureCredential()

# Initialize the monitor management client
monitor_client = MonitorManagementClient(credential, subscription_id)

# Define the properties for the Availability Test
web_test_location = WebTestLocation(
    location=WebTestGeolocation(location_id="us-ca-sjc-azr")
)

web_test_configuration = SyntheticMonitorConfiguration(
    frequency=WebTestPropertiesFrequency(time_interval="PT5M"),
    retries=3,
    locations=[web_test_location],
)

web_test_properties = WebTestProperties(
    enabled=True,
    frequency=300,
    timeout=60,
    kind=WebTestKind.multi_step,
    locations=[web_test_location],
    configurations=[web_test_configuration],
)

# Create the Availability Test
availability_test = monitor_client.web_tests.create_or_update(
    resource_group_name, app_insights_name, app_insights_name, web_test_properties
)

print("Availability test deployed successfully!")
  • ARM template is loaded from a separate file, and the parameter values are provided dynamically when constructing the deployment URL. After successful deployment of the availability test succeeds you can see the below results.

enter image description here