CI/CD on Azure · Part 4 of 7

Azure Container Apps

Deploying the image to a serverless staging with scale-to-zero: managed identity and passwordless AcrPull permission, the Container Apps environment, and a few surprises from the Azure PowerShell tooling.

  • Azure
  • Container Apps
  • Managed identity

Why Container Apps for this environment

Our staging has a particular usage profile: tested intermittently, not 24/7, with potentially long idle periods between two test sessions. That is precisely the use case Azure Container Apps is designed for: unlike Azure Container Instances, which I had initially considered, Container Apps can put its container to sleep on its own (scale-to-zero) and comes with a generous free tier, without us having to manage the container lifecycle by hand (create it before testing, destroy it afterwards).

Basic concepts

Two distinct Azure resources come into play:

  • The Container Apps Environment: the networking and logging shell that hosts one or more Container Apps. The closest analogy would be a Kubernetes cluster (or more precisely its underlying VNet): a real network boundary, inside which several apps can live side by side and reach each other directly by name. And… it is not just an analogy, because under the hood, Container Apps actually runs on an Azure Kubernetes Service cluster managed behind the scenes, with Envoy as the ingress controller, KEDA for event-driven autoscaling (scale-to-zero included), and Dapr available natively for service-to-service communication. That explains why the portal directly exposes raw K8s concepts: replicas, revisions, liveness/readiness/startup probes, autoscaling, etc.
  • The Container App: the unit actually deployed, built from our ACR image. It owns the scaling and ingress configuration, and the link to the registry.

The billing model chosen here is the Consumption plan: we pay per vCPU-second and GiB-second actually consumed, with a substantial monthly free tier, and above all the ability to go down to zero replicas when there is no traffic, hence zero compute cost during those periods.

Prerequisites

  • The Microsoft.App and Microsoft.ManagedIdentity resource providers must be registered on the subscription
  • The acrwestbalidrivers.azurecr.io/westbalidrivers:staging image must already exist in ACR (article 3)
powershell
Register-AzResourceProvider -ProviderNamespace Microsoft.ManagedIdentity
Register-AzResourceProvider -ProviderNamespace Microsoft.App

Step 1: Managed identity and pull permission on ACR

Even before creating the environment, we have a chicken-and-egg problem to solve: the Container App needs an identity to authenticate with ACR when pulling the image, but this identity must already exist and hold the permissions before the app attempts its first pull.

There are two types of managed identity on Azure:

  • system-assigned (created with, and tied to, the lifecycle of the resource itself, so only generated when the Container App is created, too late for our need)
  • user-assigned (an Azure resource in its own right, which we can create and equip before attaching it to anything). We use the latter, precisely to avoid this sequencing problem.

Working variables

powershell
$id_name = "id-westbalidrivers-staging"
$rg       = "rg-westbalidrivers"
$location = "westeurope"
$acr_name = "acrwestbalidrivers" 

Creating the managed identity, independently of any resource that will use it

powershell
New-AzUserAssignedIdentity `
  -Name $id_name `
  -ResourceGroupName $rg `
  -location $location

$identity = Get-AzUserAssignedIdentity -Name $id_name -ResourceGroupName $rg

Get the resource ID of the ACR, to define precisely the scope of the permission granted:

powershell
$acr_id = (Get-AzContainerRegistry -ResourceGroupName $rg -Name $acr_name).Id

Assign the AcrPull role (read-only rights on the registry) to this identity, on this precise scope:

powershell
New-AzRoleAssignment `
  -ObjectId $identity.PrincipalId `
  -RoleDefinitionName "AcrPull" `
  -Scope $acr_id

This is the principle of least privilege applied to the letter: this identity will never be able to push an image, or touch anything else in the subscription; it can only read the images of this registry.

Step 2: Creating the Container Apps environment

powershell
$cae_name = "cae-westbalidrivers"

