クイック スタート:ARM テンプレートを使用したイベント ハブの作成

このクイックスタートでは、Azure Resource Manager テンプレート (ARM テンプレート) を使用してイベント ハブを作成します。 ARM テンプレートをデプロイして、イベント ハブを 1 つ含む Event Hubs 型の名前空間を作成します。

前提条件

テンプレートを確認する

このクイックスタートで使用されるテンプレートは Azure クイックスタート テンプレートからのものです。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.5.6.12127",
      "templateHash": "16940368634879422816"
    }
  },
  "parameters": {
    "projectName": {
      "type": "string",
      "metadata": {
        "description": "Specifies a project name that is used to generate the Event Hub name and the Namespace name."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specifies the Azure location for all resources."
      }
    },
    "eventHubSku": {
      "type": "string",
      "defaultValue": "Standard",
      "allowedValues": [
        "Basic",
        "Standard"
      ],
      "metadata": {
        "description": "Specifies the messaging tier for Event Hub Namespace."
      }
    }
  },
  "variables": {
    "eventHubNamespaceName": "[format('{0}ns', parameters('projectName'))]",
    "eventHubName": "[parameters('projectName')]"
  },
  "resources": [
    {
      "type": "Microsoft.EventHub/namespaces",
      "apiVersion": "2021-11-01",
      "name": "[variables('eventHubNamespaceName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('eventHubSku')]",
        "tier": "[parameters('eventHubSku')]",
        "capacity": 1
      },
      "properties": {
        "isAutoInflateEnabled": false,
        "maximumThroughputUnits": 0
      }
    },
    {
      "type": "Microsoft.EventHub/namespaces/eventhubs",
      "apiVersion": "2021-11-01",
      "name": "[format('{0}/{1}', variables('eventHubNamespaceName'), variables('eventHubName'))]",
      "properties": {
        "messageRetentionInDays": 7,
        "partitionCount": 1
      },
      "dependsOn": [
        "[resourceId('Microsoft.EventHub/namespaces', variables('eventHubNamespaceName'))]"
      ]
    }
  ]
}

このテンプレートに定義されているリソースは次のとおりです。

テンプレートのその他のサンプルについては、「Azure クイック スタート テンプレート」をご覧ください。

テンプレートのデプロイ

Azure portal ユーザー インターフェイスの使用

  1. 環境が前提条件を満たしていて、ARM テンプレートの使用に慣れている場合は、 [Azure へのデプロイ] ボタンを選択します。 テンプレートが Azure portal で開きます。

    Button to deploy the Resource Manager template to Azure.

  2. 既存のリソース グループを選択するか、リソース グループを作成して選択します。

  3. リージョンを選択します。

  4. プロジェクトに一意の名前を入力します。 この名前は、Event Hubs 名前空間と名前空間内のイベント ハブの名前を生成するために使用されます。

  5. [Review + create](レビュー + 作成) を選択します。

  6. [確認および作成] ページで、 [作成] を選択します。

Azure Cloud Shell の使用

Azure Cloud Shell を使用してテンプレートをデプロイするには、以下の手順を実行します。

  1. 次のコード ブロックで [Cloud Shell を開く] を選択し、指示に従って Azure Cloud Shell にサインインします。

    $projectName = Read-Host -Prompt "Enter a project name that is used for generating resource names"
    $location = Read-Host -Prompt "Enter the location (i.e. centralus)"
    $resourceGroupName = "${projectName}rg"
    $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.eventhub/eventhubs-create-namespace-and-eventhub/azuredeploy.json"
    
    New-AzResourceGroup -Name $resourceGroupName -Location $location
    New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -projectName $projectName
    
    Write-Host "Press [ENTER] to continue ..."
    

    イベント ハブが作成されるまでしばらく時間がかかります。

  2. [コピー] を選択し、PowerShell スクリプトがコピーされます。

  3. シェル コンソールを右クリックし、 [貼り付け] を選択します。

  4. Enter キーを押してコマンドを実行します。

デプロイの検証

デプロイを検証するには、Azure portal からリソース グループを開くか、次の Azure PowerShell スクリプトを使用します。 Cloud Shell がまだ開いている場合は、最初の行 (Read-Host) をコピー/実行する必要はありません。

$projectName = Read-Host -Prompt "Enter the same project name that you used in the last procedure"
$resourceGroupName = "${projectName}rg"
$namespaceName = "${projectName}ns"

Get-AzEventHub -ResourceGroupName $resourceGroupName -Namespace $namespaceName

Write-Host "Press [ENTER] to continue ..."

リソースをクリーンアップする

Azure リソースが不要になったら、リソース グループを削除して、デプロイしたリソースをクリーンアップします。 Cloud Shell がまだ開いている場合は、最初の行 (Read-Host) をコピー/実行する必要はありません。

$projectName = Read-Host -Prompt "Enter the same project name that you used in the last procedure"
$resourceGroupName = "${projectName}rg"

Remove-AzResourceGroup -ResourceGroupName $resourceGroupName

Write-Host "Press [ENTER] to continue ..."

次のステップ

この記事では、Event Hubs 名前空間を作成し、その名前空間にイベント ハブを作成しました。 イベント ハブとの間でイベントを送信または受信するためのステップ バイ ステップの手順については、以下のイベントの送受信のチュートリアルを参照してください。