在 Bicep 模块中创建父资源时如何添加子资源

问题描述 投票:0回答:1

我有这个二头肌代码...

param location string = resourceGroup().location

module autacc './../bicepmodules/BICEP/modules/automationAccount.bicep' = {
  name: 'automationaccount'
  params : {
    applicationName: 'replen'
    environmentType: 'Development'
    tagDescription: 'test'
    userName : 'user'
    location: location
}
}

resource aa 'Microsoft.Automation/automationAccounts@2022-08-08' existing = {
  name: autacc.name 
  resource webhook1 'webhooks@2015-10-31' = {
    name: 'AutoReplen_CreateOrders_webhook'
    properties:{
        isEnabled: true
        runbook:{
        }
    }
  }

}

这导致以下错误...

 The deployment 'main3' failed with error(s). Showing 1 out of 1 error(s).
Status Message: Can not perform requested operation on nested resource. Parent resource 'automationaccount' not found. (Code:ParentResourceNotFound)

我可以通过查看门户中的部署刀片来验证此错误与“AutoReplen_CreateOrders”资源相关。

此外,因为它已经运行了多次,所以已经创建了自动化帐户,所以错误不是因为它在创建自动化帐户之前尝试创建运行手册。

我该如何解决?

要求的模块内容。

param applicationName string

@description('Location for all resources.')
param location string = resourceGroup().location

@description('Username that will be recorded in the tags of the object')
param userName string

@description('A description that will be recorded in the tags')
param  tagDescription string

param CreatedOnDate string = utcNow()

@allowed([
  'Production'
  'Test'
  'Development'
  'UAT'
]
)
@description('Environment')
param environmentType string

@description('A 3 digit number, default is 001')
@allowed([
  '001'
  '002'
  '003'
  '004'
  '005'
  '006'
  '007'
  '008'
  '009'
  'NOTSPECIFIED'
])
param instanceNumber string = 'NOTSPECIFIED'

@description('An array containing roleDefinitionid, principalId and principalType.  Pass 0 or more objects for each role assignment required.  See https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/scenarios-rbac?WT.mc_id=AZ-MVP-5003674')
param roleAssignments array = []

//param runBookName string

//param scheduleName string

var shortEnv = environmentType == 'Production' ? 'prod' : environmentType == 'Development' ? 'dev' : environmentType == 'Test' ? 'test' : 'other'
var automationAccountName = 'aa-${applicationName}-${shortEnv}-${location}${instanceNumber == 'NOTSPECIFIED' ? '' : '-${instanceNumber}'}'

resource autaccount 'Microsoft.Automation/automationAccounts@2021-06-22' = {
  name: automationAccountName
  location: location
  tags: {
    CreatedBy: userName
    CreatedOnDate : CreatedOnDate
    Description : tagDescription
    DeploymentInfo : 'deployed by Bicep module ${deployment().name}'
  }
  identity: {
    type: 'SystemAssigned'
  }
  
  properties: {
    disableLocalAuth: false
    encryption: {
      identity: {
      }
      keySource: 'Microsoft.Automation'
    }
    publicNetworkAccess: false
    sku: {
      name: 'Basic'
    } 
  }
}
azure azure-resource-manager azure-automation azure-bicep
1个回答
0
投票
  1. 从模块返回自动化账户名:
// automationAccount.bicep

...
output name string = automationAccountName
  1. 从模块创建 webhook:
// automationAccountWebhook.bicep

param automationAccountName string
param webHookName string

resource automationAccount 'Microsoft.Automation/automationAccounts@2022-08-08' existing = {
  name: automationAccountName
}

resource webhook1 'Microsoft.Automation/automationAccounts/webhooks@2015-10-31' = {
  parent : automationAccount
  name: webHookName
  properties:{
      isEnabled: true
      runbook:{
      }
  }
}
  1. 从你的主要,你可以像这样调用模块:
param location string = resourceGroup().location

module autacc './../bicepmodules/BICEP/modules/automationAccount.bicep' = {
  name: 'automationaccount'
  params : {
    applicationName: 'replen'
    environmentType: 'Development'
    tagDescription: 'test'
    userName : 'user'
    location: location
  }
}

module webhook1 './../bicepmodules/BICEP/modules/automationAccountWebhook.bicep' = {
  name: 'webhook'
  params : {
    automationAccountName: autacc.outputs.name
    webHookName: 'AutoReplen_CreateOrders_webhook'
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.