Skip to content

Commit

Permalink
shared variable file pattern examples
Browse files Browse the repository at this point in the history
Shared variable file pattern example folder upload. Contains relevant documentation and files to demo the concept.
  • Loading branch information
riosengineer committed Sep 20, 2023
1 parent cc87711 commit 7a6b3f3
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This is where we hope to bridge the gap for new Bicep users getting started, or

Using simple templated examples, with test data, the examples in this repository we hope will enable others to learn and benefit from.

Within the `Examples` folder you will find categorised concept folders that further contain example `.bicep` templates and their supporting documentation & files.
Within the `bicep-examples` or `bicep-cicd-examples` folders you will find categorised concept folders that further contain example `.bicep` templates and their supporting documentation & files.

These will showcase a particular Bicep concept with a corresponding README file to give an explanation of what the example is, and how it can benefit.

Expand Down
80 changes: 80 additions & 0 deletions bicep-examples/shared-variable-file-pattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Using shared variable file pattern in Azure Bicep - Examples

## Blog post coming soon

Building on the Microsoft Documentation on this I've expanded and created some more examples where this can be useful for you or your organisation. I've broken these down into three example chunks. I would advise first going over the documentation to familiarise yourself with the concept here:

[MS Learn - Shared variable file pattern](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/patterns-shared-variable-file)

Each example should be simple to understand and translate to real world scenarios these could help you with.

Within the `/configs` folder you'll find `json` files which contain the shared variable example data

## Deployment

In VisualStudio Code open a terminal and run:

CLI

```bash
az login
az set --subscription 'your subscription name'
az deployment create --confirm-with-what-if -g 'your resource group name' -f .\file.bicep
```

or PowerShell

```powershell
Connect-AzAccount
Set-AzContext -Subsription "your subsription name"
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your resource group name" -TemplateFile "file.bicep"
```

## Example - Naming prefixes

Using naming prefixes in a global configuration JSON file means we can centralise commonly used variables for repeated use across many Bicep templates.

Loading the content from the JSON file `naming-config.json` we can expand the variable referenced to call on our naming prefixes, for example:

```javascript
@description('Naming standards prefix JSON config file. Loads prefixes for Azure resources using {$namingPrefixes.Name}.')
var namingPrefixes = loadJsonContent('./configs/naming-config.json')
```

The variable 'namingPrefixes' is now available to call on, below is an example JSON config file, where we can call the variable that has loaded this JSON content to call our prefix standard:

```yaml
{
"virtualMachinePrefix": "vm-prod",
"webAppPrefix": "app-prod",
"functionPrefix": "func",
"sqlPrefix": "sqldb-prod",
"storagePrefix": "st",
"vnetPrefix": "vnet",
"subnetPrefix": "snet",
"nicPrefix": "nic",
"pipPrefix": "pip",
"nsgPrefix": "nsg",
"routeTablePrefix": "route"
}
```

`
${namingPrefixes.storagePrefix}
`

Example Azure Bicep code block that uses this:

```javascript
module storage_prefix 'br/public:storage/storage-account:3.0.1' = {
name: 'storage_deploy'
params:{
name: '${namingPrefixes.storagePrefix}riosengineer001'
sku: 'Standard_LRS'
kind: 'StorageV2'
location: 'uksouth'
}
}
```

There are further complete examples for you to test deploy in this folder using this concept, such as loading in virtual machine publisher information and enviornment configuration (production, dev & UAT).
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"primary": {
"name": "UK South",
"location": "uksouth",
"lprefix": "uks",
"prefix": "prd",
"rgsuffix": "prod-rg",
"env": "prod"
},
"dr": {
"name": "UK West",
"location": "ukwest",
"lprefix": "ukw",
"prefix": "dr",
"rgsuffix": "dr-rg",
"env": "dr"
},
"dev": {
"name": "UK South",
"location": "uksouth",
"lprefix": "uks",
"prefix": "dev",
"rgsuffix": "dev-rg",
"env": "dev"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"virtualMachinePrefix": "vm-prod",
"webAppPrefix": "app-prod",
"functionPrefix": "func",
"sqlPrefix": "sqldb-prod",
"storagePrefix": "st",
"vnetPrefix": "vnet",
"subnetPrefix": "snet",
"nicPrefix": "nic",
"pipPrefix": "pip",
"nsgPrefix": "nsg",
"routeTablePrefix": "route"
}
19 changes: 19 additions & 0 deletions bicep-examples/shared-variable-file-pattern/configs/vm-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"winSvr2022": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"Offer": "WindowsServer",
"sku": "2022-datacenter-azure-edition",
"version": "latest"
},
"VMSku": {
"size": "Standard_DS1_v2"
},
"licenseType": "Windows_Server",
"patchMode": "AutomaticByPlatform",
"OS": "Windows",
"OSDiskGB": 127,
"RoleExtensions": {
}
}
}
37 changes: 37 additions & 0 deletions bicep-examples/shared-variable-file-pattern/env.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Rios Engineer - Shared variable patterns
@description('Environment configuration JSON file. Loads env schema.')
var env = loadJsonContent('./configs/env-config.json')

