Simon Elms
Simon Elms

Reputation: 19808

Azure Pipeline Deployment to App Service fails: "Resource doesn't exist. Resource should exist before deployment". But App Service is running

I'm doing the Microsoft tutorial "Create Multi-stage Pipeline", exercise "Promote to Dev Stage": https://learn.microsoft.com/en-us/learn/modules/create-multi-stage-pipeline/4-promote-dev

This involves creating an Azure App Service, in my case named "tailspin-space-game-web-dev-4960.azurewebsites.net", and a multi-stage pipeline in Azure DevOps to build a web application project and deploy it to the App Service.

When I go to run the pipeline the deployment stage fails with the following message in the log:

Error: Resource 'tailspin-space-game-web-dev-4960.azurewebsites.net' doesn't exist. Resource should exist before deployment.

I've copied and pasted the URL http://tailspin-space-game-web-dev-4960.azurewebsites.net into another browser window to confirm the App Service exists and is running (although it's currently just showing the default app page since I haven't been able to deploy my application to it).

My question is: How do I test the service connection in Azure DevOps that should connect to the App Service? Alternatively, how do I debug the issue?

I haven't been able to find anything online about this error, and the only debugging info from Microsoft is about errors in creating service connections, not using them. I also can't find anything about testing service connections - the only advice I've found is to run them in the pipeline, which is a bit circular since that is where the error is occurring.

I've turned on verbose logging on the pipeline. This is the log from the failing deployment task (blank line added to highlight where it's attempting to get connection details):

##[debug]system.culture=en-US
##[debug]check path : /home/vsts/work/_tasks/AzureWebApp_18bde28a-8172-45cb-b204-5cef1393dbb1/1.163.2/node_modules/azurermdeploycommon/module.json
##[debug]adding resource file: /home/vsts/work/_tasks/AzureWebApp_18bde28a-8172-45cb-b204-5cef1393dbb1/1.163.2/node_modules/azurermdeploycommon/module.json
##[debug]system.culture=en-US
##[debug]azureSubscription=5d6d337f-3ceb-4a1d-8077-79e8c462faaf
##[debug]appType=null
##[debug]deployToSlotOrASE=false
##[debug]customWebConfig=null
##[debug]appSettings=null
##[debug]startUpCommand=null
##[debug]configurationStrings=null
##[debug]resourceGroupName=null
##[debug]slotName=production
##[debug]appName=tailspin-space-game-web-dev-4960.azurewebsites.net
##[debug]customDeployFolder=null
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf auth scheme = ServicePrincipal
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data subscriptionid = 7258ca03-764d-4d8a-94e2-1f12bcd06be1
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data subscriptionname = Visual Studio Premium with MSDN
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf auth param serviceprincipalid = ***
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data environmentAuthorityUrl = https://login.windows.net/
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf auth param tenantid = ***
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf=https://management.azure.com/
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data environment = AzureCloud
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf auth scheme = ServicePrincipal
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data msiclientId = undefined
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data activeDirectoryServiceEndpointResourceId = https://management.core.windows.net/
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data AzureKeyVaultServiceEndpointResourceId = https://vault.azure.net
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data AzureKeyVaultDnsSuffix = vault.azure.net
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf auth param authenticationType = ***
##[debug]credentials spn endpoint
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf auth param serviceprincipalkey = ***
##[debug]5d6d337f-3ceb-4a1d-8077-79e8c462faaf data EnableAdfsAuthentication = false
##[debug]{"subscriptionID":"7258ca03-764d-4d8a-94e2-1f12bcd06be1","subscriptionName":"Visual Studio Premium with MSDN","servicePrincipalClientID":"***","environmentAuthorityUrl":"https://login.windows.net/","tenantID":"***","url":"https://management.azure.com/","environment":"AzureCloud","scheme":"ServicePrincipal","activeDirectoryResourceID":"https://management.azure.com/","azureKeyVaultServiceEndpointResourceId":"https://vault.azure.net","azureKeyVaultDnsSuffix":"vault.azure.net","authenticationType":"***","servicePrincipalKey":***,"isADFSEnabled":false,"applicationTokenCredentials":{"clientId":"***","domain":"***","baseUrl":"https://management.azure.com/","authorityUrl":"https://login.windows.net/","activeDirectoryResourceId":"https://management.azure.com/","isAzureStackEnvironment":false,"authType":"***","secret":***,"isADFSEnabled":false}}

