Problem
Using Microsoft bicep code, reference a parent resource name to a resource within a module.
The code in the main.bicep file is working.
# main.bicep
param apimName string = 'devApim'
param apimLocation string = 'eastus'
param publisherName string = 'danny'
param publisherEmail string = 'danny@gmail.com'
param api_display_name string = 'Test Consumer API'
param api_description = 'Test API description'
param api_versioningScheme = 'Segment'
resource devApim_resource 'Microsoft.ApiManagement/service@2021-01-01-preview' = {
name: apimName
location: apimLocation
sku: {
name: 'Developer'
capacity: 1
}
properties: {
publisherEmail: publisherEmail
publisherName: publisherName
}
}
resource test_api_vs_v1 'Microsoft.ApiManagement/service/apiVersionSets@2021-01-01-preview' = {
// Below reference to first/parent resource is working fine as it's in the same bicep file.
parent: devApim_resource
name: 'test_api_vs_name'
properties: {
displayName: api_display_name
description: api_description
versioningScheme: api_versioningScheme
}
}
I’d like to turn the second resource (VersionSet resource) in main.bicep into a module, similar to the files below.
# main.bicep
param apimName string = 'devApim'
param apimLocation string = 'eastus'
param publisherName string = 'danny'
param publisherEmail string = 'danny@gmail.com'
param api_display_name string = 'Test Consumer API'
param api_description = 'Test API description'
param api_versioningScheme = 'Segment'
resource devApim_resource 'Microsoft.ApiManagement/service@2021-01-01-preview' = {
name: apimName
location: apimLocation
sku: {
name: 'Developer'
capacity: 1
}
properties: {
publisherEmail: publisherEmail
publisherName: publisherName
}
}
module test_api_module 'test-api.bicep' = {
name: 'test_api'
params: {
api_display_name: api_display_name
api_description: api_description
api_versioningScheme: api_versioningScheme
}
}
# test-api.bicep file
param api_display_name string
param api_description string
param api_versioningScheme string
resource test_api_vs_v1 'Microsoft.ApiManagement/service/apiVersionSets@2021-01-01-preview' = {
// Below reference to first/parent resource is not working.
//parent: devApim_resource
name: 'test_api_vs_name'
properties: {
displayName: api_display_name
description: api_description
versioningScheme: api_versioningScheme
}
}
Now, how do I use parent:devApim resource to reference/pass parent resource ‘devApim resource’ (first resource) into module resource test api vs v1 (second resource)? In the test-api.bicep module file, devApim resource does not work.
I’m a complete beginner when it comes to bicep code.
Asked by devops-admin
Solution #1
For further information, see the following documentation:
In your child module, you must include the parent resource name as a parameter:
param apimName string
The simpler way is to use the parent: prefix to create the child resource name.
resource test_api_vs_v1 'Microsoft.ApiManagement/service/apiVersionSets@2021-01-01-preview' = {
name: '${apimName}/test_api_vs_name'
...
}
Alternatively, you may use an existing resource such as this:
// Reference to the parent resource
resource devApim_resource 'Microsoft.ApiManagement/service@2021-01-01-preview' existing = {
name: apimName
}
resource test_api_vs_v1 'Microsoft.ApiManagement/service/apiVersionSets@2021-01-01-preview' = {
parent: devApim_resource
name: 'test_api_vs_name'
...
}
Then you may refer to yourself as the chil module in your main.bicep:
module test_api_module 'test-api.bicep' = {
name: 'test_api'
params: {
apimName: devApim_resource.name
api_display_name: api_display_name
api_description: api_description
api_versioningScheme: api_versioningScheme
}
}
Answered by Thomas
Post is based on https://stackoverflow.com/questions/69245544/how-to-reference-a-parent-resource-name-to-a-resource-inside-a-module-using-bice