Azure Key Vault は、キー、パスワード、証明書、その他のシークレットなどのシークレットのセキュリティで保護されたストアを提供するクラウド サービスです。 このクイック スタートでは、Bicep ファイルをデプロイしてキー コンテナーとシークレットを作成するプロセスについて説明します。
Bicep は、宣言型構文を使用してAzure リソースをデプロイするドメイン固有言語 (DSL) です。 簡潔な構文、信頼性の高いタイプ セーフ、およびコードの再利用のサポートが提供されます。 Bicepは、Azureのコードとしてのインフラストラクチャ ソリューションに最適な作成エクスペリエンスを提供します。
[前提条件]
Azure サブスクリプションがない場合は、開始する前に free アカウントを作成します。
アクセス許可を構成するには、テンプレートでMicrosoft Entraユーザー オブジェクト ID が必要です。 次の手順では、オブジェクト ID (GUID) を取得します。
次のAzure PowerShellまたはAzure CLIコマンドTry it を選択して実行し、スクリプトをシェル ウィンドウに貼り付けます。 スクリプトを貼り付けるには、シェルを右クリックし、[貼り付け] を選択します。
echo "Enter your email address that is used to sign in to Azure:" && read upn && az ad user show --id $upn --query "id" && echo "Press [ENTER] to continue ..."オブジェクト ID を書き留めます。 このクイック スタートの次のセクションで必要になります。
Bicep ファイルを確認する
Important
このクイックスタートでは、レガシーアクセス ポリシーを使用してボールトを作成する外部テンプレートを使用します。 運用環境へのデプロイでは、Azure RBAC 承認を使用してください。
このクイック スタートで使用するテンプレートは、Azure クイック スタート テンプレートから取得します。
@description('Specifies the name of the key vault.')
param keyVaultName string
@description('Specifies the Azure location where the key vault should be created.')
param location string = resourceGroup().location
@description('Specifies whether Azure Virtual Machines are permitted to retrieve certificates stored as secrets from the key vault.')
param enabledForDeployment bool = false
@description('Specifies whether Azure Disk Encryption is permitted to retrieve secrets from the vault and unwrap keys.')
param enabledForDiskEncryption bool = false
@description('Specifies whether Azure Resource Manager is permitted to retrieve secrets from the key vault.')
param enabledForTemplateDeployment bool = false
@description('Specifies the Azure Active Directory tenant ID that should be used for authenticating requests to the key vault. Get it by using Get-AzSubscription cmdlet.')
param tenantId string = subscription().tenantId
@description('Specifies the object ID of a user, service principal or security group in the Azure Active Directory tenant for the vault. The object ID must be unique for the list of access policies. Get it by using Get-AzADUser or Get-AzADServicePrincipal cmdlets.')
param objectId string
@description('Specifies the permissions to keys in the vault. Valid values are: all, encrypt, decrypt, wrapKey, unwrapKey, sign, verify, get, list, create, update, import, delete, backup, restore, recover, and purge.')
param keysPermissions array = [
'list'
]
@description('Specifies the permissions to secrets in the vault. Valid values are: all, get, list, set, delete, backup, restore, recover, and purge.')
param secretsPermissions array = [
'list'
]
@description('Specifies whether the key vault is a standard vault or a premium vault.')
@allowed([
'standard'
'premium'
])
param skuName string = 'standard'
@description('Specifies the name of the secret that you want to create.')
param secretName string
@description('Specifies the value of the secret that you want to create.')
@secure()
param secretValue string
resource kv 'Microsoft.KeyVault/vaults@2023-07-01' = {
name: keyVaultName
location: location
properties: {
enabledForDeployment: enabledForDeployment
enabledForDiskEncryption: enabledForDiskEncryption
enabledForTemplateDeployment: enabledForTemplateDeployment
tenantId: tenantId
enableSoftDelete: true
softDeleteRetentionInDays: 90
accessPolicies: [
{
objectId: objectId
tenantId: tenantId
permissions: {
keys: keysPermissions
secrets: secretsPermissions
}
}
]
sku: {
name: skuName
family: 'A'
}
networkAcls: {
defaultAction: 'Allow'
bypass: 'AzureServices'
}
}
}
resource secret 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = {
parent: kv
name: secretName
properties: {
value: secretValue
}
}
output location string = location
output name string = kv.name
output resourceGroupName string = resourceGroup().name
output resourceId string = kv.id
Bicep ファイルには、次の 2 つのAzure リソースが定義されています。
- Microsoft.KeyVault/vaults: Azureキー ボールトを作成します。
- Microsoft。KeyVault/vaults/secrets: キー コンテナー シークレットを作成します。
Bicep ファイルをデプロイする
Bicep ファイルを main.bicep として、ローカル コンピューターに保存します。
Azure CLIまたはAzure PowerShellを使用して、Bicep ファイルをデプロイします。
az group create --name myResourceGroup --location eastus az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters keyVaultName=<vault-name> objectId=<object-id>注
<vault-name>は、キー コンテナーの名前に置き換えます。<object-id>を、コンテナーが属する Microsoft Entra テナント内のユーザー、サービスプリンシパル、またはセキュリティグループのオブジェクト ID に置き換えます。 オブジェクト ID は、アクセス ポリシーの一覧に関して一意である必要があります。 Get-AzADUser または Get-AzADServicePrincipal コマンドレットを使用して取得します。デプロイが完了すると、デプロイが成功したことを示すメッセージが表示されます。
デプロイされているリソースを確認する
Azure ポータルを使用してキー コンテナーとシークレットを確認するか、次のAzure CLIまたはAzure PowerShellスクリプトを使用して作成されたシークレットを一覧表示できます。
echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault secret list --vault-name $keyVaultName &&
echo "Press [ENTER] to continue ..."
リソースをクリーンアップする
不要になったら、Azure ポータル、Azure CLI、またはAzure PowerShellを使用して、リソース グループとそのリソースを削除します。
az group delete --name myResourceGroup
次のステップ
このクイック スタートでは、Bicepを使用してキー コンテナーとシークレットを作成し、デプロイを検証しました。 Key VaultとBicepの詳細については、以下の記事に進んでください。
- Azure Key Vault のオーバービューをご覧ください
- Bicepについて詳しく知る
- Key Vault セキュリティの概要 を確認します>