適用対象:
Azure CLI ml 拡張機能 v2 (現行)
Python SDK azure-ai-ml v2 (現行)
この記事では、Azure の外部にあるデータ ソースに接続して、そのデータを Azure Machine Learning サービスで使用できるようにする方法について学習します。 Azure 接続はキー コンテナーのプロキシとして機能し、接続とのやり取りは、Azure キー コンテナーとの直接のやり取りになります。 Azure Machine Learning 接続では、ユーザー名とパスワードのデータ リソースが、シークレットとしてキー コンテナーに安全に格納されます。 キー コンテナー RBAC では、これらのデータ リソースへのアクセスが制御されます。 Azure では、このデータの可用性のために、次の外部ソースへの接続がサポートされています。
- Snowflake DB(スノーフレーク DB)
- Amazon S3
- Azure SQL DB
重要
現在、この機能はパブリック プレビュー段階にあります。 このプレビュー バージョンはサービス レベル アグリーメントなしで提供されており、運用環境のワークロードに使用することは推奨されません。 特定の機能はサポート対象ではなく、機能が制限されることがあります。
詳しくは、「Microsoft Azure プレビューの追加使用条件」をご覧ください。
前提条件
重要
Azure Machine Learning 接続では、接続の作成時に渡される資格情報をワークスペース Azure Key Vault に安全に保存します。 接続は、その後の使用のためにキー コンテナーの保存場所から資格情報を参照します。 資格情報は、キー コンテナーに格納された後は直接処理する必要はありません。 資格情報を YAML ファイルに保存するオプションがあります。 CLI コマンドまたは SDK は、それらをオーバーライドできます。 セキュリティ侵害があると資格情報が漏洩する可能性があるため、YAML ファイルに資格情報を保存することを回避することをお勧めします。
注
データを正常にインポートするには、SDK 用の最新の azure-ai-ml パッケージ (バージョン 1.5.0 以降) と ml 拡張機能 (バージョン 2.15.1 以降) がインストールされていることを確認してください。
以前の SDK パッケージまたは CLI 拡張機能がある場合は、以前のものを削除し、タブ セクションに表示されているコードを使用して新しいものをインストールしてください。 次に示す SDK および CLI 向けの手順に従ってください。
コードのバージョン
az extension remove -n ml
az extension add -n ml --yes
az extension show -n ml #(the version value needs to be 2.15.1 or later)
pip uninstall azure-ai-ml
pip install azure-ai-ml
pip show azure-ai-ml #(the version value needs to be 1.5.0 or later)
Snowflake DB 接続を作成する
この YAML ファイルでは、Snowflake DB 接続を作成します。 必ず、該当する値を更新してください。
# my_snowflakedb_connection.yaml
$schema: http://azureml/sdk-2-0/Connection.json
type: snowflake
name: my-sf-db-connection # add your datastore name here
target: jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&role=<myrole>
# add the Snowflake account, database, warehouse name and role name here. If no role name provided it will default to PUBLIC
credentials:
type: username_password
username: <username> # add the Snowflake database user name here or leave this blank and type in CLI command line
password: <password> # add the Snowflake database password here or leave this blank and type in CLI command line
CLI で Azure Machine Learning 接続を作成します。
オプション 1: YAML ファイルでユーザー名とパスワードを使用する
az ml connection create --file my_snowflakedb_connection.yaml
オプション 2: コマンド ラインでユーザー名とパスワードをオーバーライドする
az ml connection create --file my_snowflakedb_connection.yaml --set credentials.
username="<username>" credentials.
password="<password>"
オプション 1: YAML ファイルから接続を読み込む
from azure.ai.ml import MLClient, load_workspace_connection
ml_client = MLClient.from_config()
wps_connection = load_workspace_connection(source="./my_snowflakedb_connection.yaml")
wps_connection.credentials.username="<username>"
wps_connection.credentials.password="<password>"
ml_client.connections.create_or_update(workspace_connection=wps_connection)
オプション 2: Python スクリプトで WorkspaceConnection() を使用する
from azure.ai.ml import MLClient
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration
# If using username/password, the name/password values should be url-encoded
import urllib.parse
username = urllib.parse.quote(os.environ["SNOWFLAKEDB_USERNAME"], safe="")
password = urllib.parse.quote(os.environ["SNOWFLAKEDB_PASSWORD"], safe="")
target= "jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&role=<myrole>"
# add the Snowflake account, database, warehouse name and role name here. If no role name provided it will default to PUBLIC
name= <my_snowflake_connection> # name of the connection
wps_connection = WorkspaceConnection(name= name,
type="snowflake",
target= target,
credentials= UsernamePasswordConfiguration(username=username, password=password)
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
Azure Machine Learning スタジオに移動します。
左側のナビゲーションの [資産] で [データ] を選択します。 次に、[データ接続] タブを選択します。その後、次のスクリーンショットに示すように、[作成] を選択します。
[接続の作成] ペインで、スクリーンショットに示すように値を入力します。 カテゴリとしては Snowflake、[認証の種類] としては [ユーザー名パスワード] を選択します。 次の形式で [ターゲット] テキストボックスの値を指定し、<> 文字の間に特定の値を入力してください。
jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&role=<myrole>
[保存] を選択して、関連するワークスペースに関連付けられているキー コンテナーに資格情報を安全に保存します。 この接続は、データ インポート ジョブを実行するときに使用されます。
OAuth を使用する Snowflake DB 接続を作成する
このセクションでは、OAuth を使用して認証する Snowflake DB 接続を作成する方法について説明します。
重要
このセクションの手順を実行する前に、まずクライアントに代わって OAuth トークンを発行するように Azure を構成する必要があります。 この構成により、OAuth 接続に必要なサービス プリンシパルが作成されます。 接続を作成するには、次の情報が必要です。
- クライアント ID: サービス プリンシパルの ID
- クライアント シークレット: サービス プリンシパルのシークレット
- テナント ID: Microsoft Entra ID テナントの ID
この YAML ファイルにより、OAuth を使用する Snowflake DB 接続が作成されます。 必ず、該当する値を更新してください。
# my_snowflakedb_connection.yaml
name: snowflake_service_principal_connection
type: snowflake
# Add the Snowflake account, database, warehouse name, and role name here. If no role name is provided, it will default to PUBLIC.
target: jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&scope=<scopeForServicePrincipal>
credentials:
type: service_principal
client_id: <client-id> # The service principal's client id
client_secret: <client-secret> # The service principal's client secret
tenant_id: <tenant-id> # The Microsoft Entra ID tenant id
CLI で Azure Machine Learning 接続を作成します。
az ml connection create --file my_snowflakedb_connection.yaml
コマンド ラインで YAML ファイル内の情報をオーバーライドすることもできます。
az ml connection create --file my_snowflakedb_connection.yaml --set credentials.client_id="my-client-id" credentials.client_secret="my-client-secret" credentials.tenant_id="my-tenant-id"
Python SDK を使用すると、YAML ファイルに保存されている接続情報を読み込んで接続を作成できます。 必要に応じて、値をオーバーライドできます。
from azure.ai.ml import MLClient, load_workspace_connection
ml_client = MLClient.from_config()
wps_connection = load_workspace_connection(source="./my_snowflakedb_connection.yaml")
wps_connection.credentials_client_id="my-client-id"
wps_connection.credentials.client_secret="my-client-secret"
wps_connection.credentials.tenant_id="my-tenant-id"
ml_client.connections.create_or_update(workspace_connection=wps_connection)
YAML ファイルに頼らずに、Python スクリプトで接続情報を直接指定することもできます。
from azure.ai.ml import MLClient
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import ServicePrincipalConfiguration
target= "jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&role=<myrole>"
# add the Snowflake account, database, warehouse name and role name here. If no role name provided it will default to PUBLIC
name= <my_snowflake_connection> # name of the connection
auth = ServicePrincipalConfiguration(client_id="<my-client-id>", client_secret="<my-client-secret>", tenant_id="<my-tenant-id>")
wps_connection = WorkspaceConnection(name= name,
type="snowflake",
target=target,
credentials=auth
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
注
サービス プリンシパル (OAuth 用) を使用した Snowflake DB 接続の作成は、Azure CLI または Python SDK でのみ使用できます。
Azure SQL DB 接続を作成する
この YAML スクリプトでは、Azure SQL DB 接続を作成します。 必ず、該当する値を更新してください。
# my_sqldb_connection.yaml
$schema: http://azureml/sdk-2-0/Connection.json
type: azure_sql_db
name: my-sqldb-connection
target: Server=tcp:<myservername>,<port>;Database=<mydatabase>;Trusted_Connection=False;Encrypt=True;Connection Timeout=30
# add the sql servername, port addresss and database
credentials:
type: sql_auth
username: <username> # add the sql database user name here or leave this blank and type in CLI command line
password: <password> # add the sql database password here or leave this blank and type in CLI command line
CLI で Azure Machine Learning 接続を作成します。
オプション 1: YAML ファイルからのユーザー名/パスワードを使用する
az ml connection create --file my_sqldb_connection.yaml
オプション 2: YAML ファイルでユーザー名とパスワードをオーバーライドする
az ml connection create --file my_sqldb_connection.yaml --set credentials.
username="<username>" credentials.
password="<password>"
オプション 1: YAML ファイルから接続を読み込む
from azure.ai.ml import MLClient, load_workspace_connection
ml_client = MLClient.from_config()
wps_connection = load_workspace_connection(source="./my_sqldb_connection.yaml")
wps_connection.credentials.username="<username>"
wps_connection.credentials.password="<password>"
ml_client.connections.create_or_update(workspace_connection=wps_connection)
オプション 2: WorkspaceConnection() を使用する
from azure.ai.ml import MLClient
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration
# If using username/password, the name/password values should be url-encoded
import urllib.parse
username = urllib.parse.quote(os.environ["MYSQL_USERNAME"], safe="")
password = urllib.parse.quote(os.environ["MYSQL_PASSWORD"], safe="")
target= "Server=tcp:<myservername>,<port>;Database=<mydatabase>;Trusted_Connection=False;Encrypt=True;Connection Timeout=30"
# add the sql servername, port address and database
name= <my_sql_connection> # name of the connection
wps_connection = WorkspaceConnection(name= name,
type="azure_sql_db",
target= target,
credentials= UsernamePasswordConfiguration(username=username, password=password)
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
Azure Machine Learning スタジオに移動します。
左側のナビゲーションの [資産] で [データ] を選択します。 次に、[データ接続] タブを選択します。その後、次のスクリーンショットに示すように、[作成] を選択します。
[接続の作成] ペインで、スクリーンショットに示すように値を入力します。 カテゴリとしては AzureSqlDb、[認証の種類] としては [ユーザー名パスワード] を選択します。 次の形式で [ターゲット] テキストボックスの値を指定し、<> 文字の間に特定の値を入力してください。
Server=tcp:<myservername>,<port>;Database=<mydatabase>;Trusted_Connection=False;Encrypt=True;接続タイムアウト = 30
Amazon S3 接続を作成する
次の YAML ファイルを使用して Amazon S3 接続を作成します。 必ず、該当する値を更新してください。
# my_s3_connection.yaml
$schema: http://azureml/sdk-2-0/Connection.json
type: s3
name: my_s3_connection
target: <mybucket> # add the s3 bucket details
credentials:
type: access_key
access_key_id: bbbbbbbb-1c1c-2d2d-3e3e-444444444444 # add access key id
secret_access_key: H4iJ5kL6mN7oP8qR9sT0uV1wX2yZ3a # add access key secret
CLI で Azure Machine Learning 接続を作成します。
az ml connection create --file my_s3_connection.yaml
オプション 1: YAML ファイルから接続を読み込む
from azure.ai.ml import MLClient, load_workspace_connection
ml_client = MLClient.from_config()
wps_connection = load_workspace_connection(source="./my_s3_connection.yaml")
ml_client.connections.create_or_update(workspace_connection=wps_connection)
オプション 2: Python スクリプトで WorkspaceConnection() を使用する
from azure.ai.ml import MLClient
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import AccessKeyConfiguration
target=<mybucket> # add the s3 bucket details
name=<my_s3_connection> # name of the connection
wps_connection=WorkspaceConnection(name=name,
type="s3",
target= target,
credentials= AccessKeyConfiguration(access_key_id="XXXJ5kL6mN7oP8qR9sT0uV1wX2yZ3aB4cXXX",acsecret_access_key="C2dE3fH4iJ5kL6mN7oP8qR9sT0uV1w")
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
Azure Machine Learning スタジオに移動します。
左側のナビゲーションの [資産] で [データ] を選択します。 次に、[データ接続] タブを選択します。その後、次のスクリーンショットに示すように、[作成] を選択します。
[接続の作成] ペインで、スクリーンショットに示すように値を入力します。 カテゴリとしては S3、[認証の種類] としては [ユーザー名パスワード] を選択します。 次の形式で [ターゲット] テキストボックスの値を指定し、<> 文字の間に特定の値を入力してください。
<ターゲット>
非データ接続
次の接続の種類を使用して Git に接続できます。
- Python フィード
- Azure Container Registry
- API キーを使用する接続
これらの接続はデータ接続ではありませんが、コードで使うための外部サービスへの接続に使われます。
Git
次のいずれかの YAML ファイルを使って Git 接続を作成します。 必ず、該当する値を更新してください。
個人用アクセス トークン (PAT) を使って接続します。
#Connection.yml
name: test_ws_conn_git_pat
type: git
target: https://github.com/contoso/contosorepo
credentials:
type: pat
pat: dummy_pat
パブリック リポジトリに接続します (資格情報なし)。
#Connection.yml
name: git_no_cred_conn
type: git
target: https://https://github.com/contoso/contosorepo
CLI で Azure Machine Learning 接続を作成します。
az ml connection create --file connection.yaml
次の例では、GitHub リポジトリへの Git 接続を作成します。 個人用アクセス トークン (PAT) によって、接続が認証されます。
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration, PatTokenConfiguration
name = "my_git_conn"
target = "https://github.com/myaccount/myrepo"
wps_connection = WorkspaceConnection(
name=name,
type="git",
target=target,
credentials=PatTokenConfiguration(pat="E3fH4iJ5kL6mN7oP8qR9sT0uV1wX2y"),
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
スタジオで Git 接続を作成することはできません。
Python フィード
次のいずれかの YAML ファイルを使って Python フィードへの接続を作成します。 必ず、該当する値を更新してください。
個人用アクセス トークン (PAT) を使って接続します。
#Connection.yml
name: test_ws_conn_python_pat
type: python_feed
target: https://test-feed.com
credentials:
type: pat
pat: dummy_pat
ユーザー名とパスワードを使って接続します。
name: test_ws_conn_python_user_pass
type: python_feed
target: https://test-feed.com
credentials:
type: username_password
username: <username>
password: <password>
パブリック フィードに接続します (資格情報なし)。
name: test_ws_conn_python_no_cred
type: python_feed
target: https://test-feed.com3
CLI で Azure Machine Learning 接続を作成します。
az ml connection create --file connection.yaml
次の例では、Python フィード接続を作成します。 個人用アクセス トークン (PAT) またはユーザー名とパスワードによって、接続が認証されます。
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration, PatTokenConfiguration
# If using username/password, the name/password values should be url-encoded
# import urllib.parse
# username = urllib.parse.quote(os.environ["FEED_USERNAME"], safe="")
# password = urllib.parse.quote(os.environ["FEED_PASSWORD"], safe="")
name = "my_pfeed_conn"
target = "https://iJ5kL6mN7.core.windows.net/mycontainer"
wps_connection = WorkspaceConnection(
name=name,
type="python_feed",
target=target,
#credentials=UsernamePasswordConfiguration(username=username, password=password),
credentials=PatTokenConfiguration(pat="<PatTokenConfiguration>"),
#credentials=None
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
スタジオで Python フィード接続を作成することはできません。
Container Registry
次のいずれかの YAML ファイルを使って Azure Container Registry への接続を作成します。 必ず、該当する値を更新してください。
CLI で Azure Machine Learning 接続を作成します。
az ml connection create --file connection.yaml
次の例では、Azure Container Registry 接続を作成します。
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration
# If using username/password, the name/password values should be url-encoded
import urllib.parse
username = os.environ["REGISTRY_USERNAME"]
password = os.environ["REGISTRY_PASSWORD"]
name = "my_acr_conn"
target = "https://iJ5kL6mN7.core.windows.net/mycontainer"
wps_connection = WorkspaceConnection(
name=name,
type="container_registry",
target=target,
credentials=UsernamePasswordConfiguration(username=username, password=password),
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
スタジオで Azure Container Registry 接続を作成することはできません。
API キー
次の例では、API キー接続を作成します。
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration, ApiKeyConfiguration
name = "my_api_key"
target = "https://L6mN7oP8q.core.windows.net/mycontainer"
wps_connection = WorkspaceConnection(
name=name,
type="apikey",
target=target,
credentials=ApiKeyConfiguration(key="9sT0uV1wX"),
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
汎用コンテナー レジストリ
GenericContainerRegistry ワークスペース接続を使用して、イメージ ビルド用に Nexus や Artifactory などの外部レジストリを指定できます。 環境イメージは指定されたレジストリからプッシュされ、以前のキャッシュは無視されます。
次の YAML ファイルを使用して接続を作成します。 必ず、該当する値を更新してください。
#myenv.yml
$schema: https://azuremlschemas.azureedge.net/latest/environment.schema.json
name: docker-image-plus-conda-example
image: mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04
type: python_feed
conda_file: conda_dep.yml
description: Environment created from a Docker image plus Conda environment
#conda_dep.yml
name: project_environment
dependencies:
- python=3.10
- pip:
- azureml-defaults
channels:
- anaconda
- conda-forge
#connection.yml
name: ws_conn_generic_container_registry
type: container_registry
target: https://test-registry.com
credentials:
type: username_password
username: <username>
password: <password>
#hello_world_job.yml
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
command: echo "hello world"
environment: azureml:<env name>@latest
資格情報を使用して YAML ファイルから接続を作成します。
az ml connection create --file connection.yaml --credentials username=<username> password=<password> --resource-group my-resource-group --workspace-name my-workspace
環境を作成する
az ml environment create --name my-env --version 1 --file my_env.yml --conda-file conda_dep.yml --image mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04 --resource-group my-resource-group --workspace-name my-workspace
環境が正常に作成されたことを確認できます
az ml environment show --name my-env --version 1 --resource-group my-resource-group --workspace-name my-workspace
次の例では、一般的な Container Registry 接続を作成します。
import os
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
from azure.ai.ml.entities import Environment
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration
from azureml.core.conda_dependencies import CondaDependencies
from azure.ai.ml import command
username = os.environ["REGISTRY_USERNAME"]
password = os.environ["REGISTRY_PASSWORD"]
# Enter details of Azure Machine Learning workspace
subscription_id = "<SUBSCRIPTION_ID>"
resource_group = "<RESOURCE_GROUP>"
workspace = "<AML_WORKSPACE_NAME>"
ml_client = MLClient( DefaultAzureCredential(), subscription_id, resource_group, workspace)
credentials = UsernamePasswordConfiguration(username=username, password=password)
# Create GenericContainerRegistry workspace connection for a generic registry
ws_connection = WorkspaceConnection(name="<name>", target="<target>", type="GenericContainerRegistry", credentials=credentials)
ml_client.connections.create_or_update(ws_connection)
# Create an environment
env_docker_conda = Environment(image="<base image>", conda_file="<yml file>", name="docker-image-plus-conda-example", description="Environment created from a Docker image plus Conda environment.")
ml_client.environments.create_or_updat(env_docker_conda)
job = command(command="echo 'hello world'", environment=env_docker_conda,display_name="v2-job-example")
returned_job = ml_client.create_or_update(job)
Azure Machine Learning スタジオに移動します。
左側のナビゲーションの [管理] で [接続] を選択し、[作成] を選択します。
[Other resources types]\(その他のリソースの種類\) の *[汎用コンテナー レジストリ] を選択しますScreenshot highlighting the option to connect to a generic container registry in Azure Machine Learning studio UI.
必要な情報を入力し、[接続の追加] を選択します
関連コンテンツ
データ接続 (Snowflake DB、Amazon S3、または Azure SQL DB) を使用する場合、詳細については次の記事を参照してください。