Azure Key Vault は、キー、パスワード、証明書、その他のシークレットなど、シークレットのための安全な保管場所を提供するクラウド サービスです。 このクイックスタートでは、Bicep をデプロイしてキー コンテナーとシークレットを作成する過程を中心に取り上げます。
Bicep は、宣言型の構文を使用して Azure リソースをデプロイするドメイン固有言語 (DSL) です。 簡潔な構文、信頼性の高いタイプ セーフ、およびコードの再利用のサポートが提供されます。 Bicep により、Azure のコード ソリューションとしてのインフラストラクチャに最適な作成エクスペリエンスが実現します。
前提条件
Azure サブスクリプションをお持ちでない場合は、開始する前に無料アカウントを作成してください。
Microsoft Entra ユーザーオブジェクト ID は、テンプレートによるアクセス許可の設定で必要です。 次の手順を使用してオブジェクト ID (GUID) を取得します。
[使ってみる] を選択し、シェル ウィンドウにスクリプトを貼り付けて、次の Azure PowerShell または Azure CLI コマンドを実行します。 スクリプトを貼り付けるには、シェルを右クリックし、 [貼り付け] を選択します。
echo "Enter your email address that is used to sign in to Azure:" && read upn && az ad user show --id $upn --query "objectId" && echo "Press [ENTER] to continue ..."
オブジェクト ID を書き留めます。 このクイック スタートの次のセクションで必要になります。
Bicep ファイルを確認する
このクイックスタートで使用されるテンプレートは 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 exampleRG --location eastus az deployment group create --resource-group exampleRG --template-file main.bicep --parameters keyVaultName=<vault-name> objectId=<object-id>
注意
<vault-name> は、キー コンテナーの名前に置き換えます。 <object-id> は、コンテナーに使用する AzureMicrosoft Entra テナントのユーザー、サービス プリンシパル、またはセキュリティ グループのオブジェクト ID に置き換えます。 オブジェクト ID は、アクセス ポリシーの一覧に関して一意である必要があります。 それを取得するには、Get-AzADUser または Get-AzADServicePrincipal コマンドレットを使います。
デプロイが完了すると、デプロイが成功したことを示すメッセージが表示されます。
デプロイされているリソースを確認する
Azure portal を使用し、キー コンテナーとシークレットを確認するか、次の 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 portal、Azure CLI、または Azure PowerShell を使用して、リソース グループとそのリソースを削除します。
az group delete --name exampleRG
次のステップ
このクイックスタートでは、Bicep を使ってキー コンテナーとシークレットを作成した後、デプロイを検証しました。 Key Vault と Bicep について詳しくは、引き続き以下の記事をご覧ください。
- Azure Key Vault の概要を確認する
- Bicep について詳しく学習する
- Key Vault のセキュリティの概要を確認する