Skip to content

Commit 1366403

Browse files
authored
Existing resource example (#18)
main.bicep template README.md explanation Typo on repo repo fixed
1 parent 4ee3b5c commit 1366403

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you find this repository useful, please save the repository by hitting the
1919
- Install [AzureCLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli) **[recommended method]** or
2020
- [Azure PowerShell](https://learn.microsoft.com/en-us/powershell/azure/install-azure-powershell?view=azps-10.3.0) (must install [Bicep manually](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install#install-manually) if you want to use with PowerShell)
2121

22-
4. [Fork](https://github.com/riosengineer/Bicepify/fork) this repository so you have a copy to lab with our examples
22+
4. [Fork](https://github.com/riosengineer/Bicepify/fork) this repository so you have a copy to lab with the examples
2323

2424
5. In your forked repository, click the green Code and Open with VisualStudio for a quick start
2525

bicep-examples/existing/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Azure Bicep - Existing references
2+
3+
## Introduction
4+
5+
Referencing existing Azure resources in your Bicep templates is a useful way to cut down on repeating parameter values that may already be known because the resource already exists. In addition to getting properties from existing resources.
6+
7+
Using the `existing` keyword you can reference an existing Azure resource to call into your properties for deployments.
8+
9+
You can read more from the official Microsoft Learn documentation on existing resources in Bicep [here](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/existing-resource?WT.mc_id=MVP_319025).
10+
11+
## 📃 Benefits of existing references
12+
13+
1. ✅ (DRY) Reduce parameter or variable repetition by using existing to pull in existing Azure resources. E.g. instead of defining a parameter for a Log Analytics workspace name or resourceId that already exists you can leverage `existing`
14+
15+
2. ✅ Access. Allows access to existing resource properties easily e.g. ResourceId of existing resource.
16+
17+
3. ✅ Scope. Allows for scope flexibility. You can reference a resource in a different resource group for example.
18+
19+
## Azure Bicep existing examples
20+
21+
In this example, we are referencing two existing Azure resources:
22+
23+
- Resource Group
24+
- Log Analytics Workspace
25+
26+
Both of these resources are likely to already existing in your Azure environment. If you're deploying a new resource you may want to put this into an existing resource group, using an existing log analytics workspace that is centralised for all metrics to ingest into. In this example within the `main.bicep` file:
27+
28+
We are defining the existing resources to be used in a newly deploy Storage Account.
29+
30+
```javascript
31+
module storageAccount 'br/public:avm/res/storage/storage-account:0.8.3' = {
32+
name: 'storageAccount-${uniqueString(subscription().subscriptionId)}'
33+
scope: rg
34+
params: {
35+
name: 'st${uniqueString(deployment().name)}'
36+
location: location
37+
diagnosticSettings:[
38+
{
39+
workspaceResourceId: law.id
40+
metricCategories: [
41+
{
42+
category: 'AllMetrics'
43+
}
44+
]
45+
}
46+
]
47+
}
48+
}
49+
```
50+
51+
`scope: rg` which is leveraging the existing symbolic name of the existing Resource Group where we have specified the existing `name:` of the Resource Group in the `main.bicep` file.
52+
53+
`workspaceResourceId: law.id` which is referencing the existing Log Analytics Workspace resource to retrieve the `resourceId` property.
54+
55+
Combining these enables a new Storage Account to be created in an existing Resource Group and diagnostic settings to send all metrics to an existing Log Analytics Workspace.
56+
57+
## 🚀 Deployment
58+
59+
> [!NOTE]
60+
> The deployment commands will create the existing resources first before leveraging the Bicep template to utilise these.
61+
62+
In VisualStudio Code open a terminal and run:
63+
64+
CLI
65+
66+
```bash
67+
az login
68+
az account set --subscription 'subscription name or id'
69+
az group create -l uksouth -n existing-rg
70+
az monitor log-analytics workspace create -n 'existing-law' -g 'existing-rg'
71+
az deployment sub create -l 'uksouth' --confirm-with-what-if -f '.\main.bicep'
72+
```
73+
74+
or PowerShell
75+
76+
```powershell
77+
Connect-AzAccount
78+
Set-AzContext -Subscription "subscription name or id"
79+
New-AzResourceGroup -Location "UKSouth" -Name "existing-rg"
80+
New-AzOperationalInsightsWorkspace -Location "UKSouth" -Name "existing-law" -ResourceGroupName "existing-rg"
81+
New-AzSubscriptionDeployment -Confirm -Location "UKSouth" -TemplateFile ".\main.bicep"
82+
```

bicep-examples/existing/main.bicep

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
targetScope = 'subscription'
2+
3+
metadata name = 'Existing examples'
4+
metadata description = 'Showcasing Azure Bicep existing resources'
5+
metadata owner = '[email protected]'
6+
7+
@description('Azure region for deployments.')
8+
param location string = 'uksouth'
9+
10+
// Defining existing resource group named 'existing-rg'
11+
resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' existing = {
12+
name: 'existing-rg'
13+
}
14+
15+
// Defining existing log analytics workspace named 'existing-law' from the existing resource group above 'scope: rg`
16+
resource law 'Microsoft.OperationalInsights/workspaces@2023-09-01' existing = {
17+
scope: rg
18+
name: 'existing-law'
19+
}
20+
21+
// Deploying Storage Account to existing resource group & log analytics workspace
22+
module storageAccount 'br/public:avm/res/storage/storage-account:0.8.3' = {
23+
name: 'storageAccount-${uniqueString(subscription().subscriptionId)}'
24+
scope: rg
25+
params: {
26+
name: 'st${uniqueString(deployment().name)}'
27+
location: location
28+
diagnosticSettings:[
29+
{
30+
workspaceResourceId: law.id
31+
metricCategories: [
32+
{
33+
category: 'AllMetrics'
34+
}
35+
]
36+
}
37+
]
38+
}
39+
}
40+
41+
@description('Storage Account name output string.')
42+
output storageAccountName string = storageAccount.outputs.name
43+
44+
@description('Log Analytics Workspace Id.')
45+
output lawName string = law.id

0 commit comments

Comments
 (0)