Best practices dilemma: explicit dependencies and cleaner code or implicit dependencies with less cleaner code #12194
Replies: 1 comment
-
Hi @nelo-0, I have a suggestion for a different, and maybe a better or more readable approach. The options you are giving are okay too, but I foresee readability issues. I would go for a KISS approach. This is what I would suggest for you: Environment.bicep targetScope = 'subscription'
@allowed([
'dev'
'qa'
'prod'
])
param parEnvironment string = 'dev'
param parResourceGroupName string
param parPublicIpName string
resource resResourceGroup 'Microsoft.Resources/resourceGroups@2023-07-01' = {
name: parResourceGroupName
location: 'westeurope'
}
module modPublicIp 'publicIp.bicep' = {
name: 'deploy-pip-${parEnvironment}'
scope: resourceGroup(resResourceGroup.name)
params: {
parPublicIpName: parPublicIpName
// other module properties here
}
}
// append more modules needed for your environment Main.bicep targetScope = 'subscription'
type environmentType = {
environmentType: ('dev' | 'qa' | 'prod')
resourceGroupName: string
publicIpName: string
*: string
}[]
param parEnvironments environmentType = [
{
environmentType: 'dev'
resourceGroupName: 'rg-dev'
publicIpName: 'pip-dev'
// append other properties here
}
{
environmentType: 'qa'
resourceGroupName: 'rg-qa'
publicIpName: 'pip-qa'
// append other properties here
}
{
environmentType: 'qa'
resourceGroupName: 'rg-prod'
publicIpName: 'pip-prod'
// append other properties here
}
]
module modEnvironments 'environment.bicep' = [for environment in parEnvironments: {
name: 'deploy-environment-${environment.environmentType}'
params: {
parResourceGroupName: environment.resourceGroupName
parEnvironment: environment.environmentType
parPublicIpName: environment.publicIpName
}
}] This is what I would suggest. Simple, maintainable and scalable. Let me know if you have more questions. 💪 |
Beta Was this translation helpful? Give feedback.
-
I'm writing bicep code to deploy multiple resources with a single module file by using loops. The goal is to having one bicep solution for this (static environment). Following best practices I should avoid using explicit dependencies.
OPTION A - implicit dependencies, The scope declaration is really ugly to be honest...
OPTION B - explicit dependencies, a lot cleaner but 'what-if' doesn't detect the publicips...
Which one should I use?
Beta Was this translation helpful? Give feedback.
All reactions