次の方法で共有


クイックスタート: Bicep を使用して Azure Cache for Redis を作成する

Bicep と Azure Cache for Redis を使用してキャッシュをデプロイする方法について説明します。 キャッシュをデプロイした後、既存のストレージ アカウントと共にキャッシュを使用して診断データを保持します。 デプロイ対象のリソースを定義する方法と、デプロイの実行時に指定されるパラメーターを定義する方法について説明します。 この Bicep ファイルは、独自のデプロイに使用することも、要件に合わせてカスタマイズすることもできます。

Bicep は、宣言型の構文を使用して Azure リソースをデプロイするドメイン固有言語 (DSL) です。 簡潔な構文、信頼性の高いタイプ セーフ、およびコードの再利用のサポートが提供されます。 Bicep により、Azure のコード ソリューションとしてのインフラストラクチャに最適な作成エクスペリエンスが実現します。

[前提条件]

  • Azure サブスクリプション:Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。
  • ストレージ アカウント:これを作成するには、「Azure Storage アカウントの作成」を参照してください。 ストレージ アカウントは、診断データに使用されます。 exampleRG という名前の新しいリソース グループでストレージ アカウントを作成します。

Azure Cache for Redis (アジュール・キャッシュ・フォー・レディス)

Bicep ファイルを確認する

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

@description('Specify the name of the Azure Redis Cache to create.')
param redisCacheName string = 'redisCache-${uniqueString(resourceGroup().id)}'

@description('Location of all resources')
param ___location string = resourceGroup().___location

@description('Specify the pricing tier of the new Azure Redis Cache.')
@allowed([
  'Basic'
  'Standard'
  'Premium'
])
param redisCacheSKU string = 'Standard'

@description('Specify the family for the sku. C = Basic/Standard, P = Premium.')
@allowed([
  'C'
  'P'
])
param redisCacheFamily string = 'C'

@description('Specify the size of the new Azure Redis Cache instance. Valid values: for C (Basic/Standard) family (0, 1, 2, 3, 4, 5, 6), for P (Premium) family (1, 2, 3, 4, 5)')
@allowed([
  0
  1
  2
  3
  4
  5
  6
])
param redisCacheCapacity int = 1

@description('Specify name of Built-In access policy to use as assignment.')
@allowed([
  'Data Owner'
  'Data Contributor'
  'Data Reader'
])
param builtInAccessPolicyName string = 'Data Reader'

@description('Specify name of custom access policy to create.')
param builtInAccessPolicyAssignmentName string = 'builtInAccessPolicyAssignment-${uniqueString(resourceGroup().id)}'

@description('Specify the valid objectId(usually it is a GUID) of the Microsoft Entra Service Principal or Managed Identity or User Principal to which the built-in access policy would be assigned.')
param builtInAccessPolicyAssignmentObjectId string = newGuid()

@description('Specify human readable name of principal Id of the Microsoft Entra Application name or Managed Identity name used for built-in policy assignment.')
param builtInAccessPolicyAssignmentObjectAlias string = 'builtInAccessPolicyApplication-${uniqueString(resourceGroup().id)}'

@description('Specify name of custom access policy to create.')
param customAccessPolicyName string = 'customAccessPolicy-${uniqueString(resourceGroup().id)}'

@description('Specify the valid permissions for the customer access policy to create. For details refer to https://aka.ms/redis/ConfigureAccessPolicyPermissions')
param customAccessPolicyPermissions string = '+@connection +get +hget allkeys'

@description('Specify name of custom access policy to create.')
param customAccessPolicyAssignmentName string = 'customAccessPolicyAssignment-${uniqueString(resourceGroup().id)}'

@description('Specify the valid objectId(usually it is a GUID) of the Microsoft Entra Service Principal or Managed Identity or User Principal to which the custom access policy would be assigned.')
param customAccessPolicyAssignmentObjectId string = newGuid()

@description('Specify human readable name of principal Id of the Microsoft Entra Application name or Managed Identity name used for custom policy assignment.')
param customAccessPolicyAssignmentObjectAlias string = 'customAccessPolicyApplication-${uniqueString(resourceGroup().id)}'

