top of page

LetsDevOps: Azure Bicep - How to reference an existing resource in bicep.

Introduction

In this blog we will learn how to reference an existing resource in bicep.


What is Existing resource

Assume you already have an existing resource created in azure and you want to refer in your current bicep file. Like While creating a webApp you want to assign already created AppSevicePlan etc.


existing Keyword

existing keyword help to declare already created resource. When resource is referenced with existing keyword it does not redeploy.


Architecture


Syntax


resource <resource-symbolic-name> '<resource-type>@<api-version>' existing =
{
  name: '<name of the Resource>'
}

Scope

It is also important to understand the scope of the existing resource. It helps to access the resource from same or different scope. Scope can be defined for Resource Group, Subscription, Management Group, Tenant.

  1. Same Scope

  2. Different Scope

Same Scope

Assume you are going to create web app in the Resource Group demo and you want to refer already existing app service plan which is also in same Resource Group demo then in this case existing resource is in same scope.


resource funcAppServicePlan 'Microsoft.Web/serverfarms@2021-03-01' existing = {
  name: webAppServicePlanName
}

Different Scope [resource group]

Assume you are going to create web app in the Resource Group demo and you want to refer already existing app service plan which exist in different Resource Group aspdemo then in this case existing resource is in different scope.


resource funcAppServicePlan 'Microsoft.Web/serverfarms@2021-03-01' existing = {
  name: webAppServicePlanName
  scope: resourceGroup(aspdemo)
}

Different Scope [Subscription]


resource funcAppServicePlan 'Microsoft.Web/serverfarms@2021-03-01' existing = {
  name: webAppServicePlanName
  scope: subscription(subscriptionId)
}

GitHub Bicep Code



Demo












Comments


bottom of page