Azure resources like Virtual Machines (VMs), App Services, Azure Functions, and Azure Kubernetes Service (AKS) using Bicep templates with PowerShell
Azure resources like Virtual Machines (VMs), App Services, Azure Functions, and Azure Kubernetes Service (AKS) using Bicep templates with PowerShell
json #
deploying Azure resources like Deployment parameters
└── README.md #
Virtual Machines (VMs), App
Documentation
Services, Azure Functions, and
Azure Kubernetes Service (AKS) Bicep Templates
using Bicep templates with 1. Virtual Machines (VMs)
PowerShell. File: vm.bicep
param vmName string
Setup and Prerequisites param adminUsername string
1. Install Azure PowerShell: param adminPassword string
Ensure you have the latest
Azure PowerShell module: resource vm
Install-Module -Name Az - 'Microsoft.Compute/virtualMachines
AllowClobber -Scope CurrentUser @2023-03-01' = {
Update-Module -Name Az name: vmName
location: resourceGroup().location
2. Install Bicep CLI: Install or
properties: {
upgrade Bicep: hardwareProfile: {
az bicep install vmSize: 'Standard_DS1_v2'
az bicep upgrade }
3. Log in to Azure: osProfile: {
Connect-AzAccount computerName: vmName
4. Set the Subscription: adminUsername: adminUsername
Replace adminPassword: adminPassword
}
SUBSCRIPTION_ID with
storageProfile: {
your subscription ID: imageReference: {
Set-AzContext -SubscriptionId publisher: 'Canonical'
SUBSCRIPTION_ID offer: 'UbuntuServer'
sku: '18.04-LTS'
Directory Structure version: 'latest'
Organize your files as follows: }
bicep-templates/ osDisk: {
│ createOption: 'FromImage'
├── main.bicep # Main managedDisk: {
Bicep file to orchestrate resources storageAccountType:
├── vm.bicep # Module 'Standard_LRS'
for Virtual Machine }
├── app-service.bicep # }
Module for App Service }
├── aks.bicep # Module networkProfile: {
for AKS networkInterfaces: [
├── function-app.bicep # {
Module for Azure Functions id: nic.id
}
] resource subnet
} 'Microsoft.Network/virtualNetworks/s
} ubnets@2023-02-01' = {
} parent: vnet
name: 'default'
resource nic properties: {
'Microsoft.Network/networkInterfaces addressPrefix: '10.0.0.0/24'
@2023-02-01' = { }
name: '${vmName}-nic' }
properties: {
ipConfigurations: [ 2. App Service
{ File: app-service.bicep
name: 'ipconfig1' param appName string
properties: { param hostingPlanName string
subnet: {
id: subnet.id resource appServicePlan
} 'Microsoft.Web/serverfarms@2023-
privateIPAllocationMethod: 02-01' = {
'Dynamic' name: hostingPlanName
} location: resourceGroup().location
} sku: {
] name: 'B1'
} tier: 'Basic'
} }
}
resource vnet
'Microsoft.Network/virtualNetworks@ resource webApp
2023-02-01' = { 'Microsoft.Web/sites@2023-02-01' = {
name: '${vmName}-vnet' name: appName
properties: { location: resourceGroup().location
addressSpace: { properties: {
addressPrefixes: [ serverFarmId: appServicePlan.id
'10.0.0.0/16' }
] }
}
subnets: [ 3. Azure Kubernetes Service (AKS)
{ File: aks.bicep
name: 'default' param clusterName string
properties: { param nodeCount int = 3
addressPrefix: '10.0.0.0/24' param nodeSize string =
} 'Standard_DS2_v2'
}
] resource aksCluster
} 'Microsoft.ContainerService/managed
} Clusters@2023-02-01' = {
name: clusterName
location: resourceGroup().location
properties: { serverFarmId: hostingPlan.id
dnsPrefix: clusterName siteConfig: {
agentPoolProfiles: [ appSettings: [
{ {
name: 'nodepool1' name: 'AzureWebJobsStorage'
count: nodeCount value:
vmSize: nodeSize storageAccount.properties.primaryE
osType: 'Linux' ndpoints.blob
mode: 'System' }
} {
] name:
enableRBAC: true 'FUNCTIONS_WORKER_RUNTIME'
} value: 'node'
} }
]
4. Azure Functions }
File: function-app.bicep }
param functionName string }
param storageAccountName string
5. Main Deployment File
resource storageAccount main.bicep orchestrates all
'Microsoft.Storage/storageAccounts modules:
@2023-02-01' = { module vm './vm.bicep' = {
name: storageAccountName name: 'vmDeployment'
location: resourceGroup().location params: {
sku: { vmName: 'myVM'
name: 'Standard_LRS' adminUsername: 'adminUser'
} adminPassword: 'P@ssw0rd!'
} }
}
resource hostingPlan
'Microsoft.Web/serverfarms@2023- module appService './app-
02-01' = { service.bicep' = {
name: '${functionName}-plan' name: 'appServiceDeployment'
location: resourceGroup().location params: {
sku: { appName: 'myAppService'
name: 'Y1' hostingPlanName: 'myHostingPlan'
tier: 'Dynamic' }
} }
}
module aks './aks.bicep' = {
resource functionApp name: 'aksDeployment'
'Microsoft.Web/sites@2023-02-01' = { params: {
name: functionName clusterName: 'myAKSCluster'
location: resourceGroup().location nodeCount: 3
properties: { nodeSize: 'Standard_DS2_v2'
}
}
Takeaways
Modular design improves
maintainability and
reusability.
Use parameter files for
flexible deployments.
Validate templates before
deployment to ensure
correctness.