resource redisCache 'Microsoft.Cache/redis@2023-08-01' = {
  name: redisCacheName
  ___location: ___location
  properties: {
    enableNonSslPort: false
    minimumTlsVersion: '1.2'
    sku: {
      capacity: redisCacheCapacity
      family: redisCacheFamily
      name: redisCacheSKU
    }
    redisConfiguration: {
      'aad-enabled': 'true'
    }
  }
}

resource redisCacheBuiltInAccessPolicyAssignment 'Microsoft.Cache/redis/accessPolicyAssignments@2023-08-01' = {
  name: builtInAccessPolicyAssignmentName
  parent: redisCache
  properties: {
    accessPolicyName: builtInAccessPolicyName
    objectId: builtInAccessPolicyAssignmentObjectId
    objectIdAlias: builtInAccessPolicyAssignmentObjectAlias
  }
}

resource redisCacheCustomAccessPolicy 'Microsoft.Cache/redis/accessPolicies@2023-08-01' = {
  name: customAccessPolicyName
  parent: redisCache
  properties: {
    permissions: customAccessPolicyPermissions
  }
  dependsOn: [
    redisCacheBuiltInAccessPolicyAssignment
  ]
}

resource redisCacheCustomAccessPolicyAssignment 'Microsoft.Cache/redis/accessPolicyAssignments@2023-08-01' = {
  name: customAccessPolicyAssignmentName
  parent: redisCache
  properties: {
    accessPolicyName: customAccessPolicyName
    objectId: customAccessPolicyAssignmentObjectId
    objectIdAlias: customAccessPolicyAssignmentObjectAlias
  }
  dependsOn: [
    redisCacheCustomAccessPolicy
  ]
}

Bicep ファイルには、次のリソースが定義されています。

Azure Cache for Redis Bicep ファイルをデプロイする

  1. Bicep ファイルを main.bicep としてローカル コンピューターに保存します。

  2. Azure CLI または Azure PowerShell のどちらかを使用して Bicep ファイルをデプロイします。

    az deployment group create --resource-group exampleRG --template-file main.bicep --parameters existingDiagnosticsStorageAccountName=<storage-name> existingDiagnosticsStorageAccountResourceGroup=<resource-group>
    

    <storage-name> を、このクイックスタートの始めに作成したストレージ アカウントの名前に置き換えます。 <resource-group> を、ストレージ アカウントが置かれているリソース グループの名前に置き換えます。

    デプロイが完了すると、デプロイが成功したことを示すメッセージが表示されます。

Azure Managed Redis

Bicep ファイルを確認する

@description('Specify the name of the Azure Redis Cache to create.')
param redisCacheName string = 'redisCache-${uniqueString(resourceGroup().id)}'

@description('Location of all resources')
param ___location string = resourceGroup().___location

resource redisEnterprise 'Microsoft.Cache/redisEnterprise@2024-05-01-preview' = {
  name: redisCacheName
  ___location: ___location
  sku: {
    name: 'Balanced_B5'
  }
  identity: {
    type: 'None'
  }
  properties: {
    minimumTlsVersion: '1.2'    
  }
}

resource redisEnterpriseDatabase 'Microsoft.Cache/redisEnterprise/databases@2024-05-01-preview' = {
  name: 'default'
  parent: redisEnterprise
  properties:{
    clientProtocol: 'Encrypted'
    port: 10000
    clusteringPolicy: 'OSSCluster'
    evictionPolicy: 'NoEviction'
    persistence:{
      aofEnabled: false 
      rdbEnabled: false
    }
  }
}

Bicep ファイルには、次のリソースが定義されています。

Bicep ファイルをデプロイする

  1. Bicep ファイルを main.bicep としてローカル コンピューターに保存します。

  2. Azure CLI または Azure PowerShell のどちらかを使用して Bicep ファイルをデプロイします。

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

デプロイされているリソースを確認する

Azure portal、Azure CLI、または Azure PowerShell を使用して、リソースグループ内のデプロイ済みリソースをリスト表示します。

az resource list --resource-group exampleRG

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

不要になったら、リソース グループを削除します。これにより、リソース グループ内のリソースが削除されます。

az group delete --name exampleRG

次のステップ

このチュートリアルでは、Bicep と Azure Cache for Redis を使用してキャッシュをデプロイする方法について説明しました。 Azure Cache for Redis と Bicep の詳細については、下の記事を参照してください。