@description('Define each organisational environment short prefix.')
var primaryPrefix = env.primary.prefix
var drPrefix = env.dr.prefix
var devPrefix = env.dev.prefix

module kv_prod 'br/public:security/keyvault:1.0.2' = {
name: 'kv_deploy1'
params:{
location: env.primary.location
prefix: primaryPrefix
}
}

module kv_dr 'br/public:security/keyvault:1.0.2' = {
name: 'kv_deploy21'
params:{
location: env.primary.location
prefix: drPrefix
}
}

module kv_dev 'br/public:security/keyvault:1.0.2' = {
name: 'kv_deploy3'
params:{
location: env.primary.location
prefix: devPrefix
}
}

// Output KeyVault names
output prod string = kv_prod.name
output dr string = kv_dr.name
output dev string = kv_dev.name
53 changes: 53 additions & 0 deletions bicep-examples/shared-variable-file-pattern/namingPrefixes.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Rios Engineer - Shared variable patterns
@description('Naming standards prefix JSON config file. Loads prefixes for Azure resources using {$namingPrefixes.Name}.')
var namingPrefixes = loadJsonContent('./configs/naming-config.json')

@description('Deployment location for resources.')
param location string = 'uksouth'

@description('Virtual Network Address space.')
param vnetAddressPrefix string = '10.1.0.0/16'

@description('Virtual Network Address space.')
param snetAddressPrefix string = '10.1.1.0/24'

module storage_prefix 'br/public:storage/storage-account:3.0.1' = {
name: 'storage_deploy'
params:{
name: '${namingPrefixes.storagePrefix}riosengineer001'
sku: 'Standard_LRS'
kind: 'StorageV2'
location: 'uksouth'
}
}

module pip_prefix 'br/public:network/public-ip-address:1.0.2' = {
name: 'pip_deploy'
params: {
name: '${namingPrefixes.pipPrefix}-prefix-demo'
location: location
domainNameLabel: 'pip-demo'
}
}

module vnet 'br/public:network/virtual-network:1.1.3' = {
name: 'vnet_deploy'
params:{
name: '${namingPrefixes.vnetPrefix}-prod-uks-001'
addressPrefixes: [
vnetAddressPrefix
]
subnets:[
{
name: '${namingPrefixes.subnetPrefix}-rios-demo'
addressPrefix: snetAddressPrefix
}
]
}
}

// Output resource names
output storage string = storage_prefix.outputs.name
output pip string = pip_prefix.outputs.name
output vnet string = vnet.outputs.name
output snet array = vnet.outputs.subnetNames
101 changes: 101 additions & 0 deletions bicep-examples/shared-variable-file-pattern/vm.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Rios Engineer - Shared variable patterns
@description('Global VM JSON config file.')
var compute = loadJsonContent('./configs/vm-config.json')

@description('Naming standards prefix JSON config file. Loads prefixes for Azure resources using {$namingPrefixes.Name}.')
var naming = loadJsonContent('./configs/naming-config.json')

@description('Deployment location for resources.')
param location string = resourceGroup().location

// Windows Global Config
var image = compute.winSvr2022.imageReference
var sku = compute.winSvr2022.VMSku.size

// Naming standards
var vmPrefix = naming.virtualMachinePrefix
var nicPrefix = naming.nicPrefix
var pipPrefix = naming.pipPrefix

// Prefix names
var vmName = '${vmPrefix}-demo'
var nic = '${nicPrefix}-rios-demo'
var pip = '${pipPrefix}-rios-demo'
var snet = subnet.id

@description('Username for the Virtual Machine.')
param adminUsername string

@description('Password for the Virtual Machine.')
@minLength(12)
@secure()
param adminPassword string

/*
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2020-06-01' existing = {
name: 'vnet-prod-uks-001'
}*/

resource subnet 'Microsoft.Network/virtualNetworks/subnets@2020-06-01' existing = {
name: 'vnet-prod-uks-001/snet-rios-demo'
}

resource publicIP 'Microsoft.Network/publicIPAddresses@2020-06-01' = {
name: pip
location: location
properties: {
publicIPAllocationMethod: 'Dynamic'
}
}
resource networkInterface 'Microsoft.Network/networkInterfaces@2020-06-01' = {
name: nic
location: location
properties: {
ipConfigurations: [ {
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
subnet: {
id: snet
}
publicIPAddress: {
id: publicIP.id
}
}
} ]
}
}
resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-06-01' = {
name: vmName
location: location
properties: {
hardwareProfile: {
vmSize: sku
}
osProfile: {
computerName: vmName
adminUsername: adminUsername
adminPassword: adminPassword
}
storageProfile: {
imageReference: {
offer: image.Offer
publisher: image.publisher
sku: image.sku
version: image.version
}
osDisk: {
createOption: 'FromImage'
}
}
networkProfile: {
networkInterfaces: [ {
id: networkInterface.id
} ]
}
}
}

output vm string = virtualMachine.name
output os object = virtualMachine.properties

0 comments on commit 7a6b3f3

Please sign in to comment.