Skip to main content

Enforcing Init Scripts on Azure Machine Learning Compute Instances

·2 mins

Introduction #

This post is a quick tip, showing how you can automatically enforce an Init Script during Azure Machine Learning Compute Instance provisioning. This ensures that even when the user did not specify a script, a default script is always being executed.

For more details on the init script capabilities, have a look at the documentation.

Instructions #

Firstly, create your desired init script – in our example, we’ll just use a simple example:

#!/bin/bash

echo "Hello World"

Then base64-encode the script and replace the value in line 26 with your encoded script:

{
    "mode": "All",
    "policyRule": {
        "if": {
            "allOf": [
                {
                    "field": "type",
                    "equals": "Microsoft.MachineLearningServices/workspaces/computes"
                },
                {
                    "field": "Microsoft.MachineLearningServices/workspaces/computes/computeType",
                    "in": [
                        "ComputeInstance"
                    ]
                }
            ]
        },
        "then": {
            "effect": "append",
            "details": [{
                    "field": "Microsoft.MachineLearningServices/workspaces/computes/setupScripts.scripts.creationScript.scriptSource",
                    "value": "inline"
                },
                {
                    "field": "Microsoft.MachineLearningServices/workspaces/computes/setupScripts.scripts.creationScript.scriptData",
                    "value": "IyEvYmluL2Jhc2gKCmVjaG8gIkhlbGxvIFdvcmxkIg=="
                }
            ]
        }
    }
}

Next, create a new Policy definition in Azure Policy:

New Policy definition
Create a new Policy definition

You’ll need to define in which subscription the definition should live in, give it a name and a description and then finally paste the policy JSON into the Policy Rule. Then hit Save.

Our Policy definition
Our Policy definition

Now that we have a Policy, we can create an assignment. This will apply the policy to Azure resources. Inside Azure Policy, navigate to Assignments and select Assign policy.

Creating our assignment
Creating our assignment

Then define the scope of the assignment (which subscriptions it should affect), select our Policy definition and hit Create.

Policy assignment
Asssigning our policy

That’s it, our policy is live!

Making sure it works #

Lastly, go to Azure Machine Learning Studio and provision a new Compute Instance without selecting an init script. Once the instance has been provisioned, open the instance details and you should see the stdout under the Logs tab.

Our init script successfully executed
Our init script successfully executed

Hope this helps!

Happy instance creating!