Got service connection details for Azure App Service:'tailspin-space-game-web-dev-4960.azurewebsites.net'
##[debug][POST]https://login.windows.net/***/oauth2/token/
##[debug][GET]https://management.azure.com/subscriptions/7258ca03-764d-4d8a-94e2-1f12bcd06be1/resources?$filter=resourceType EQ 'Microsoft.Web%2FSites' AND name EQ 'tailspin-space-game-web-dev-4960.azurewebsites.net'&api-version=2016-07-01
##[debug]Correlation ID from ARM api call response : 37546212-3807-41bf-8985-9c811d0f9c75
##[debug]Deployment Failed with Error: Error: Resource 'tailspin-space-game-web-dev-4960.azurewebsites.net' doesn't exist. Resource should exist before deployment.
##[debug]task result: Failed
##[error]Error: Resource 'tailspin-space-game-web-dev-4960.azurewebsites.net' doesn't exist. Resource should exist before deployment.
##[debug]Processed: ##vso[task.issue type=error;]Error: Resource 'tailspin-space-game-web-dev-4960.azurewebsites.net' doesn't exist. Resource should exist before deployment.
##[debug]Processed: ##vso[task.complete result=Failed;]Error: Resource 'tailspin-space-game-web-dev-4960.azurewebsites.net' doesn't exist. Resource should exist before deployment.
##[debug]Deployment failed

Upvotes: 22

Views: 37944

Answers (14)

Stinky Towel
Stinky Towel

Reputation: 778

I got the same error when attempting to deploy a new function app. Since my function app was already setup in Azure and running, the error message was not helpful. I re-ran my build/deploy pipeline in Azure Devops with System Diagnostics enabled. This provided more detail and I found the following warning:

##[debug]ZIP Deploy response: {"statusCode":403,"statusMessage":"Ip Forbidden"...

I went back to my Azure Function in Azure portal and enabled public network access with restrictions and added the Azure Devops outbound IPs as allowed on my function, see https://learn.microsoft.com/en-us/azure/devops/organizations/security/allow-list-ip-url?view=azure-devops&tabs=IP-V4#ip-addresses-and-range-restrictions

Next deploy worked!

Upvotes: 1

Rafa_Arcangel
Rafa_Arcangel

Reputation: 31

I had the same problem in the past.

From the Microsoft page https://learn.microsoft.com/en-us/troubleshoot/azure/devops/resource-not-exist-error-services-deployment, they say:

enter image description here

Finally, I solved purging the azure cache with a command in a powershell task like:

- task: PowerShell@2
  displayName: "Purge Cache"
  inputs:
    targetType: 'inline'
    script: |
      az cache purge

Upvotes: 2

Teshan
Teshan

Reputation: 381

Check the service connection configured with correct subscription, then check function app available under drop down options.

for example, if your service connection created with dev subscription, when you deploy to prod subscription with same service connection this might occur.

enter image description here

Upvotes: 0

Amila mendis
Amila mendis

Reputation: 23

Set the WEBSITE_RUN_FROM_PACKAGE = 1 and it worked for me

Upvotes: 0

kadakoglucigdem
kadakoglucigdem

Reputation: 26

It should not be a problem if you specify the deployment slot, in my experience.

Upvotes: 0

Juan Carlos
Juan Carlos

Reputation: 596

There is a cache issue on newly created resources in azure that shows a misleading error: "Resource should exist before deployment", but if you wait > 10 minutes and retry it will be fine.

As you may see on the attached image, I have not done any code changes as the commit hashes are all the same. I just kept trying until it worked. I've been having this issue for a month.

So my suggestion before doing any configuration changes is just to wait and retry several times.

enter image description here enter image description here

** UPDATE Nov 2022 ** Looks like the issue is not present (for me) anymore

Upvotes: 2

Denis Olivares
Denis Olivares

Reputation: 21

Not having the resources appearing in the "List resources" option indeed indicates a connection problem.

What solved the problem in my case was to update the service connection and specify the "Resource group" associated with the connection.

  • Azure App Service deploy
  • In front of "Azure subscription" click "Manage"
  • In the Overview page of your service connection, click "Edit"
  • Select the desired Resource Group and save it.

After doing this, the resources started to show up in the resources list.

Upvotes: 2

Val
Val

Reputation: 31

I created the app service plan as F1 (Free) during the exercise, instead of B1 as specified in the exercise. I also got the resource not found error. Once I deleted all and recreated as B1, problem miraculously disappeared!

