Coder Perfect

How can I use Bicep to reference resources from other subscriptions?

Problem

Bicep is used to generate an Azure Function from an app service plan that was previously developed under a different subscription.

The following error appears:

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"NotFound","message":"{\r\n  \"Code\": \"NotFound\",\r\n  \"Message\": \"Cannot find ServerFarm with name asp-intsharedep-prod.\",\r\n  \"Target\": null,\r\n  \"Details\": [\r\n    {\r\n      \"Message\": \"Cannot find ServerFarm with name myasp.\"\r\n    },\r\n    {\r\n      \"Code\": \"NotFound\"\r\n    },\r\n    {\r\n      \"ErrorEntity\": {\r\n        \"ExtendedCode\": \"51004\",\r\n        \"MessageTemplate\": \"Cannot find {0} with name {1}.\",\r\n        \"Parameters\": [\r\n          \"ServerFarm\",\r\n          \"myasp\"\r\n        ],\r\n        \"Code\": \"NotFound\",\r\n        \"Message\": \"Cannot find ServerFarm with name myasp.\"\r\n      }\r\n    }\r\n  ],\r\n  \"Innererror\": null\r\n}"}]}}

It’s just a plain bicep file with no modules or targetScope. My app service plan is referred to as:

resource serverFarm 'Microsoft.Web/serverfarms@2020-06-01'  existing = {
  name: aspName
  scope: resourceGroup('subscriptionid','rg-shared-asp')
}

Is it feasible to use app service plans as a reference in other subscriptions?

Asked by alefa240

Solution #1

Resourcegroup is the default target scope. The resourcegroup scope is set implicitly if you don’t specify a target scope. Resource group, subscription, management group, and tenant are the scopes that are available. You can set the scope to either the other subscription or the tenancy or management group if they are both under the same tenant or management group. For instance, in your biceps file, write:

targetScope = 'tenant'

On the subject of az cli:

az deployment tenant create \
  --name demoTenantDeployment \
  --location WestUS \
  --template-file main.bicep
https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/scope-extension-resources#apply-at-deployment-scope

Answered by MoonHorse

Post is based on https://stackoverflow.com/questions/69946426/how-to-reference-resources-in-other-subscriptions-with-bicep