New-AzContainerAppManagedEnv `
  -Name $cae_name `
  -ResourceGroupName $rg `
  -location $location

And then… it gets stuck:

shell
New-AzContainerAppManagedEnv_CreateExpanded: Invalid request body for environment 'cae-westbalidrivers'. Must contain 'properties'.

A Container Apps Environment needs a log destination configured. Not in the sense that logs absolutely have to be stored somewhere, but in the sense that the creation cmdlet needs this choice to be stated explicitly, if only to say that we do not want to store anything.

What I expected, and what it actually took

My first attempt to create the environment with only -Name, -ResourceGroupName and -Location failed with a rather cryptic error (Must contain 'properties'). Digging into it, the reason lies in the way this cmdlet is generated: with no optional parameter set, the body of the request sent to Azure goes out with an empty properties object, which Azure flatly rejects.

This kind of gap between intuition (“a minimal environment should be creatable with the bare minimum, such as the mandatory parameters of the PowerShell cmdlet”) and the reality of Azure tooling is not an isolated case; I had already grumbled about it when preparing for the AZ-104, see the end of this doc.
The cmdlets of the Az module are largely generated automatically from each product team’s specifications, with no business logic that would check that a minimal call makes sense. The reflex to keep: when faced with a cryptic error, check which options the official docs (or the portal) offer, rather than assuming that a call reduced to the mandatory parameters will be enough.

The simplest solution: do not store the logs

The Azure portal offers a “Don’t save logs (Stream logs without storing them)” option, which translates in PowerShell into setting -AppLogConfigurationDestination to an empty string. It is a value explicitly supported by the API (just like log-analytics or azure-monitor), not a workaround trick. It is enough to populate the properties of the request, without having to provision anything else:

powershell
$cae_name = "cae-westbalidrivers"

New-AzContainerAppManagedEnv `
  -Name $cae_name `
  -ResourceGroupName $rg `
  -location $location `
  -AppLogConfigurationDestination "" 

The trade-off: logs can still be viewed live through the container’s log stream (Get-AzContainerAppRevision, or the “Log stream” tab of the portal), but nothing is stored: no history to query after the fact. For a staging tested now and then, with no need to investigate an incident from the day before, that is plenty, and it avoids provisioning an extra resource for a need we do not really have.

Get the ID of this environment, needed to attach our Container App to it:

powershell
$cae_id = (Get-AzContainerAppManagedEnv -Name $cae_name -ResourceGroupName $rg).Id

Step 3: Creating the Container App

Creation is done by assembling several configuration objects upfront, then passing them all to New-AzContainerApp. It is more verbose than a single call with flat parameters, but each building block stays readable on its own.

The container definition

powershell
$container = New-AzContainerAppTemplateObject `
  -Name "westbalidrivers" `
  -Image "acrwestbalidrivers.azurecr.io/westbalidrivers:staging" `
  -ResourceCpu 0.25 `
  -ResourceMemory "0.5Gi"

0.25 vCPU and 0.5 GiB are more than enough for an Nginx serving static files; they are actually the smallest sizes available on the Consumption plan.

The registry credentials

powershell
$registryCredential = New-AzContainerAppRegistryCredentialObject -Server "acrwestbalidrivers.azurecr.io" -Identity $identity.Id

-Identity here refers to the managed identity created in step 1, by its full resource ID. No password, no secret to store: the identity itself, through the AcrPull role we assigned to it, is what authorizes the pull.

The network configuration (ingress)

powershell
$configuration = New-AzContainerAppConfigurationObject `
  -IngressExternal:$true `
  -IngressTargetPort 80 `
  -Registry $registryCredential
  • -IngressExternal:$true: makes the app reachable from the Internet (as opposed to an internal ingress, reserved for communication between apps of the same environment)
  • -IngressTargetPort 80: the internal port our Nginx container listens on

Final assembly

powershell
New-AzContainerApp `
  -Name "ca-westbalidrivers-staging" `
  -ResourceGroupName $rg `
  -location $location `
  -EnvironmentId $cae_id `
  -Configuration $configuration `
  -TemplateContainer $container `
  -UserAssignedIdentity @($identity.Id) `
  -ScaleMinReplica 0 `
  -ScaleMaxReplica 1

The cmdlets of the Az.App module are relatively recent and still evolve quite fast. If a command returns an unknown parameter error, a Get-Help New-AzContainerApp -Full (or the equivalent for the object concerned) gives the exact signature of the version installed locally.

Step 4: Scale-to-zero, in practice

powershell
-ScaleMinReplica 0 `
-ScaleMaxReplica 1

