Bicep 배포 스크립트에서 프라이빗 가상 네트워크에 액세스

Microsoft.Resources/deploymentScripts 버전 2023-08-01을 사용하면 몇 가지 추가 구성을 사용하여 프라이빗 네트워크에서 배포 스크립트를 실행할 수 있습니다.

  • 사용자가 할당한 관리 ID를 만들고 identity 속성에 지정합니다. ID를 할당하려면 ID를 참조하세요.

  • 프라이빗 네트워크에서 스토리지 계정을 만들고 기존 스토리지 계정을 사용하도록 배포 스크립트를 지정합니다. 자세한 내용은 기존 스토리지 계정 사용을 참조하세요. 스토리지 계정에는 몇 가지 추가 구성이 필요합니다.

    1. Azure Portal에서 스토리지 계정을 엽니다.
    2. 왼쪽 메뉴에서 액세스 제어(IAM)를 선택하고 역할 할당 탭을 선택합니다.
    3. 스토리지 파일 데이터 권한 있는 기여자 역할을 사용자 할당 관리 ID에 추가합니다.
    4. 왼쪽 메뉴의 보안 + 네트워킹에서 네트워킹을 선택한 다음, 방화벽 및 가상 네트워크를 선택합니다.
    5. 선택한 가상 네트워크 및 IP 주소에서 사용을 선택합니다.
    6. 가상 네트워크 아래에 서브넷을 추가합니다. 다음 스크린샷에서 서브넷을 dspvnVnet이라고 합니다.
    7. 예외에서 신뢰할 수 있는 서비스 목록의 Azure 서비스가 이 스토리지 계정에 액세스하도록 허용을 선택합니다.

    Screenshot of selections for configuring a storage account for accessing a private network.

다음 Bicep 파일은 배포 스크립트를 실행하기 위한 환경을 구성하는 방법을 보여 줍니다.

@maxLength(10) // Required maximum length, because the storage account has a maximum of 26 characters
param prefix string
param location string = resourceGroup().location
param userAssignedIdentityName string = '${prefix}Identity'
param storageAccountName string = '${prefix}stg${uniqueString(resourceGroup().id)}'
param vnetName string = '${prefix}Vnet'
param subnetName string = '${prefix}Subnet'

resource vnet 'Microsoft.Network/virtualNetworks@2023-05-01' = {
  name: vnetName
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    enableDdosProtection: false
    subnets: [
      {
        name: subnetName
        properties: {
          addressPrefix: '10.0.0.0/24'
          serviceEndpoints: [
            {
              service: 'Microsoft.Storage'
            }
          ]
          delegations: [
            {
              name: 'Microsoft.ContainerInstance.containerGroups'
              properties: {
                serviceName: 'Microsoft.ContainerInstance/containerGroups'
              }
            }
          ]
        }
      }
    ]
  }
}

resource subnet 'Microsoft.Network/virtualNetworks/subnets@2023-05-01' existing = {
  parent: vnet
  name: subnetName
}
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {
    networkAcls: {
      bypass: 'AzureServices'
      virtualNetworkRules: [
        {
          id: subnet.id
          action: 'Allow'
          state: 'Succeeded'
        }
      ]
      defaultAction: 'Deny'
    }
  }
}

resource userAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
  name: userAssignedIdentityName
  location: location
}

resource storageFileDataPrivilegedContributor 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
  name: '69566ab7-960f-475b-8e7c-b3118f30c6bd' // Storage File Data Privileged Contributor
  scope: tenant()
}

resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  scope: storageAccount

  name: guid(storageFileDataPrivilegedContributor.id, userAssignedIdentity.id, storageAccount.id)
  properties: {
    principalId: userAssignedIdentity.properties.principalId
    roleDefinitionId: storageFileDataPrivilegedContributor.id
    principalType: 'ServicePrincipal'
  }
}

다음 Bicep 파일을 사용하여 배포를 테스트할 수 있습니다.

param prefix string

param location string  = resourceGroup().location
param utcValue string = utcNow()

param storageAccountName string
param vnetName string
param subnetName string
param userAssignedIdentityName string

resource vnet 'Microsoft.Network/virtualNetworks@2023-05-01' existing = {
  name: vnetName

  resource subnet 'subnets' existing = {
    name: subnetName
  }
}

resource userAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
  name: userAssignedIdentityName
}

resource dsTest 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
  name: '${prefix}DS'
  location: location
  identity: {
    type: 'userAssigned'
    userAssignedIdentities: {
      '${userAssignedIdentity.id}': {}
    }
  }
  kind: 'AzureCLI'
  properties: {
    forceUpdateTag: utcValue
    azCliVersion: '2.52.0'
    storageAccountSettings: {
      storageAccountName: storageAccountName
    }
    containerSettings: {
      subnetIds: [
        {
          id: vnet::subnet.id
        }
      ]
    }
    scriptContent: 'echo "Hello world!"'
    retentionInterval: 'P1D'
    cleanupPreference: 'OnExpiration'
  }
}

다음 단계

이 문서에서는 프라이빗 가상 네트워크에 액세스하는 방법을 알아보았습니다. 자세히 알아보려면 다음을 수행합니다.