DEV Community

1suleyman
1suleyman

Posted on

πŸ’» Exercise 02 – Making Your Bicep Templates Smarter with Parameters & Variables (Using Your Own Subscription)

🧰 Tools I Used:

  • Visual Studio Code (with the Bicep extension)
  • Azure CLI + Bicep CLI
  • An active Azure subscription

🎯 Goal:

In this exercise, I focused on making my Bicep template more reusable and environment-aware. Here’s what I accomplished:

βœ… Accepted parameters like location and environment type

βœ… Used variables to dynamically choose SKUs

βœ… Generated unique names for my storage account and web app

βœ… Deployed it all with a single CLI command


πŸ› οΈ Step 1: Add Parameters and Variables

Inside main.bicep, I added parameters to keep things flexible β€” so I wouldn’t hardcode values like region or service names.

param location string = 'westus'
param storageAccountName string = 'toylaunch${uniqueString(resourceGroup().id)}'
param appServiceAppName string = 'toylaunchapp${uniqueString(resourceGroup().id)}'

@allowed([
  'nonprod'
  'prod'
])
param environmentType string

var appServicePlanName = 'toy-product-launch-plan'
var storageAccountSkuName = (environmentType == 'prod') ? 'Standard_GRS' : 'Standard_LRS'
var appServicePlanSkuName = (environmentType == 'prod') ? 'P2v3' : 'F1'
Enter fullscreen mode Exit fullscreen mode

➑️ This setup means I can choose between prod and nonprod, and the template will auto-select the right SKUs.


πŸ—οΈ Step 2: Update Resources to Use Parameters

Here's how I wired those parameters and variables into the resource definitions:

resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: storageAccountSkuName
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
  }
}

resource appServicePlan 'Microsoft.Web/serverfarms@2024-04-01' = {
  name: appServicePlanName
  location: location
  sku: {
    name: appServicePlanSkuName
  }
}

resource appServiceApp 'Microsoft.Web/sites@2024-04-01' = {
  name: appServiceAppName
  location: location
  properties: {
    serverFarmId: appServicePlan.id
    httpsOnly: true
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸš€ Step 3: Deploy!

Logged in:

az login
az account set --subscription "My Subscription"
Enter fullscreen mode Exit fullscreen mode

Deployed with:

az deployment group create \
  --resource-group BicepRG \
  --name deploy-bicep-env \
  --template-file main.bicep \
  --parameters environmentType=nonprod
Enter fullscreen mode Exit fullscreen mode

πŸ˜… First, I forgot to create the resource groupβ€”rookie mistake. Fixed it with:

az group create \
  --name BicepRG \
  --location westus
Enter fullscreen mode Exit fullscreen mode

βœ… Step 4: Check the Azure Portal

Boom πŸ’₯ β€” after deploying, I jumped into the Azure Portal and opened up the BicepRG resource group. There it was:

  • A Storage Account
  • An App Service Plan
  • A Web App
  • All with the correct SKUs for nonprod: Standard_LRS + F1

πŸ” Bonus: Try a Production Deployment

Changed just one value in the CLI:

az deployment group create \
  --resource-group BicepRG \
  --name deploy-bicep-prod \
  --template-file main.bicep \
  --parameters environmentType=prod
Enter fullscreen mode Exit fullscreen mode

βœ… That deployed a Standard_GRS storage account and a P2v3 App Service Plan β€” super clean.


πŸ“¦ Recap

Task What I Learned/Did
Parameters Accepted inputs like location and environment
Variables Used logic to change SKUs based on environment
Dynamic naming Used uniqueString() to avoid name clashes
Deployment Used az deployment group create for both envs
Verified Checked everything in the Portal πŸ‘€

πŸ’¬ Learning Bicep? Or working on Azure infra too?

Let’s connect on LinkedIn β€” drop me a message and say hi! Always happy to chat with other folks on the same path. πŸš€

Top comments (0)