Share via


建立映像,並以使用者指派的受控識別來存取 Azure 儲存體帳戶中的檔案

適用於:✔️ Linux VM ✔️ 彈性擴展集

本文中會說明如何使用 Azure VM Image Builder 建立自訂映像。 此服務會使用使用者指派的受控識別來存取 Azure 記憶體帳戶中的檔案,而且可以封鎖對記憶體帳戶的未驗證存取。

Azure VM Image Builder 支援使用指令碼,以及從 GitHub、Azure 儲存體帳戶和其他位置複製檔案。 如果您想要使用位置,VM Image Builder 必須可從外部存取這些位置。

在接下來的範例中,您將建立兩個資源群組,一個用於自訂映像,另一個用於裝載含有指令檔的 Azure 儲存體帳戶。 這些範例會模擬真實情節,其中您的組建成品或映像檔案可能建置於不同的儲存體帳戶。 您將建立使用者指派的身分識別,然後將身分識別讀取權限授與指令檔,但不允許這個檔案的公用存取。 然後,您將使用 Shell 自訂工具,從儲存體帳戶下載並執行指令碼。

建立資源群組

  1. 因為您會重複使用某些資訊,因此請建立一些變數來儲存這些資訊。

    # Image resource group name 
    imageResourceGroup=aibmdimsi
    # Storage resource group
    strResourceGroup=aibmdimsistor
    # Location 
    location=WestUS2
    # Name of the image to be created
    imageName=aibCustLinuxImgMsi01
    # Image distribution metadata reference name
    runOutputName=u1804ManImgMsiro
    
  2. 為訂閱識別碼建立變數:

    subscriptionID=$(az account show --query id --output tsv)
    
  3. 建立資源群組以儲存映像和指令碼:

    # Create a resource group for the image template
    az group create -n $imageResourceGroup -l $location
    # Create a resource group for the script storage
    az group create -n $strResourceGroup -l $location
    
  4. 建立使用者指派的身分識別,並在資源群組上設定權限:

    VM Image Builder 會使用所提供的使用者身分識別,將映像插入資源群組。 在此範例中,您會建立具有發布映像特定動作的 Azure 角色定義。 然後此將角色定義指派給使用者身分識別。

    # Create a user-assigned identity for VM Image Builder to access the storage account where the script is located
    identityName=aibBuiUserId$(date +'%s')
    az identity create -g $imageResourceGroup -n $identityName
    
    # Get an identity ID
    imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $identityName --query clientId -o tsv)
    
    # Get the user-identity URI, which is needed for the template
    imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$imageResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName
    
    # Download the preconfigured role definition example
    curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json
    
    # Update the definition
    sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json
    sed -i -e "s/<rgName>/$imageResourceGroup/g" aibRoleImageCreation.json
    
    # Create role definitions
    az role definition create --role-definition ./aibRoleImageCreation.json
    
    # Grant the role definition to the user-assigned identity
    az role assignment create \
        --assignee $imgBuilderCliId \
        --role "Azure Image Builder Service Image Creation Role" \
        --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
    
  5. 建立儲存體帳戶,並從 GitHub 將範例指令碼複製到儲存體:

    # Script storage account
    scriptStorageAcc=aibstorscript$(date +'%s')
    
    # Script container
    scriptStorageAccContainer=scriptscont$(date +'%s')
    
    # Script URL
    scriptUrl=https://$scriptStorageAcc.blob.core.windows.net/$scriptStorageAccContainer/customizeScript.sh
    
    # Create the storage account and blob in the resource group
    az storage account create -n $scriptStorageAcc -g $strResourceGroup -l $location --sku Standard_LRS
    
    az storage container create -n $scriptStorageAccContainer --fail-on-exist --account-name $scriptStorageAcc
    
    # Copy in an example script from the GitHub repo 
    az storage blob copy start \
        --destination-blob customizeScript.sh \
        --destination-container $scriptStorageAccContainer \
        --account-name $scriptStorageAcc \
        --source-uri https://raw.githubusercontent.com/azure/azvmimagebuilder/master/quickquickstarts/customizeScript.sh
    
  6. 授權 VM Image Builder 在映像資源群組中建立資源。 --assignee 值是使用者身分識別的識別碼。

    az role assignment create \
        --assignee $imgBuilderCliId \
        --role "Storage Blob Data Reader" \
        --scope /subscriptions/$subscriptionID/resourceGroups/$strResourceGroup/providers/Microsoft.Storage/storageAccounts/$scriptStorageAcc/blobServices/default/containers/$scriptStorageAccContainer 
    

修改範例

下載 JSON 範例檔案,並以您先前建立的變數來設定該檔案。

curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/quickquickstarts/7_Creating_Custom_Image_using_MSI_to_Access_Storage/helloImageTemplateMsi.json -o helloImageTemplateMsi.json
sed -i -e "s/<subscriptionID>/$subscriptionID/g" helloImageTemplateMsi.json
sed -i -e "s/<rgName>/$imageResourceGroup/g" helloImageTemplateMsi.json
sed -i -e "s/<region>/$location/g" helloImageTemplateMsi.json
sed -i -e "s/<imageName>/$imageName/g" helloImageTemplateMsi.json
sed -i -e "s%<scriptUrl>%$scriptUrl%g" helloImageTemplateMsi.json
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateMsi.json
sed -i -e "s%<runOutputName>%$runOutputName%g" helloImageTemplateMsi.json

建立映像

  1. 將映像組態提交至 VM Image Builder 服務:

    az resource create \
        --resource-group $imageResourceGroup \
        --properties @helloImageTemplateMsi.json \
        --is-full-object \
        --resource-type Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateMsi01
    
  2. 啟動映像組建:

    az resource invoke-action \
        --resource-group $imageResourceGroup \
        --resource-type  Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateMsi01 \
        --action Run 
    

組建可能需要大約 15 分鐘才能完成。

建立 VM

  1. 從映像建立 VM:

    az vm create \
    --resource-group $imageResourceGroup \
    --name aibImgVm00 \
    --admin-username aibuser \
    --image $imageName \
    --location $location \
    --generate-ssh-keys
    
  2. 建立 VM 之後,對其啟動安全殼層 (SSH) 工作階段。

    ssh aibuser@<publicIp>
    

建立 SSH 連線後,您應該會接收到「當天的訊息」,顯示該映像是自訂的:


*******************************************************
**            This VM was built from the:            **
**      !! AZURE VM IMAGE BUILDER Custom Image !!    **
**         You have just been Customized :-)         **
*******************************************************

清除資源

如果您不再需要在此流程中建立的資源,您可執行下列程式碼予以刪除:


az role definition delete --name "$imageRoleDefName"
```azurecli-interactive
az role assignment delete \
    --assignee $imgBuilderCliId \
    --role "$imageRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
az identity delete --ids $imgBuilderId
az resource delete \
    --resource-group $imageResourceGroup \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateMsi01
az group delete -n $imageResourceGroup
az group delete -n $strResourceGroup

下一步

如果您對使用 VM Image Builder 有任何問題,請參閱針對 Azure VM Image Builder 進行疑難排解