(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to Provision Secrets with Azure Key Vault in IaC & DevOps

João Barros 04 de December de 2026 4 min read

This tutorial shows how to create and manage secrets automatically using Azure Key Vault integrated into Infrastructure as Code & DevOps. You will learn why to separate secrets from code and how to automate provisioning and secure usage in pipelines with Bicep and GitHub Actions.

Prerequisites

  • Azure account with permissions to create a Resource Group, a Key Vault and a Managed Identity.
  • GitHub repository for the code and GitHub Actions enabled.
  • Bicep CLI or Azure CLI installed locally (az cli v2.20+ recommended).
  • Basic knowledge of Bicep, Git and GitHub Actions.

Step 1: Why and solution design

Separating secrets from code improves security and auditability. The pattern we will implement: create a Resource Group, a Key Vault and a User Assigned Managed Identity with Bicep; grant the Managed Identity access to read secrets; and use GitHub Actions to create or update secrets in the Key Vault via Azure CLI authenticated with OIDC or Service Principal.

Step 2: Create the Bicep file for the resources

We will create a bicep file that creates the Resource Group, the Key Vault and the User Assigned Managed Identity, and assigns access (Key Vault access policy) for the Managed Identity. This makes the infrastructure reproducible.

targetScope = 'subscription'

param location string = resourceGroup().location
param rgName string = 'rg-secrets-demo'

resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: rgName
  location: location
}

resource uami 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
  name: 'uami-secrets-demo'
  location: rg.location
}

resource kv 'Microsoft.KeyVault/vaults@2021-10-01' = {
  name: 'kv-secrets-demo'
  location: rg.location
  properties: {
    tenantId: subscription().tenantId
    sku: { family: 'A' name: 'standard' }
    accessPolicies: [
      {
        tenantId: subscription().tenantId
        objectId: uami.properties.principalId
        permissions: { secrets: [ 'get', 'list', 'set', 'delete' ] }
      }
    ]
  }
  dependsOn: [uami]
}

output keyVaultName string = kv.name
output uamiClientId string = uami.properties.clientId

Save as infra.bicep. Then deploy with the Azure CLI (the example below assumes you use a new Resource Group).

az group create --name rg-secrets-demo --location northeurope
az deployment group create --resource-group rg-secrets-demo --template-file infra.bicep

Step 3: Insert an initial secret manually (optional)

You can add a secret manually to test. Use the Azure CLI authenticated with a user that has permissions.

az keyvault secret set --vault-name kv-secrets-demo --name 'DbPassword' --value 'P@ssw0rd123!'

Step 4: Configure GitHub Actions to provision/update secrets

We will create a workflow that uses OIDC to obtain an Azure AD token and run az cli to write secrets to the Key Vault. This pattern avoids storing long-lived credentials in GitHub.

# .github/workflows/provision-secrets.yml
name: Provision Secrets to Key Vault
on:
  push:
    branches: [ main ]

permissions:
  id-token: write
  contents: read

jobs:
  provision:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Login to Azure using OIDC
      uses: azure/login@v2
      with:
        client-id: ${{ secrets.AZURE_CLIENT_ID }} # User Assigned Identity clientId if needed
        tenant-id: ${{ secrets.AZURE_TENANT_ID }}
        allow-no-subscriptions: true

    - name: Set secret in Key Vault
      env:
        SECRET_VALUE: ${{ secrets.DB_PASSWORD }}
      run: |
        az account show --query id
        az keyvault secret set --vault-name kv-secrets-demo --name DbPassword --value "$SECRET_VALUE"

Important notes: you can configure OIDC directly without client-id for workloads if you are using a federated credential; if you use User Assigned Managed Identity the flows are different. Here we use a Service Principal stored in the GitHub Secrets (AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET) as a simple example.

Step 5: Permissions and common errors

Common errors: 1) 401/403 from the Azure CLI — typically insufficient permissions on the Key Vault or OIDC credentials misconfigured; 2) "User not found" in the access policies — ensure the objectId of the identity is correct; 3) Workflow failing due to missing secrets — confirm that DB_PASSWORD is defined in the repository Secrets.

Verify the result

Confirm that the resources exist via Azure CLI:

az keyvault show --name kv-secrets-demo
az keyvault secret show --vault-name kv-secrets-demo --name DbPassword --query value -o tsv

If the second command returns the expected value, the provisioning flow is working. In GitHub Actions, validate that the job completed successfully and that the az keyvault secret set steps ran without errors.

Conclusion

Automating secrets provisioning with Bicep and GitHub Actions improves security, traceability and repeatability. Next steps: use Managed Identities for applications instead of secrets, integrate Azure Key Vault with Key Vault references in App Service or AKS, and configure Key Vault soft-delete and purge protection. Tip: always test in an isolated environment and validate access policies before moving to production — which Azure authentication method will you adopt (OIDC vs Service Principal vs Managed Identity)?