빠른 시작: Bicep 파일을 서비스 주체로 배포

이 빠른 시작에서는 비대화형 인증이라고도 하는 앱 전용 인증을 사용하여 Microsoft Graph 리소스가 포함된 Bicep 파일을 배포합니다. CI/CD(연속 통합 및 지속적인 업데이트) 파이프라인에 제로 터치 배포 통합을 위해 이 메커니즘을 사용할 수 있습니다.

위임된 인증 또는 대화형 인증을 사용하여 배포하려면 Microsoft Graph 리소스를 사용하여 Bicep 파일 만들기를 참조 하세요.

Important

Microsoft Graph Bicep은 현재 미리 보기로 제공됩니다. 베타, 미리 보기로 제공되거나 아직 일반 공급으로 릴리스되지 않은 Azure 기능에 적용되는 약관은 Microsoft Azure 미리 보기에 대한 추가 사용 약관을 참조하세요.

필수 조건

서비스 주체 만들기 및 Azure 역할 할당

이전 세션에서 Azure CLI에 로그인하는 동안 나중에 Bicep 파일을 배포하는 데 사용하는 서비스 주체를 만듭니다.

이 빠른 시작에서는 클라이언트 암호라고도 하는 애플리케이션 암호를 사용하여 서비스 주체를 만듭니다. 또한 서비스 주체에게 리소스 그룹으로 범위가 지정된 관리 ID 기여자 역할을 할당합니다.

주의

이 빠른 시작에서는 단순성 및 테스트 목적으로만 애플리케이션 암호를 사용합니다. 프로덕션 환경에서는 사용하지 마세요.

# Create a resource group
az group create --name exampleRG --location eastus

# Create a service principal with the Managed Identity Contributor role. Replace {myServicePrincipalName}, {mySubscriptionId}, and {myResourceGroupName} with your values.
az ad sp create-for-rbac --name {myServicePrincipalName} --role "Managed Identity Contributor" --scopes "/subscriptions/{mySubscriptionId}/resourceGroups/{myResourceGroupName}"

출력 콘솔:

{
  "appId": "myServicePrincipalId",
  "displayName": "myServicePrincipalName",
  "password": "myServicePrincipalPassword",
  "tenant": "myOrganizationTenantId"
}

출력에는 키가 포함됩니다 password . 이 값은 검색할 수 없으므로 복사해야 합니다.

서비스 주체에 Microsoft Graph 권한 할당

Microsoft Graph PowerShell을 사용하여 서비스 주체에 Group.ReadWrite.All 애플리케이션 전용 권한을 부여합니다. 권한 있는 역할 관리자 역할을 사용하면 AppRoleAssignment.ReadWrite.AllApplication.Read.All 권한을 자신에게 부여하여 이 작업을 수행할 수 있습니다.

주의

AppRoleAssignment.ReadWrite.All 권한이 부여된 앱은 적절한 사용자만 액세스해야 합니다. 자세한 내용은 AppRoleAssignment.ReadWrite.All을 참조하세요.

# Authenticate to Microsoft Graph
Connect-MgGraph -Scopes "AppRoleAssignment.ReadWrite.All","Application.Read.All"

# Find the service principal created to run the deployment
$mySP = Get-MgServicePrincipalByAppId -AppId "myServicePrincipalId"

# Find the service principal for Microsoft Graph
$graphSP = Get-MgServicePrincipalByAppId -AppId "00000003-0000-0000-c000-000000000000"

# Assign Group.ReadWrite.All app-only permission (id = 62a82d76-70ea-41e2-9197-370581804d09)
New-MgServicePrincipalAppRoleAssignedTo -ResourceId $graphSP.Id -ServicePrincipalId $graphSP.Id -PrincipalId $mySP.Id -AppRoleId "62a82d76-70ea-41e2-9197-370581804d09" 

서비스 주체로 로그인하여 Bicep 파일 배포

이전에 만든 서비스 주체로 로그인합니다.

# Sign in with the service principal created earlier. This sample uses the Bash console.
spID=$(az ad sp list --display-name myServicePrincipalName --query "[].{spID:appId}" --output tsv)
tenantID=$(az ad sp list --display-name myServicePrincipalName --query "[].{tenantID:appOwnerOrganizationId}" --output tsv)
echo "Using appId $spID in tenant $tenantID"

az login --service-principal --username $spID --password {paste your SP password here} --tenant $tenantID

중요

콘솔에 암호를 표시하지 않고 대화형으로 사용 az login 하려는 경우 다음 명령을 bash사용합니다read -s.

read -sp "Azure password: " AZ_PASS && echo && az login --service-principal -u <app-id> -p $AZ_PASS --tenant <tenant>

Bicep 파일 배포

이제 리소스 그룹의 범위를 사용하여 Bicep 파일을 배포합니다.

az deployment group create --resource-group exampleRG --template-file main.bicep

참고 항목

복제 지연으로 인해 MSI(관리 서비스 ID)를 Microsoft Entra 그룹의 소유자로 추가하면 배포가 실패할 수 있습니다. 잠시 기다린 다음 동일한 Bicep 파일을 다시 배포합니다.

리소스 정리

Azure 리소스가 더 이상 필요하지 않은 경우 Azure CLI 또는 Azure PowerShell 모듈을 사용하여 만든 리소스 그룹을 삭제합니다.

참고 항목

리소스 그룹은 Azure 개념이며 Microsoft Graph 리소스에 영향을 주지 않습니다. Microsoft Graph 리소스는 Microsoft Graph에 대한 추가 요청으로 정리해야 합니다. 이를 위해 Azure CLI 또는 Azure PowerShell, Microsoft Graph CLI 또는 Microsoft Graph PowerShell을 사용할 수 있습니다.

다음 예제에서는 먼저 Azure 리소스를 삭제한 다음, Azure CLI 및 Azure PowerShell을 사용하여 Microsoft Graph 리소스를 삭제하는 명령을 보여 줍니다.

# Delete the resource group
az group delete --name exampleRG

# Delete the Microsoft Graph group
az rest --method delete --url 'https://graph.microsoft.com/v1.0/groups%28uniqueName=%27myExampleGroup%27%29'

# Delete the client service principal
spID=$(az ad sp list --display-name myServicePrincipalName --query "[].{spID:id}" --output tsv)
az ad sp delete --id $spID