이미지를 만들고 사용자가 할당한 관리 ID를 사용하여 Azure 스토리지 계정의 파일에 액세스

적용 대상: ✔️ Linux VM ✔️ 유연한 확장 집합

이 문서에서는 Azure VM Image Builder를 사용하여 사용자 지정된 이미지를 만드는 방법을 보여 줍니다. 이 서비스는 사용자가 할당한 관리 ID를 사용하여 Azure Storage 계정의 파일에 액세스하며, 스토리지 계정에 대한 인증되지 않은 액세스를 차단할 수 있습니다.

Azure VM Image Builder는 GitHub, Azure 스토리지 계정 및 기타 위치에서 스크립트를 사용하고 파일을 복사할 수 있도록 지원합니다. 위치를 사용하려면 외부에서 VM Image Builder에 액세스할 수 있어야 합니다.

다음 예제에서는 두 개의 리소스 그룹을 만듭니다. 하나는 사용자 지정 이미지에 대한 리소스 그룹이며 다른 하나는 스크립트 파일이 포함된 Azure 스토리지 계정을 호스트하기 위한 리소스 그룹입니다. 이 예제는 다양한 스토리지 계정에 빌드 아티팩트 또는 이미지 파일이 있을 수 있는 실제 시나리오를 시뮬레이션합니다. 사용자가 할당한 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이 만들어지면 이를 사용하여 SSH(Secure Shell) 세션을 시작합니다.

    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 문제 해결을 참조하세요.