alefa240
alefa240

Reputation: 63

How to reference resources in other subscriptions with Bicep?

I use Bicep to create an Azure Function using an app service plan that was created earlier in another subscription.

I get the following error:

{"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 is only a simple bicep file, no modules, no targetScope. I refer to my app service plan by:

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

Is it not possible to reference app service plans in other subscriptions?

Upvotes: 4

Views: 6778

Answers (1)

MoonHorse
MoonHorse

Reputation: 2489

The default target scope is resourcegroup. So if you don't state a target scope, the resourcegroup scope is set implicitly. The available scopes are resource group, subscription, management group, and tenant. So if the other subscription is under the same tenant or management group, you can set the scope to either of them. For example, state in your bicep file:

targetScope = 'tenant'

And on az cli:

az deployment tenant create \
  --name demoTenantDeployment \
  --location WestUS \
  --template-file main.bicep

https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/scope-extension-resources#apply-at-deployment-scope

Upvotes: 4

Related Questions