ScaleMinReplica 0 is the parameter that changes everything compared to an App Service or a classic VM: when no request comes in for a while, Azure simply stops the last active replica. The compute cost drops back to zero.

The trade-off is a “cold start”: the first request arriving after an idle period has to wait for Azure to wake a container up (pull the image from ACR if the local cache has expired, start Nginx). For our image of a few tens of megabytes, Nginx starts in a fraction of a second. Added to the other delays, it stays in the order of ten seconds, perfectly acceptable for a test environment.

ScaleMaxReplica 1 sets a low ceiling: no need to scale horizontally for a staging; this parameter mostly acts as a budget safeguard in case of an unexpected traffic spike (a link shared a little too widely, for example).

Optional, but recommended before sharing the URL: at this point, the app is publicly reachable by anyone who knows (or guesses) its generated URL. The appendix Restricting access to Container Apps with Entra ID covers setting up authentication in front of the container, to keep scanners or bots from waking the container up constantly. The tests below assume this step has not been applied yet (hence the direct 200 OK, with no redirect to a sign-in page).

Step 5: Tests and validation

Getting the generated URL

powershell
$app = Get-AzContainerApp -Name "ca-westbalidrivers-staging" -ResourceGroupName $rg
$fqdn = $app.Configuration.IngressFqdn

The result looks like ca-westbalidrivers-staging.<hash>.<region>.azurecontainerapps.io, a domain name generated and fully managed by Azure, with HTTPS enabled by default.

Checking that the site responds

bash
curl -IL https://$fqdn
shell
HTTP/1.1 301 Moved Permanently
location: https://ca-westbalidrivers-staging.<hash>.westeurope.azurecontainerapps.io/
date: Tue, 04 Aug 2026 08:53:58 GMT
transfer-encoding: chunked

HTTP/2 200 
server: nginx/1.30.4
date: Tue, 04 Aug 2026 08:53:59 GMT
content-type: text/html
content-length: 89459
last-modified: Sat, 01 Aug 2026 19:25:33 GMT
etag: "6a6e482d-15d73"
accept-ranges: bytes

A 200 OK response, and above all a valid certificate (which can be checked directly in a browser through the green padlock, no warning), confirm that TLS is handled by Azure with no Nginx configuration on our side.

Then check that the /transfers page works, Mapbox map included (a reminder from chapter 3): this assumes the image pushed to staging was built with --build-arg NEXT_PUBLIC_MAPBOX_TOKEN=....

Watching the cold start

To see scale-to-zero in action: leave the app without traffic for about ten minutes, then send a request again and watch the response time (noticeably longer than usual, while a replica restarts), followed by a return to normal latency on the following requests.

powershell
# Number of active replicas at a given moment, useful to watch the drop to zero after an idle period
Get-AzContainerAppRevision -ContainerAppName "ca-westbalidrivers-staging" -ResourceGroupName $rg | Select-Object Name, Replica
shell
Name                                Replica
----                                -------
ca-westbalidrivers-staging--p7fsgsn       0

After scaling to 0, a new request to the website from a browser will take about ten seconds between the request and the content showing up, while the Container App starts.

Checklist before moving on

  • The id-westbalidrivers-staging managed identity exists, with the AcrPull role assigned on the ACR
  • The Container Apps environment is created
  • The Container App responds over HTTPS on its generated URL, with a valid certificate
  • The /transfers page (Mapbox) works correctly
  • Scale-to-zero behavior has been observed at least once (cold start seen after inactivity)

Next

So far, every step of the pipeline has been run by hand, from PowerShell, precisely to validate each building block on its own before automating it. In the next chapter, we put it all together in a GitHub Actions pipeline: image build on push to staging, push to ACR, and automatic deployment to this Container App, with no manual intervention at all.

© 2026 Louis Lecat