Programmatically create 'n' Azure VMs using Bicep templates and GitHub actions workflows. 🔽
You can use Azure Bicep Templates to define your VM infrastructure and a GitHub Actions workflow to automate their deployment. The pipeline runs the IaC that programmatically provisions 'n' number of Azure Virtual Machines iteratively. This article is a step-by-step guide to implement such Infrastructure as Code automation ( IaC + .bicep + .yaml ).
VMs-via-BICEP-programmatic-iterative 📑 < Public GitHub Repository that contains all the IaC files that we need. 🔸 FIRST: We create a GitHub Repo where the main.bicep, vm.bicep module, deploy.yml pipeline and .json parameters will be commited/pushed.
|
Create an Azure Resource Group, a Managed Identity and a Federated Identity Credential to integrate with GitHub via VSCode PS console
---------------------------------------------------------------------------------------------------------------------------------------------
|
$managedIdentityName = "bicep-demo-deploy-federation"
$subscriptionID = "<YOUR-AZURE-SUBSCRIPTION-ID"> # az account show # portal.azure.com
$resourceGroupName = "VMs-via-BICEP-programmatic-iterative"
New-AzResourceGroup -Name "managed-identity" -Location "eastus"
Register-AzResourceProvider -ProviderNamespace Microsoft.ManagedIdentity
$managedIdentity = New-AzUserAssignedIdentity -Name $managedIdentityName -ResourceGroupName managed-identity -Location eastus
New-AzResourceGroup -Name $resourceGroupName -Location eastus
$roleAssignment = New-AzRoleAssignment -ObjectId $managedIdentity.PrincipalId -RoleDefinitionName "Contributor" -Scope "/subscriptions/${subscriptionID}
/resourceGroups/${resourceGroupName}"
$githubOrganization = "agustinborrajo"
$environmentName = "deploy"
$repoName = "VMs-via-BICEP-programmatic-iterative"
$subjectUri = "repo:${githubOrganization}/${repoName}:environment:${environmentName}"
New-AzFederatedIdentityCredential -ResourceGroupName managed-identity -IdentityName $managedIdentity.name -Name bicep-demo-federation -Issuer "https://
token.actions.githubusercontent.com" -Subject $subjectUri
$managedIdentity.ClientID
|
---------------------------------------------------------------------------------------------------------------------------------------------
The last command $managedIdentity.ClientID gives you the GitHub Environment Secret needed to complete the integration with Azure
|
|
🔸 VISUAL STUDIO CODE : The below orientative snapshot shows the above commands and their output.
|
🔸 GITHUB ENVIRONMENT SECRETS : We add the above CLIENT_ID (Federated Identity Credential) + Azure Subscription ID + Azure Tenant + VM USR/PSW.
|
🔸 VISUAL STUDIO CODE : We create/open deploy.yml (pipeline) + vm.bicep (module) + main.bicep (iteration) + dev.parameter.json (params).
VMs-via-BICEP-programmatic-iterative 📑 can be forked to get the below IaC files:
|
🔸 GITHUB : deploy.yml (pipeline) + vm.bicep (module) + main.bicep (iteration) + dev.parameter.json (params) after the commit/push (below):
|
🔸 GITHUB + AZURE : All VMs, NICs, IPs & DSK resources are created Once the pipeline successfully Runs the Azure Bicep Deployment.
|
|