共用方式為


更新事件驅動工作流程 (EDW) 工作負載的應用程式程式碼

本文概述使用 Azure SDK 搭配 Azure 服務,在 Azure 中複寫 EDW 工作負載的主要應用程式程式碼更新。

資料存取程式碼

AWS 實作

AWS 工作負載依賴 AWS 服務及其相關聯的資料存取 AWS SDK。 我們已經將 AWS 服務對應至對等的 Azure 服務,因此現在可以使用 Azure SDK,建立程式碼來存取 Python 中生產者佇列和取用者結果資料庫資料表的資料。

Azure 實作

針對資料平面,生產者郵件內文 (承載) 為 JSON,且不需要在 Azure 中進行任何結構描述變更。 原始取用者應用程式會將已處理的訊息儲存在 DynamoDB 資料表中。 藉由對取用者應用程式程式碼進行微幅修改,我們可以將已處理的訊息儲存在 Azure 儲存體資料表中。

驗證碼

AWS 實作

AWS 工作負載會使用 IAM 角色原則來定義對 Amazon Simple Queue Service (SQS) 資源的完整存取權:

{
  "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sqs:*",
            "Resource": "*"
        }
    ]
}

AWS 工作負載會使用 IAM 角色原則來定義 Amazon DynamoDB 資源的完整存取權:

{
  "Version": "2012-10-17",
  "Statement": [
    {
        "Effect": "Allow",
        "Action": "dynamodb:*",
        "Resource": "*"
    }
  ]
}

在 AWS 工作負載中,使用 AWS CLI 來指派這些原則:

aws iam create-policy --policy-name sqs-sample-policy --policy-document <filepath/filename>.json
aws iam create-policy --policy-name dynamodb-sample-policy --policy-document <filepath/filename>.json
aws iam create-role --role-name keda-sample-iam-role  --assume-role-policy-document <filepath/filename>.json

aws iam attach-role-policy --role-name keda-sample-iam-role --policy-arn=arn:aws:iam::${<AWSAccountID>}:policy/sqs-sample-policy
aws iam attach-role-policy --role-name keda-sample-iam-role --policy-arn=arn:aws:iam::${<AWSAccountID>}:policy/dynamodb-sample-policy

# Set up trust relationship Kubernetes federated identity credential and map IAM role via kubectl annotate serviceaccount

Azure 實作

讓我們探索如何使用 AKS,在 Azure 環境中執行類似的 AWS 服務通訊邏輯。

您可以套用兩個 Azure RBAC 角色定義,以控制 Azure 儲存體佇列和 Azure 儲存體資料表的資料平面存取。 這些角色就像 AWS 用來控制 SQS 和 DynamoDB 存取的 IAM 角色原則。 Azure RBAC 角色不會與資源一起使用。 相反地,您要將角色指派給與指定資源相關聯的服務主體。

在 EDW 工作負載的 Azure 實作中,將角色指派給連結至 AKS Pod 中工作負載身分識別的使用者指派受控識別。 適用於 Azure 儲存體佇列和 Azure 儲存體資料表的 Azure Python SDK 會自動使用安全性主體的內容來存取這兩個資源中的資料。

使用儲存體佇列資料參與者角色,允許角色受託人讀取、寫入或刪除 Azure 儲存體佇列,以及使用儲存體資料表資料參與者角色,允許受託人讀取、寫入或刪除 Azure 儲存體資料表的資料。

