title: Azure function app intermittently fails on first deployment: "Resource should exist before deployment."
body: <h2>Problem Statement</h2>
  <p>I have a pipeline which goes something like this:</p>
  <ol>
  <li>Deploy a new app service using an ARM template</li>
  <li>Deploy a zip artifact to that app service</li>
  </ol>
  <p>More often than not, the second step fails with the following message when running the pipeline for the first time:</p>
  <pre><code>##[error]Error: Resource 'my-function-app' doesn't exist. Resource should exist before deployment.
  </code></pre>
  <p>If I &quot;re-run failed jobs&quot; or simply run the pipeline again, then the pipeline will succeed. This suggests to me that this isn't an issue with the values I am providing in the pipeline; but is something to do with the timing of the second stage.</p>
  <h2>Additional Details</h2>
  <ol>
  <li>I am only deploying to a single location; North Europe</li>
  <li>I am deploying to a linux app service plan with SKU P1V3 (although I have noticed this issue with every SKU I have used to date)</li>
  <li>I am not currently using deployment slots</li>
  <li>The app service ARM template is deployed in the pipeline like this:
  <pre class="lang-yaml prettyprint-override"><code>- task: AzureResourceManagerTemplateDeployment@3
    name: DeployFunctionAppARM
    displayName: 'Deploy Function App ARM'
    inputs:
      deploymentScope: Resource Group
      azureResourceManagerConnection: ${{ variables.serviceConnection }}
      subscriptionId: ${{ variables.azureSubscription }}
      action: 'Create Or Update Resource Group'
      resourceGroupName: ${{ variables.resourceGroup }}
      location: ${{ variables.location }} # North Europe
      templateLocation: 'Linked artifact'
      csmFile: 'BuiltARMTemplates/main.json' # Built from bicep
      overrideParameters: &gt;-
        -parameter1 $(parameter1)
        -parameter2 $(parameter2)
        ...
  </code></pre>
  </li>
  <li>The artifact is then deployed like this:
  <pre class="lang-yaml prettyprint-override"><code>- task: AzureFunctionApp@1
    displayName: 'Deploy Function App'
    inputs:
      azureSubscription: ${{ variables.azureSubscription }}
      appType: functionAppLinux
      appName: 'my-funciton-app'
      package: $(Pipeline.Workspace)/**/*.zip # Matches only one zip file
  </code></pre>
  </li>
  <li>The artifact deployment is <em>definitely, 100%</em> waiting until the ARM deployment is complete before proceeding.</li>
  </ol>
  <h2>Attempted Fix</h2>
  <p>I have tried running a script between the ARM deployment and the artifact deployment that uses the Azure CLI to verify the status of the app service:</p>
  <ol>
  <li>Deploy a new app service using an ARM template</li>
  <li><em>Call Azure CLI to make sure the app service is up and running</em></li>
  <li>Deploy a zip artifact to that app service</li>
  </ol>
  <pre class="lang-sh prettyprint-override"><code>sub=&quot;${{ parameters.subscriptionId }}&quot;
  rg=&quot;${{ parameters.resourceGroup }}&quot;
  fn=&quot;${{ parameters.functionAppName }}&quot;
  expected=&quot;Running&quot;
  attempts=30 # Thirty Attempts 
  delaySeconds=10 # Gives about 5 minutes before timeout
  
  for attempt in $(seq $attempts)
  do
      # Check if function app is &quot;Running&quot;
      if [[ &quot;$(az resource show --ids /subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Web/sites/$fn | jq -r &quot;.properties.state&quot;)&quot; = &quot;$expected&quot;* ]]
      then
          echo &quot;$fn is \&quot;$expected\&quot;&quot;
          break # If the resource is running, then break the loop
      fi
  
      # Otherwise, wait before trying again
      echo &quot;[$attempt/$attempts] $fn is not currently \&quot;$expected\&quot; waiting for $delaySeconds seconds...&quot;
      sleep $delaySeconds
  done
  </code></pre>
  <p><em>Sometimes</em> this script detects that the app service hasn't been provisioned yet, so I can delay the artifact deployment.</p>
  <p>This has reduced the failure rate somewhat, but not completely. Often the Azure CLI is able to find that the app service exists and is running, but I still get the error message above when attempting the artifact deployment.</p>
  <h2>My Question</h2>
  <p>Does anybody know how to detect when an app service has been provisioned, but is not yet ready for a deployment to be made?</p>
  <p><strong>Note:</strong>
  <em>I have not posted the full pipelines or any ARM templates as they're incredibly verbose (it's a big pipeline!) and I'm not sure that they're terribly important for the question I'm asking. That said, I am very happy to provide further details if they are material to the problem at hand!</em></p>