퀵스타트: Bicep를 사용하여 Azure 키 볼트 및 비밀 만들기

Azure Key Vault 키, 암호, 인증서 및 기타 비밀과 같은 비밀에 대한 보안 저장소를 제공하는 클라우드 서비스입니다. 이 빠른 시작 가이드에서는 키 볼트 및 시크릿을 만들기 위해 Bicep 파일을 배포하는 프로세스에 중점을 둡니다.

Bicep 선언적 구문을 사용하여 Azure 리소스를 배포하는 DSL(도메인별 언어)입니다. 간결한 구문, 신뢰할 수 있는 형식 안전성 및 코드 재사용 지원을 제공합니다. Bicep Azure 코드로서의 인프라 솔루션에 가장 적합한 제작 환경을 제공합니다.

필수 조건

  • Azure 구독이 없는 경우 시작하기 전에 free 계정 만듭니다.

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 whether the key vault is a standard vault or a premium vault.')
@allowed([
  'standard'
  'premium'
])
param skuName string = 'standard'

@description('Specifies all secrets {"secretName":"","secretValue":""} wrapped in a secure object.')
@secure()
param secretsObject object

resource kv 'Microsoft.KeyVault/vaults@2023-07-01' = {
  name: keyVaultName
  location: location
  properties: {
    enabledForDeployment: enabledForDeployment
    enabledForTemplateDeployment: enabledForTemplateDeployment
    enabledForDiskEncryption: enabledForDiskEncryption
    enableRbacAuthorization: true
    tenantId: tenantId
    enableSoftDelete: true
    softDeleteRetentionInDays: 90
    enablePurgeProtection: true
    sku: {
      name: skuName
      family: 'A'
    }
    networkAcls: {
      defaultAction: 'Allow'
      bypass: 'AzureServices'
    }
  }
}

resource secrets 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = [for secret in secretsObject.secrets: {
  name: secret.secretName
  parent: kv
  properties: {
    value: secret.secretValue
  }
}]

output location string = location
output name string = kv.name
output resourceGroupName string = resourceGroup().name
output resourceId string = kv.id

Bicep 파일에는 두 개의 Azure 리소스가 정의됩니다.

Bicep 파일을 배포

  1. Bicep 파일을 main.bicep로 로컬 컴퓨터에 저장합니다.

  2. secretsObject 값을 제공하는 매개 변수 파일(예: main.parameters.json)을 만듭니다:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "keyVaultName": { "value": "<vault-name>" },
        "secretsObject": {
          "value": {
            "secrets": [
              { "secretName": "adminpassword", "secretValue": "<your-secret-value>" }
            ]
          }
        }
      }
    }
    
  3. Azure CLI 또는 Azure PowerShell을 사용하여 Bicep 파일을 배포합니다.

    az group create --name myResourceGroup --location eastus
    az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters @main.parameters.json
    

    비고

    <vault-name>vault.azure.net 네임스페이스 내에서 전역적으로 고유해야 하는 키 자격 증명 모음 이름으로 바꿉니다. <your-secret-value>를 저장할 비밀 값으로 바꾸세요. secretsObjectsecureObject(으)로 선언되었기 때문에 해당 값은 로그에 기록되거나 다시 출력되지 않습니다.

    배포가 완료되면 배포가 성공했음을 나타내는 메시지가 표시됩니다.

Key Vault RBAC 역할 할당하기

이 Bicep 파일로 생성된 Key Vault는 권한 부여에 Azure RBAC를 사용합니다. 데이터 평면을 통해 비밀에 액세스하려면(예: Azure CLI 또는 Azure PowerShell 사용) 자신에게 적절한 역할을 할당해야 합니다.

echo "Enter your key vault name:" &&
read keyVaultName &&
az role assignment create --role "Key Vault Secrets Officer" \
    --assignee-object-id $(az ad signed-in-user show --query id -o tsv) \
    --scope $(az keyvault show --name $keyVaultName --query id -o tsv)

비고

역할 할당이 전파되는 데 1~2분 정도 걸릴 수 있습니다.

배포된 리소스 검토

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

비고

리소스 그룹을 삭제하면 키 자격 증명 모음도 삭제되지만, 자격 증명 모음은 일시 삭제 상태가 되어 보존 기간(기본값: 90일) 동안 복구할 수 있는 상태로 유지됩니다. 자격 증명 모음 이름은 해당 기간 동안 전역에서 예약된 상태로 유지되며, 영구 삭제 방지가 사용하도록 설정되어 있으므로 기간이 끝나기 전에 자격 증명 모음을 영구 삭제할 수 없습니다. 표준 키 자격 증명 모음의 경우 일시 삭제된 자격 증명 모음에는 요금이 청구되지 않습니다. 자세한 내용은 Key Vault 일시 삭제 개요 참조하세요.

다음 단계

이 빠른 시작에서는 Bicep을 사용하여 키 자격 증명 모음과 비밀을 만든 후 배포의 유효성을 검사했습니다. Key Vault 및 Bicep 대해 자세히 알아보려면 아래 문서를 계속 진행하세요.