下列步驟示範如何使用 Azure CLI,建立受控識別,以及指派儲存體佇列資料參與者儲存體資料表資料參與者角色:

  1. 使用 az identity create (部分機器翻譯) 命令來建立受控識別。

    managedIdentity=$(az identity create \
        --resource-group $resourceGroup \
        --name $managedIdentityName
    
  2. 使用 命令,將az role assignment create角色指派給受控識別。

    principalId=$(echo $managedIdentity | jq -r `.principalId`)
    
    az role assignment create \
        --assignee-object-id $principalId \
        --assignee-principal-type ServicePrincipal
        --role "Storage Queue Data Contributor" \
        --scope $resourceId
    
  3. 使用 命令,將az role assignment create角色指派給受控識別。

    az role assignment create \
        --assignee-object-id $principalId \
        --assignee-principal-type ServicePrincipal
        --role "Storage Table Data Contributor" \
        --scope $resourceId
    

若要查看工作範例,請參閱 deploy.sh中的 指令碼。

生產者程式碼

AWS 實作

AWS 工作負載會使用 AWS boto3 Python 程式庫來與 AWS SQS 佇列互動,以設定儲存體佇列存取。 AWS IAM AssumeRole 功能使用裝載應用程式的 EKS Pod 相關聯的 IAM 身分識別,向 SQS 端點進行驗證。

import boto3
# other imports removed for brevity
sqs_queue_url = "https://<region>.amazonaws.com/<queueid>/source-queue.fifo"
sqs_queue_client = boto3.client("sqs", region_name="<region>")    
response = sqs_client.send_message(
    QueueUrl = sqs_queue_url,
    MessageBody = 'messageBody1',
    MessageGroupId='messageGroup1')

Azure 實作

Azure 實作使用適用於 Python 的 Azure SDK 和無密碼 OAuth 驗證來與 Azure 儲存體佇列服務互動。 DefaultAzureCredential Python 類別是工作負載身分識別感知,並使用與工作負載身分識別相關聯的受控識別向儲存體佇列進行驗證。

下列範例示範如何使用 DefaultAzureCredential 類別向 Azure 儲存體佇列進行驗證:

from azure.identity import DefaultAzureCredential
from azure.storage.queue import QueueClient
# other imports removed for brevity

# authenticate to the storage queue.
account_url = "https://<storageaccountname>.queue.core.windows.net"
default_credential = DefaultAzureCredential()
aqs_queue_client = QueueClient(account_url, queue_name=queue_name ,credential=default_credential)

aqs_queue_client.create_queue()
aqs_queue_client.send_message('messageBody1')

您可以在 aqs-producer.py中檢閱佇列產生者 () 的程式碼。

取用者程式碼

AWS 實作

DynamoDB 存取的原始 AWS 程式碼會使用 AWS boto3 Python 程式庫來與 AWS SQS 佇列互動。 工作負載的取用者部分會使用與產生者相同的程式碼來連線到 AWS SQS 佇列以讀取訊息。 取用者也含有使用 AWS IAM AssumeRole 功能連線到 DynamoDB 的 Python 程式碼,以使用裝載應用程式的 EKS Pod 相關聯的 IAM 身分識別向 DynamoDB 端點進行驗證。

# presumes policy deployment ahead of time such as: aws iam create-policy --policy-name <policy_name> --policy-document <policy_document.json>
dynamodb = boto3.resource('dynamodb', region_name='<region>')
table = dynamodb.Table('<dynamodb_table_name>')
table.put_item(
    Item = {
      'id':'<guid>',
      'data':jsonMessage["<message_data>"],
      'srcStamp':jsonMessage["<source_timestamp_from_message>"],
      'destStamp':'<current_timestamp_now>',
      'messageProcessingTime':'<duration>'
    }
)

Azure 實作

Azure 實作使用適用於 Python 的 Azure SDK 來與 Azure 儲存體資料表互動。

現在您需要產生者程式碼來向 Azure 儲存體資料表進行驗證。 如先前所述,在上一節中搭配 DynamoDB 使用的結構描述與 Azure 儲存體資料表不相容。 您可以使用與 Azure Cosmos DB 相容的資料表結構描述,在 DynamoDB 中儲存與 AWS 工作負載相同的資料。

下列範例顯示 Azure 所需的程式碼:

from azure.storage.queue import QueueClient
from azure.data.tables import (TableServiceClient)

    creds = DefaultAzureCredential()
    table = TableServiceClient(
        endpoint=f"https://{storage_account_name}.table.core.windows.net/",  
        credential=creds).get_table_client(table_name=azure_table)

entity={
    'PartitionKey': _id,
    'RowKey': str(messageProcessingTime.total_seconds()),
    'data': jsonMessage['msg'],
    'srcStamp': jsonMessage['srcStamp'],
    'dateStamp': current_dateTime
}
        
response = table.insert_entity(
    table_name=azure_table,
    entity=entity,
    timeout=60
)

不同於 DynamoDB,Azure 儲存體資料表程式碼會同時指定 PartitionKeyRowKeyPartitionKey 類似於 DynamoDB 中的識別碼 uniqueidentiferPartitionKey 是 Azure 儲存體資料表邏輯容器中某分割區的 uniqueidentifierRowKey 是指定分割區中所有資料列的 uniqueidentifier

您可以在 GitHub 存放庫中檢閱完整的產生者和取用者程式碼。

建立容器映像並推送至 Azure Container Registry

現在,您可以建置容器映像並推送至 Azure Container Registry (ACR)

在複製存放庫的 app 目錄中,名為 docker-command.sh 的殼層指令碼會建置容器映像並推送至 ACR。 開啟 .sh 檔案並檢閱程式碼。 指令碼會建置產生者和取用者容器映像並推送至 ACR。 如需詳細資訊,請參閱 Azure 中的容器登錄簡介在 ACR 中推送和提取映像

若要建置容器映像並推送至 ACR,請確定環境變數 AZURE_CONTAINER_REGISTRY 設定為映像推送的目的地登錄名稱,然後執行下列命令:

./app/docker-command.sh

下一步

參與者

本文由 Microsoft 維護。 下列參與者最初撰寫:

  • Ken Kilty | 首席 TPM
  • Russell de Pina | 首席 TPM
  • Jenny Hayes | 資深內容開發人員
  • Carol Smith | 資深內容開發人員
  • Erin Schaffer |內容開發人員 2