Create and use a compute instance

Completed

When you want to execute code in notebooks, you can choose to use a compute instance managed by Azure Machine Learning. You can create a compute instance in the Azure Machine Learning studio, using the Azure command-line interface (CLI), or the Python software development kit (SDK).

Create a compute instance with the Python SDK

To create a compute instance with the Python SDK, you can use the following code:

from azure.ai.ml.entities import ComputeInstance

ci_basic_name = "basic-ci-12345"
ci_basic = ComputeInstance(
    name=ci_basic_name, 
    size="STANDARD_DS3_v2"
)
ml_client.begin_create_or_update(ci_basic).result()

To understand which parameters the ComputeInstance class expects, you can review the reference documentation.

Note

Compute instances need to have a unique name across an Azure region (for example within west europe). If the name already exists, an error message will tell you to try again with another name.

Alternatively, you can also create a compute instance by using a script. With a script, you ensure that any necessary packages, tools, or software is automatically installed on the compute and you can clone any repositories to the compute instance. When you need to create compute instances for multiple users, using a script allows you to create a consistent development environment for everyone.

Assign a compute instance to a user

As a data scientist, you can attach a compute instance to notebooks to run cells within the notebook. To be allowed to work with the compute instance, it needs to be assigned to you as a user.

A compute instance can only be assigned to one user, as the compute instance can't handle parallel workloads. When you create a new compute instance, you can assign it to someone else if you have the appropriate permissions.

Minimize compute time

When you're actively working on code in a notebook, you want your compute instance to be running. When you're not executing any code, you want your compute instance to be stopped to save on costs.

When a compute instance is assigned to you, you can start and stop a compute instance whenever you need. You can also add a schedule to the compute instance to start or stop at set times. Additionally, you can configure a compute to automatically shut down when it has been idle for a set amount of time.

By scheduling your compute instance to stop at the end of every day, you avoid unnecessary costs if you forget to stop a compute instance.

Use a compute instance

To use a compute instance, you need an application that can host notebooks. The easiest option to work with the compute instance is through the integrated notebooks experience in the Azure Machine Learning studio.

You can prefer to work with Visual Studio Code for easier source control of your code. If you want to edit and run code in Visual Studio Code, you can attach a compute instance to run notebook cells remotely.