Azure / Azure/bicep

For loop Iteration and Conditions together is not supported in resource property block

Open
#7,273 0 comments 0 reactions 0 assignees View on GitHub
revisit
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 21m
Merged PRs (30d)
79

Description

**Bicep version**
Bicep CLI version 0.4.1124

**Describe the bug**

Hello Team, We have a elite customer requirement to create re-usable modules using Azure Bicep where we will be consuming Bicep files and parameter files to deploy Infrastructure components.

We have got a requirement where we plan to get rid of the key value pairs where it's not required in parameter file by using contains function, We evaluate like below for a NAT gw bicep snippet.

```bicep
@description('NAT Gateway input parameters')
param natgwValues object

resource nwgateway 'Microsoft.Network/natGateways@2021-05-01' = {
name: natgwValues.name
location: natgwValues.location
**tags: contains(natgwValues, 'tags') ? natgwValues.tags : null**
```

In below case customer is allowed to add tags in parameter file or if they have not provided, it will evaluate this condition & doesn't hold the deployment.
When we grilled down more deeper, We have added for loop in the Nat gw module like below so that it can support adding multiple public IP for the customer.

```bicep
properties: {
idleTimeoutInMinutes: contains(natgwValues, 'idleTimeoutInMinutes') ? natgwValues.idleTimeoutInMinutes : 4
**publicIpAddresses: [for pip in natgwValues.publicIpAddresses: {
id: resourceId(pip.publicIpRgName, 'Microsoft.Network/publicIPAddresses', pip.publicIpAddressName)
}]**
```

Now we would like you expert advise to suggest how we can add condition and iteration together for a used case like above.
I have tried leveraging variables something like below->

```bicep
**var PublicIpAddresses = [for publicipaddress in natgwValues.PublicIpAddresses: {
id: resourceId(publicipaddress.publicIpRgName, 'Microsoft.Network/publicIPAddresses', publicipaddress.publicIpAddressName)
}]**
```

and add evaluation under property block
```bicep
****publicIpAddresses: contains(natgwValues, 'PublicIpAddresses') ? PublicIpAddresses : null**
```

But still I have to add the publicIpaddresses in my parameter json & pass it as [] since this condition is getting evaluated on variable.

We would really appreciate your response in this regard and kindly suggest us any workaround or solution in this matter.

**To Reproduce**

I can help in reproducing this issue.

**Additional context**
Add any other context about the problem here.

Contributor guide

Open the contributing guide

Research direction

Start with the NAT gateway resource and parameter examples in the issue, reproducing the interaction between the for loop and the contains-based condition. Done means a resource property can safely omit the optional public IP input while supporting iteration when it is supplied; the issue names no repository files or tests to guide implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.