|
| 1 | +# Azure Bicep - Imports and Exports |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +The import and export feature in Bicep allows you to reuse commonly used variables and types efficiently. Exports enable you to define variables that can be imported for use in other templates, while imports allow you to pull in pre-defined variables—eliminating the need to duplicate code across multiple Bicep files. |
| 6 | + |
| 7 | +Instead of manually defining a variable in every new Bicep file, such as: |
| 8 | + |
| 9 | + `var budgetAlertEmail = 'dan@rios.engineer'` |
| 10 | + |
| 11 | +You can store this value centrally and simply import it into your template when needed. |
| 12 | + |
| 13 | +This functionality extends beyond just variables (and types). It can be applied to various use cases, such as subscription IDs, service principal IDs, app registrations, and private DNS zone FQDNs and tons more. Helping maintain consistency and reducing repetitive code. |
| 14 | + |
| 15 | +## 📃 Benefits of User Defined Types |
| 16 | + |
| 17 | +✅ Centraliation: Allows you to define commonly repeated variables and user defined types in one file that many Bicep templates can reuse. |
| 18 | + |
| 19 | +✅ Reduces repetition: Variables you may be repeating in each Bicep template can now be moved centrally, reducing repetition and streamlining templates. |
| 20 | + |
| 21 | +✅ Resuability: The exports can now be used across multiple projects and templates allowing much greater resuability for standards and common values. This can also help reduce configuration errors. |
| 22 | + |
| 23 | +## Export Examples |
| 24 | + |
| 25 | +In the exports example, you can define what variables or types you want to be available to be imported by defining an @export() decorator next to them. |
| 26 | + |
| 27 | +For example, a `shared.bicep` file could reside in the root of your Bicep folder within your repository, with these commonly used variables as an example: |
| 28 | + |
| 29 | +```bicep |
| 30 | +// shared.bicep with common vars |
| 31 | +@export() |
| 32 | +@description('The Primary Azure Region location') |
| 33 | +var location = 'uksouth' |
| 34 | +
|
| 35 | +@export() |
| 36 | +@description('Branch Office Public IP') |
| 37 | +var branchOfficePublicIP = '82.110.72.90' |
| 38 | +``` |
| 39 | + |
| 40 | +### Entra example: |
| 41 | + |
| 42 | +```bicep |
| 43 | +@export() |
| 44 | +@description('Common Entra Security Group(s) for RBAC') |
| 45 | +var entraSecurityGroups = { |
| 46 | + SG_Cloud_Team: { |
| 47 | + displayName: 'SG_Cloud_Team' |
| 48 | + objectId: '11111111-1111-1111-1111-111111111111' |
| 49 | + } |
| 50 | + SG_Security_Team: { |
| 51 | + displayName: 'SG_Security_Team' |
| 52 | + objectId: '22222222-2222-2222-2222-222222222222' |
| 53 | + } |
| 54 | + SG_Dev_Team: { |
| 55 | + displayName: 'SG_Dev_Team' |
| 56 | + objectId: '33333333-3333-3333-3333-333333333333' |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | +## Import Examples |
| 61 | +### Entra ObjectId |
| 62 | +```bicep |
| 63 | +import * as shared from 'shared.bicep' |
| 64 | +
|
| 65 | +module rg 'br/public:avm/res/resources/resource-group:0.4.1' = { |
| 66 | +... |
| 67 | +roleAssignments: [ |
| 68 | + { |
| 69 | + principalId: shared.entraSecurityGroups.SG_Cloud_Team.objectId // Using imported Entra Security Group Object ID |
| 70 | + roleDefinitionIdOrName: 'Contributor' |
| 71 | + } |
| 72 | + ] |
| 73 | +``` |
| 74 | + |
| 75 | +### ACL IP Example: |
| 76 | +```bicep |
| 77 | +import * as shared from 'shared.bicep' |
| 78 | +// or you can only import the required variable vs all available via |
| 79 | +// import { branchOfficePublicIP } as branchOfficePublicIP from 'shared.bicep' as an example |
| 80 | +module keyVault 'br/public:avm/res/key-vault/vault:0.12.1' = { |
| 81 | +.... |
| 82 | + networkAcls: { |
| 83 | + defaultAction: 'Deny' |
| 84 | + bypass: 'AzureServices' |
| 85 | + virtualNetworkRules: [] |
| 86 | + ipRules: [ |
| 87 | + { |
| 88 | + value: shared.branchOfficePublicIP // using central import value from shared.bicep |
| 89 | + action: 'Allow' |
| 90 | + } |
| 91 | + ] |
| 92 | + } |
| 93 | + } |
| 94 | +``` |
| 95 | + |
| 96 | +## 🚀 Deployment |
| 97 | + |
| 98 | +> [!NOTE] |
| 99 | +> You need to have a resource group deployed before trying this out. |
| 100 | +
|
| 101 | +In VisualStudio Code open a terminal and run: |
| 102 | + |
| 103 | +CLI |
| 104 | + |
| 105 | +```bash |
| 106 | +az login |
| 107 | +az account set --subscription 'subscription name or id' |
| 108 | +az deployment group create -g 'your-rg' --confirm-with-what-if -f '.\main.bicep' -p 'main.bicepparam' |
| 109 | +``` |
| 110 | + |
| 111 | +or PowerShell |
| 112 | + |
| 113 | +```powershell |
| 114 | +Connect-AzAccount |
| 115 | +Set-AzContext -Subscription "subscription name or id" |
| 116 | +New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg" -TemplateFile "main.bicep" -TemplateParameterFile "main.bicepparam" |
| 117 | +``` |
0 commit comments