Upvotes: 3

Lukas Gorniak
Lukas Gorniak

Reputation: 41

Encountered the same resource issue while https://learn.microsoft.com/en-gb/learn/modules/create-multi-stage-pipeline

Lead me to: Resource should exist before deployment. Cannot read property 'getApplicationURL' of undefined issue on Github https://github.com/microsoft/azure-pipelines-tasks/issues/15532

Information found there:

latest update: The ARM team does not have a quick fix on this issue.

SOS Replication Delays Root Cause Statement

Azure is a multi-regional product, where relative information of resources and their state needs to be replicated across multiple regions. Essentially, while the resource data itself resides in the selected region, metadata related to managing the resource, such as the resource id and its data location, is replicated across the various regions. This allows Azure to accept a resource management request from anywhere in the world and route the request to the closest region based on latency. This selection process is independent of the HTTP request body; even if the request has the notion of a 'location' associated with it. Through this model, we’re able to provide better performance for Azure’s management layer from any location while keeping the data securely stored in the configured region. However, our investigation discovered that the issue you encountered was caused by a transient delay in the replication flow of resource metadata between Azure’s {source region} and {destination region} regions. We recognize that this is as a problem, and we are continuously working on improving the platform. The Azure Resource Manager (ARM) team is actively iterating on significant improvements to the replication flows and mechanisms, including large-scale enhancements of our replication architecture. Meanwhile, we recommend that you use Azure Resource Graph whenever possible for resource list calls. Azure Resource Graph offers a one-minute SLO and is designed to performantly handle list calls at scale.

Work around:

So, we have to look for a work around. Since Azure App service deploy tasks checks the existence of the resource before deploying and this is where the issue is happening. We have requested the App service team to provide us the az cli/powershell commands to deploy the packages to app services so that you can implement this workaround until we have a permanent fix from the ARM team.

Below are the useful links to carry out the deployment using the cli:

Run your app in Azure App Service directly from a ZIP package: https://learn.microsoft.com/en-us/azure/app-service/deploy-run-package

Automate resource deployment for your function app in Azure Functions: https://learn.microsoft.com/en-us/azure/azure-functions/functions-infrastructure-as-code?tabs=windows

Deploy resources with ARM templates and Azure PowerShell: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-powershell

How to use Azure Resource Manager (ARM) deployment templates with Azure CLI: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-cli

Upvotes: 4

jspinella
jspinella

Reputation: 2423

You'll also get this error if the service principal tied to the Azure DevOps Service Connection you're trying to use does not have adequate permissions for the App Service in question. I gave it the Contributor role which is probably more than it needs to be but... not much more.

At least nowadays in the Azure Pipelines pipeline editor, for your "Deploy Azure App Service" step, there is a list of resources you can select under "App Service Name" that populates with all the available app services that the Azure DevOps Service Connection has permission to access. If your app service isn't in that dropdown list, you will get the error in question. It's a hint that you probably need to go to the app service and give the service principal DevOps is using a role that will allow it permission to deploy the app service.

Upvotes: 23

Roman Kiprin
Roman Kiprin

Reputation: 19

Just add 'xargs echo' to remove quotes:

WebAppNameDev=$(terraform output appservice_name_dev | xargs echo)

Upvotes: 1

Rachel
Rachel

Reputation: 1296

I got this error when triggering the Function App deployment immediately after deploying the App Service to Azure. After waiting 10 minutes and re-running the pipeline manually it worked.

Upvotes: 25

Mengdi Liang
Mengdi Liang

Reputation: 19036

xxxx.azurewebsites.net is just the URL that used to direct to your app in browser.

.azurewebsites.net is the default address automatic attached after app name by azure service. The complete address format we construct is <web app name>.azurewebsites.net. This is fixed, and would not be affected to change by anything.


Another experience I want to share is, no matter for api or cli(if you make use), the Name blank you input should all be the contents you configured while create the app service. For example,

enter image description here

Upvotes: 4

Simon Elms
Simon Elms

Reputation: 19808

Found the issue. I was using the wrong appName in the AzureWebApp@1 task in the pipeline.

I didn't notice that the App Service name was slightly different from the name that appears in the URL.

App Service name: tailspin-space-game-web-dev-4960

Name in URL: tailspin-space-game-web-dev-4960.azurewebsites.net

I was using the name from the URL in the appName. I should have been using the App Service name.

Upvotes: 5

Related Questions