Azure CLI를 사용하여 사용자 지정 항목을 만들고 Azure 구독에 대한 이벤트 구독

이 문서에서는 Azure CLI를 사용하여 사용자 지정 토픽을 만들고 사용자 지정 토픽에 이벤트를 보내는 방법을 보여 주는 샘플 Azure CLI 스크립트를 제공합니다.

Azure를 구독하고 있지 않다면 시작하기 전에 Azure 체험 계정을 만듭니다.

사전 요구 사항

샘플 스크립트

Azure Cloud Shell 시작

Azure Cloud Shell은 이 항목의 단계를 실행하는 데 무료로 사용할 수 있는 대화형 셸입니다. 공용 Azure 도구가 사전 설치되어 계정에서 사용하도록 구성되어 있습니다.

Cloud Shell을 열려면 코드 블록의 오른쪽 위 모서리에 있는 사용해 보세요를 선택하기만 하면 됩니다. 또한 https://shell.azure.com 로 이동하여 별도의 브라우저 탭에서 Cloud Shell을 시작할 수 있습니다.

Cloud Shell이 열리면 환경에 대해 Bash가 선택되어 있는지 확인합니다. 후속 세션은 Bash 환경에서 Azure CLI를 사용합니다. 복사를 선택하여 코드 블록을 복사하고 Cloud Shell에 붙여넣고 Enter 키를 눌러 실행합니다.

Azure에 로그인

Cloud Shell은 로그인한 초기 계정에서 자동으로 인증됩니다. 다음 스크립트를 통해 다른 구독을 사용하여 로그인하고 <Subscription ID>를 Azure 구독 ID로 바꿉니다. Azure를 구독하고 있지 않다면 시작하기 전에 Azure 체험 계정을 만듭니다.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

자세한 내용은 활성 구독 설정 또는 대화형으로 로그인을 참조하세요.

스크립트 실행

# Create Event Grid custom topic

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
subscriptionId="$(az account show --query id -o tsv)"
resourceGroup="msdocs-event-grid-rg-$randomIdentifier"
tag="event-grid"
topic="msdocs-event-grid-topic-$randomIdentifier"
site="msdocs-event-grid-site-$randomIdentifier"
eventSubscription="msdocs-event-subscription-$randomIdentifier"
webappEndpoint="https://$site.azurewebsites.net/api/updates"
storage="msdocsstorage$randomIdentifier"

# Create a resource group
echo "Creating  in "$location"..."
az group create --name $resourceGroup --location "$location" --tag $tag

# Enable and then show the Event Grid resource provider
az provider register --namespace Microsoft.EventGrid
az provider show --namespace Microsoft.EventGrid --query "registrationState"

# Create custom topic
echo "Creating $topic"
az eventgrid topic create \
--resource-group $resourceGroup \
--name $topic \
--location "$location"

# Create a message endpoint
echo "Creating $site"
az deployment group create \
  --resource-group $resourceGroup \
  --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
  --parameters siteName=$site hostingPlanName=viewerhost

# To view your web app, navigate to https://<your-site-name>.azurewebsites.net

# Subscribe to custom topic
az eventgrid event-subscription create \
  --source-resource-id "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.EventGrid/topics/$topic" \
  --name demoViewerSub \
  --endpoint $webappEndpoint

# View your web app again to see the subscription validation event.
# Select the eye icon to expand the event data
  
# Send an event to your custom topic
url=$(az eventgrid topic show --name $topic -g $resourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $topic -g $resourceGroup --query "key1" --output tsv)
echo $url
echo $key
event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Ducati", "model": "Monster"},"dataVersion": "1.0"} ]'
curl -X POST -H "aeg-sas-key: $key" -d "$event" $url

# View your web app again to see the event that you just sent

리소스 정리

다음 명령을 사용하여 이러한 리소스가 계속해서 필요한 경우가 아니면 az group delete 명령을 사용하여 리소스 그룹 및 연결된 모든 리소스를 제거합니다. 이러한 리소스 중 일부는 만들고 삭제하는 데 시간이 걸릴 수 있습니다.

az group delete --name $resourceGroup

샘플 참조

이 스크립트는 다음 명령을 사용하여 이벤트 구독을 만듭니다. 표에 있는 각 명령은 명령에 해당하는 문서에 연결됩니다.

명령 주의
az eventgrid event-subscription create Event Grid 구독을 만듭니다.

다음 단계