イメージを作成し、ユーザーが割り当てたマネージド ID を使用して Azure ストレージ アカウント内のファイルにアクセスする

適用対象: ✔️ Linux VM ✔️ フレキシブルなスケール セット

この記事では、Azure VM Image Builder を使用してカスタマイズされたイメージを作成する方法について説明します。 このサービスは、ユーザー割り当てマネージド ID を使用して Azure ストレージ アカウント内のファイルにアクセスし、ストレージ アカウントへの認証されていないアクセスのブロックを実現できます。

Azure VM Image Builder では、スクリプトの使用と、GitHub、Azure ストレージ アカウント、およびその他の場所からのファイルのコピーがサポートされています。 場所を使用する場合は、VM Image Builder に対して外部からアクセスできる必要があります。

次の例では、カスタム イメージ用と、スクリプト ファイルを含む Azure ストレージ アカウントをホストする 2 つのリソース グループを作成します。 この例では、さまざまなストレージ アカウントにビルド成果物またはイメージ ファイルが存在する可能性がある実際のシナリオをシミュレートします。 ユーザーが割り当てた ID を作成し、その ID にスクリプト ファイルに対する読み取りアクセス許可を付与しますが、ファイルへのパブリック アクセスは許可しません。 次に、シェル カスタマイザーを使用して、ストレージ アカウントからスクリプトをダウンロードして実行します。

リソース グループを作成する

  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. サブスクリプション ID の変数を作成します。

    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. ユーザーが割り当てた ID を作成し、リソース グループにアクセス許可を設定します。

    VM Image Builder は、指定されたユーザー ID を使用して、リソース グループにイメージを挿入します。 この例では、イメージを配布するための特定のアクションを使用して、Azure ロールの定義を作成します。 このロール定義はその後、ユーザー ID に割り当てられます。

    # 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 の値はユーザー ID です。

    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 を作成したら、その VM を使用して Secure Shell (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 のトラブルシューティング を参照してください