Lesson 2 of 3 Compute Targets for Machine Learning

Compute Instances for Development

This lesson explains what a compute instance provides, how idle shutdown and schedules control cost, how instances are secured, and why production work runs as jobs.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the ownership and storage of a compute instance.
  • Explain idle shutdown and schedules.
  • Identify settings that reduce an instance's attack surface.
  • Distinguish work suited to instances from work suited to jobs.

What a compute instance provides

A compute instance is a managed VM for one person's development work. It comes with Python, the Azure Machine Learning SDK, MLflow, common frameworks, Jupyter and JupyterLab, and it can be used from the studio's notebook editor or from VS Code on the desktop through a remote connection. Files in the user's folder are stored in the workspace's file share rather than on the VM disk, so they survive stopping, restarting and even deleting the instance.

A compute instance is assigned to a single user. Only that user can run notebooks or open terminals on it, and when it is created on behalf of another user, the creator chooses who it belongs to. Notebook code that calls Azure services can authenticate as the assigned user, which keeps permissions personal and auditable.

$schema: https://azuremlschemas.azureedge.net/latest/computeInstance.schema.json
name: ci-amira
type: computeinstance
size: Standard_DS3_v2
idle_time_before_shutdown_minutes: 30

Controlling cost

A compute instance bills for its VM whenever it runs, whether or not anyone uses it. Two features stop it when it is not needed, and both should be configured for every instance.

  • Idle shutdown stops the instance after a period with no activity, such as no running kernels, terminals or remote connections. It is set with idle_time_before_shutdown_minutes or in the studio.
  • Schedules start and stop the instance at fixed times, such as starting at 08:00 and stopping at 19:00 on weekdays, using cron expressions or recurrence patterns.

A stopped instance still incurs small storage charges for its disk, but no VM charges. Azure Policy can require idle shutdown on all compute instances in a subscription, which prevents forgotten instances from running all weekend.

Customizing and securing instances

Teams often need the same extra packages or settings on every instance. A setup script runs when the instance is created, and a startup script can run on each start, installing packages or cloning repositories. Custom applications, such as RStudio or another container image, can be added at creation. For larger differences, a job with its own environment is often better than a heavily customized instance, because the environment is versioned and reproducible.

Security settings include disabling SSH access unless it is needed, placing the instance in a managed or customer virtual network with no public IP address, and assigning it a managed identity for jobs and code that should not use the user's identity. Instances should also be restarted or recreated regularly to receive operating system and package updates, because they are not patched while they run.

Instances and jobs

A compute instance can run jobs, which is convenient for testing a job definition quickly. Production training should not depend on it, however: an instance has one node, belongs to one person, and may be stopped by idle shutdown. The usual pattern is to explore and debug in a notebook on the instance, move the code into a script, and submit it as a job to a cluster or serverless compute, often from the same notebook.

from azure.ai.ml import command, Input

job = command(
    code="./src",
    command="python train.py --data ${{inputs.data}}",
    inputs={"data": Input(type="uri_file", path="azureml:churn-data:2")},
    environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
    compute="cpu-cluster",
)
ml_client.jobs.create_or_update(job)
Worked example

Stopping forgotten instances

The monthly bill shows that 12 compute instances ran continuously, although the data scientists work only on weekdays during office hours. Some data scientists say they need long-running notebooks overnight occasionally. The team wants to reduce cost with minimal effort and without breaking those overnight runs.

Configure idle shutdown on every instance, for example after 60 minutes, so instances stop when nobody is working on them but stay on while a notebook kernel is active. Add a schedule that starts instances on weekday mornings for convenience, and use Azure Policy to require idle shutdown on new instances. Long overnight work should move to jobs on a cluster, which do not depend on an instance staying on.

A schedule that stops every instance at 19:00 would also stop the legitimate overnight notebooks. Deleting idle instances each week would lose their configuration and inconvenience users. Replacing instances with a shared cluster would not provide interactive notebooks, because clusters run jobs.

Check yourself

Which setting stops a compute instance automatically when a data scientist forgets to shut it down, and what still incurs cost while it is stopped?

Show answer

Idle shutdown stops it after a period of inactivity, and schedules can stop it at set times. While stopped, compute is not billed, but its disk and the workspace storage still are.

Exam Tips

Remember for the exam

  • A compute instance belongs to one user; files live in the workspace file share.
  • Idle shutdown and schedules stop instances when they are not needed.
  • Disable SSH and remove the public IP address unless required.
  • Explore on an instance; run production training as jobs on clusters or serverless compute.