Tutorial Lengkap Azure ML Managed Endpoints: Deployment Model Production
Azure ML Managed Endpoints menyediakan solusi fully managed untuk deploy model machine learning secara scalable. Endpoints menangani infrastruktur, scaling, security, dan monitoring secara otomatis.
Mengapa Managed Endpoints?
Manfaat Utama:- Fully managed: Tidak perlu mengelola infrastruktur
- Auto-scaling: Scale berdasarkan traffic
- Blue-green deployments: Rollout yang aman
- Built-in monitoring: Metrics dan logging
- Security: Authentication dan network isolation
- Online endpoints: Real-time inference
- Batch endpoints: Batch processing skala besar
Prerequisites
pip install azure-ai-ml azure-identity
Azure CLI
az login
az extension add -n ml
Online Endpoints
1. Buat Online Endpoint
from azure.ai.ml import MLClient
from azure.ai.ml.entities import ManagedOnlineEndpoint
from azure.identity import DefaultAzureCredential
mlclient = MLClient(
credential=DefaultAzureCredential(),
subscriptionid="your-subscription-id",
resourcegroupname="my-resource-group",
workspacename="my-ml-workspace"
)
Buat endpoint
endpoint = ManagedOnlineEndpoint(
name="my-online-endpoint",
description="Online endpoint untuk real-time inference",
authmode="key", # atau "amltoken"
tags={"environment": "production"}
)
mlclient.onlineendpoints.begincreateorupdate(endpoint).result()
print(f"Endpoint dibuat: {endpoint.name}")
2. Buat Deployment
from azure.ai.ml.entities import (
ManagedOnlineDeployment,
Model,
Environment,
CodeConfiguration
)
Buat deployment
bluedeployment = ManagedOnlineDeployment(
name="blue",
endpointname="my-online-endpoint",
model=Model(path="./model"),
codeconfiguration=CodeConfiguration(
code="./scoring",
scoringscript="score.py"
),
environment=Environment(
condafile="./environment.yml",
image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest"
),
instancetype="StandardDS3v2",
instancecount=1
)
mlclient.onlinedeployments.begincreateorupdate(bluedeployment).result()
print("Deployment dibuat")
3. Scoring Script
# scoring/score.py
import json
import joblib
import numpy as np
import os
import logging
def init():
"""Inisialisasi model saat startup."""
global model
modelpath = os.path.join(os.getenv("AZUREMLMODELDIR"), "model.joblib")
model = joblib.load(modelpath)
logging.info("Model berhasil dimuat")
def run(rawdata):
"""Jalankan inference pada data masuk."""
try:
data = json.loads(rawdata)
features = np.array(data["features"])
# Jalankan prediksi
predictions = model.predict(features)
probabilities = model.predictproba(features)
return {
"predictions": predictions.tolist(),
"probabilities": probabilities.tolist()
}
except Exception as e:
logging.error(f"Error: {str(e)}")
return {"error": str(e)}
4. Set Traffic
# Arahkan semua traffic ke deployment blue
endpoint.traffic = {"blue": 100}
mlclient.onlineendpoints.begincreateorupdate(endpoint).result()
Dapatkan detail endpoint
endpoint = mlclient.onlineendpoints.get("my-online-endpoint")
print(f"Scoring URI: {endpoint.scoringuri}")
print(f"Traffic: {endpoint.traffic}")
5. Test Endpoint
import json
Test data
testdata = {
"features": [[5.1, 3.5, 1.4, 0.2], [6.2, 3.4, 5.4, 2.3]]
}
Panggil endpoint
response = mlclient.onlineendpoints.invoke(
endpointname="my-online-endpoint",