빠른 시작: Bicep 모듈을 프라이빗 모듈 레지스트리에 게시

Bicep 모듈을 프라이빗 모듈 레지스트리에 게시하는 방법과 Bicep 파일에서 모듈을 호출하는 방법을 알아봅니다. 프라이빗 모듈 레지스트리를 사용하면 조직 내에서 Bicep 모듈을 공유할 수 있습니다. 자세한 내용은 Bicep 모듈용 프라이빗 레지스트리 만들기를 참조하세요. 공용 모듈 레지스트리에 기여하려면 기여 가이드를 참조하세요.

필수 조건

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

모듈 레지스트리를 사용하려면 Bicep CLI 버전 0.4.1008 이상이 있어야 합니다. Azure CLI와 함께 사용하려면 Azure CLI 버전 2.31.0 이상도 있어야 합니다. Azure PowerShell과 함께 사용하려면 Azure PowerShell 버전 7.0.0 이상도 있어야 합니다.

Bicep 레지스트리는 ACR(Azure Container Registry)에 호스트됩니다. 생성하려면 빠른 시작: Bicep 파일을 사용하여 컨테이너 레지스트리 만들기를 참조하세요.

Bicep 개발 환경을 설정하려면 Bicep 도구 설치를 참조하세요. 관련 단계를 완료한 후 Visual Studio CodeBicep 확장 또는 Visual StudioBicep 확장을 사용할 수 있습니다.

Bicep 모듈 만들기

모듈은 다른 Bicep 파일에서 배포되는 Bicep 파일입니다. 모든 Bicep 파일을 모듈로 사용할 수 있습니다. 이 빠른 시작에서는 다음 Bicep 파일을 사용할 수 있습니다. 스토리지 계정을 만듭니다.

@minLength(3)
@maxLength(11)
param storagePrefix string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Standard_ZRS'
  'Premium_LRS'
  'Premium_ZRS'
  'Standard_GZRS'
  'Standard_RAGZRS'
])
param storageSKU string = 'Standard_LRS'
param location string

var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'

resource stg 'Microsoft.Storage/storageAccounts@2021-06-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

output storageEndpoint object = stg.properties.primaryEndpoints

Bicep 파일을 storage.bicep으로 저장합니다.

모듈 게시

ACR(Azure Container Registry)이 없는 경우 필수 구성 요소를 참조하여 만들 수 있습니다. ACR의 로그인 서버 이름이 필요합니다. 로그인 서버 이름의 형식은 <registry-name>.azurecr.io입니다. 로그인 서버 이름을 가져오려면 다음을 수행합니다.

az acr show --resource-group <resource-group-name> --name <registry-name> --query loginServer

다음 구문을 사용하여 Bicep 파일을 프라이빗 모듈 레지스트리에 모듈로 게시합니다.

az bicep publish --file storage.bicep --target br:exampleregistry.azurecr.io/bicep/modules/storage:v1 --documentationUri https://www.contoso.com/exampleregistry.html

이전 샘플에서 ./storage.bicep은 게시할 Bicep 파일입니다. 필요에 따라 파일 경로를 업데이트합니다. 모듈 경로에는 다음 구문이 있습니다.

br:<registry-name>.azurecr.io/<file-path>:<tag>
  • br은 Bicep 레지스트리의 스키마 이름입니다.
  • 파일 경로는 Azure Container Registry에서 repository라고 합니다. 파일 경로에는 / 문자로 구분된 세그먼트가 포함되어 있습니다. 이 파일 경로는 레지스트리에 없는 경우 생성됩니다.
  • 태그는 모듈의 버전을 지정하는 데 사용됩니다.

게시된 모듈을 확인하기 위해 ACR 리포지토리를 나열할 수 있습니다.

az acr repository list --name <registry-name> --output table

모듈 호출

모듈을 호출하려면 Visual Studio Code에서 새 Bicep 파일을 만듭니다. 새 Bicep 파일에서 다음 줄을 입력합니다.

module stgModule 'br:<registry-name>.azurecr.io/bicep/modules/storage:v1'

<registry-name>을 ACR 레지스트리 이름으로 바꿉니다. 모듈을 로컬 캐시로 복원하는 데 잠시 시간이 소요됩니다. 모듈이 복원되면 모듈 경로 아래에 있는 빨간색 곱슬선이 사라집니다. 줄 끝에 = 및 공백을 추가한 후, 다음 스크린샷과 같이 필수 속성을 선택합니다. 모듈 구조체가 자동으로 채워집니다.

Visual Studio Code Bicep extension required properties

다음 예제는 완료된 Bicep 파일입니다.

@minLength(3)
@maxLength(11)
param namePrefix string
param location string = resourceGroup().location

module stgModule 'br:ace1207.azurecr.io/bicep/modules/storage:v1' = {
  name: 'stgStorage'
  params: {
    location: location
    storagePrefix: namePrefix
  }
}

Bicep 파일을 로컬로 저장한 다음, Azure CLI 또는 Azure PowerShell을 사용하여 Bicep 파일을 배포합니다.

resourceGroupName = "{provide-a-resource-group-name}"
templateFile="{provide-the-path-to-the-bicep-file}"

az group create --name $resourceGroupName --location eastus

az deployment group create --resource-group $resourceGroupName --template-file $templateFile

Azure Portal에서 스토리지 계정이 성공적으로 생성되었는지 확인합니다.

리소스 정리

Azure 리소스가 더 이상 필요 없으면 Azure CLI 또는 Azure PowerShell 모듈을 사용하여 빠른 시작 리소스 그룹을 삭제합니다.

resourceGroupName = "{provide-the-resource-group-name}"

az group delete --name $resourceGroupName

다음 단계