---
title: "Add Container Apps Key Vault secret references only when the secret exists"
url: "https://www.dariuszparys.com/notes/container-apps-key-vault-secret-refs-only-when-secret-exists"
author: "Dariusz Parys"
published: 2026-11-23T00:00:00.000Z
description: "Azure Container Apps fails to provision a revision when a Key Vault-referenced secret is missing, which blocks the whole deployment, so the reference should be added only when a parameter is switched after the secret exists."
tags:
  - "Azure"
  - "Bicep"
---

# Add Container Apps Key Vault secret references only when the secret exists

## Context

Azure Container Apps resolves Key Vault-referenced secrets when it provisions a revision. If a referenced secret is missing from the vault, provisioning fails, and so does the deployment of the whole app. Every other change in that deployment is blocked too, not only the feature that needs the secret.

## Insight

When you prepare infrastructure before the secret exists, for example a new provider's API key that someone still has to create for each environment, put the secret and its environment variable behind a feature parameter. With the default value, a deployment leaves the app unchanged. Switch the parameter only once the secret is in the vault.

## Bicep

```bicep
@allowed([ 'Old', 'New' ])
param provider string = 'Old'

var extraSecrets = provider == 'New'
  ? [
      {
        name: 'new-provider-key'
        keyVaultUrl: 'https://${keyVaultName}${az.environment().suffixes.keyvaultDns}/secrets/new-provider-key'
        identity: identity.id
      }
    ]
  : []
var extraEnv = provider == 'New'
  ? [ { name: 'NewProvider__ApiKey', secretRef: 'new-provider-key' } ]
  : []

// in the container app:
secrets: concat([ /* existing secrets */ ], extraSecrets)
env: concat([ /* existing env */ ], extraEnv)
```

Also pass the parameter itself as a plain env value, for example `Email__Provider`, so the app and the infrastructure always agree on which provider is active.
