Search code examples
azureazure-bicep

How to do it in Bicep: Create a Resource if another exists


I wrote a bicep template to create an Event Grid subscription for an Azure function. Here is my attempt so far.

I want to put a condition to only create this subscription if a specific azure function exist.

  resource azureFunctionApp 'Microsoft.Web/sites@2022-03-01' existing = 
  {
     name: funcationAppName
  }


  resource azureFunction1 'Microsoft.Web/sites/functions@2022-03-01' existing = {
    parent: azureFunctionApp
    name: azureFunction1Name
  }

  var functionExist = contains(azureFunction1 .name, azureFunction1Name) == 'true'

  resource AzureFunctionEventGridSubscription 'Microsoft.EventGrid/eventSubscriptions@2022-06-15' = if(functionExist)  {
   //do a bunch of stuff
  }

However, when I deploy this bicep on Azure, I am still getting error saying resource 'azureFunction1Name' does not exist. How can I skipped creating this AzureFunctionEventGridSubscription and have this skipped and execute without errors?


Solution

  • That does not work in Bicep. The existing keyword can only refer to, well, existing resources.

    You will need to build this logic around your Bicep deployment and only trigger the deployment if you actually want something to be deployed.