📋 In This Post
Lab Overview
An internal tool gets used nine times a day, all of them between 9am and 5pm, all of them by the same four people. On a virtual machine it costs the same at 3am as it does at noon. Most internal software looks like this, and most of it is billed as though it were busy.
The alternative is to run nothing when nobody is asking. That is free, and it is not free — somebody has to wait while a container starts. This week deploys that trade and measures both ends of it.
| Difficulty | Intermediate — assumes containers and Terraform; the scaling behaviour is what the week is about |
| Time to complete | About an hour. The scale-to-zero test cannot be hurried: it waits for the idle timer, which took 602 seconds here |
| Deployment method | Terraform (azurerm), state in HCP Terraform, local execution |
| Source and lab guide | week-09-container-apps on GitHub — exact prerequisites, variables, commands, expected outputs and cleanup |
Cloud Services and Concepts
| Service | Purpose here | What it is |
|---|---|---|
| Azure Container Apps | Runs the container | Runs containers without you running the servers underneath. An environment is the boundary a set of apps shares. |
| Consumption plan | Why an idle app costs nothing | No hourly fee of its own. You pay for vCPU-seconds, memory and requests, against a monthly free grant. |
| Scale to zero | What this week turns on | After about ten minutes idle the app drops to zero replicas and bills nothing. The next caller pays the cold start — measured at 16.9s against 0.13s warm. |
| Diagnostic setting | How the logs leave | Sends console and system logs to a destination, authenticating as ARM rather than with the destination's shared key. |
| User-assigned managed identity | What the app authenticates as | An Azure identity with no password attached to the app, and the seam a private registry pull would use. |
Architecture and Workflow
Prerequisites
- Permissions: Contributor on the workload subscription is enough. This week creates no role assignment, so it does not need User Access Administrator — worth saying, because the following week does.
- Reader on the resource group holding the Log Analytics workspace. The workspace is read through a data source and never modified.
- An existing Log Analytics workspace. This week sends its logs to the one week 8 built rather than creating its own. One subscription works fine; the lab splits the workload and the workspace across two.
- Terraform ≥ 1.10 with
azurerm5.x (1.16.6 was the version that actually ran here). azurerm 5.x requires an explicitsubscription_idon every provider block. - Four resource providers on the workload subscription
—
Microsoft.App,Microsoft.Insights,Microsoft.ManagedIdentityandMicrosoft.OperationalInsights. The deploy script registers them and waits; registration is per subscription and is a real precondition. - Builds on week 8 (the workspace and its table plans). Week 8 does not need to still be deployed, only to exist.
Versions, every variable and the role scopes are in the lab guide on GitHub, which is the reproducible half of this write-up.
Implementation
The goal: a workload that serves real traffic from the internet, runs nothing when idle, and reports to week 8's workspace across a subscription boundary.
Five resources, and the shape of the week is in the last one:
| Resource | Count | Its job |
|---|---|---|
| Resource group | 1 | what dies together at teardown |
| User-assigned managed identity | 1 | the seam a private registry would authenticate through |
| Container Apps environment | 1 | the boundary — network and log destination |
| Container app | 1 | the workload — a Microsoft-published sample image, 0 to 3 replicas, HTTPS only |
| Diagnostic setting | 1 | routes logs into week 8's workspace, in another subscription |
1. The HCP workspace, created first and deliberately
POST /api/v2/organizations/Katta/workspaces
{ "name": "azure-week-09-dev", "execution-mode": "local" }
created: azure-week-09-dev | execution-mode: local
A new HCP workspace defaults to remote execution, which runs the plan on HCP's servers where there is no Azure CLI. Setting it at birth is one line; discovering it afterwards costs a confusing failure on the wrong machine.
Six resources there, not five — the sixth is the data source reading week 8's workspace. Terraform tracks what it reads as well as what it owns.
2. The Terraform
main.tf answers "what runs, and how little of it runs when nobody
is asking". logging.tf is separate because its answer lives in
another subscription.The replica floor is the week in one argument:
template {
min_replicas = 0 # the whole point
max_replicas = 3 # a ceiling, not a target
cooldown_period_in_seconds = 300
http_scale_rule {
name = "http-concurrency"
concurrent_requests = 10
}
}
3. Deploy
$ ./scripts/deploy.sh
registering Microsoft.App (currently NotRegistered)
attempt 1: Registered
...
azurerm_container_app_environment.workload: Creation complete after 14m55s
azurerm_container_app.hello: Creation complete after 18s
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
The environment took 14 minutes 55 seconds. The app on top of it took 18. Provider registration lives in the deploy script rather than in Terraform, because registration is subscription-wide shared state and one week's destroy must not unregister what another week still needs.
4. It answers — and only over HTTPS
allow_insecure_connections left at its default the ingress also
answers port 80 with a 200 — and a client that gets a 200 in the clear has no
reason to ever upgrade.5. And then it stops
6. The logs land in last week's workspace
What production does differently
- A private registry. This pulls a Microsoft sample anonymously. Production builds its own image into ACR — which adds a registry block pointing at that managed identity, not a password.
- Builds in CI, on week 7's federated credential, not on a laptop.
- Something in front of the ingress. External ingress straight to the app is right for a demonstration and rare in production.
Validation and Evidence
A successful terraform apply proves the resources exist. It
says nothing about whether the app refuses plain HTTP, whether replicas
actually reach zero, or whether the logs land in the right tables — so
those are tested separately.
validate.sh — 6 checks, 8 assertions, 8 passed / 0
failed:
| Test | Expected | Actual | Status |
|---|---|---|---|
HTTPS GET / | 200 | 200 | PASS |
| Plain HTTP on port 80 | redirected or refused — never 200 | 301, never served in the clear | PASS |
| Warm request, average of 3 | under 5 s | 0.13 s | PASS |
| Replicas reach 0 when idle | 0 replicas within the cooldown | 602 s to zero | PASS |
| First request after idle | still a 200, however slow | 16.94 s, still a 200 | PASS |
| Log rows in week 8's workspace | more than zero | 3 | PASS |
Rows in shared AzureDiagnostics | 0 — dedicated tables only | 0 | PASS |
| The cold-start penalty | — | 16.81 s on one request | derived from the two rows above, not asserted |
That last pair is the test that the logs went to dedicated tables rather than the shared one — which is what makes week 8's per-table cost design apply to them at all.
Security and Cost
Security
- No password exists. The app carries a user-assigned managed identity, and a managed identity cannot hold a secret — that is a property of the object, not a discipline anyone keeps.
- HTTPS only, proven by a 301 rather than assumed from a setting.
- Named log categories, not
allLogs— which is defined by Azure and silently grows when Azure adds a category. - The image comes from MCR, so there is no registry credential to hold in the first place.
Cost
The free grant is 180,000 vCPU-seconds, 360,000 GiB-seconds and 2 million requests per subscription per month. This week used roughly 600 vCPU-seconds. A Consumption-only environment carries no fixed hourly fee — though private endpoints and planned maintenance do attract a management charge on either plan, and this week uses neither.
Measured: $0.00. Read from Cost Management after teardown,
not from a rate card — sub-lab-dev shows no charges at all for the
days this ran. The free grant absorbed the whole week.
Idle replicas bill eight times cheaper than active — but only if the floor is above zero. A floor of zero has no idle state to bill. Keeping one replica warm is the other defensible answer, and the one to pick if 17 seconds is unacceptable.
Troubleshooting and Lessons
The default logging path was impossible.
- Problem: the default log destination could not be used at all.
- Root cause:
log-analyticswires the environment to a workspace using that workspace's shared key. Week 8 disabled local authentication, so there is no key to hand over. - Resolution:
azure-monitorplus a diagnostic setting, which authenticates as Azure Resource Manager instead. The cost is that the portal's Logs blade on the app goes dark; queries run against the workspace. - Lesson learned: a security decision made one week becomes an architecture constraint the next. The estate is the thing that has to be consistent, not the individual week.
A configuration that never converged.
- Problem: every plan wanted to set
log_analytics_destination_type = "Dedicated"again, forever. - Root cause: ARM does not persist that field here, so it reads back null and Terraform sees a permanent diff.
- Resolution: removed the argument. These logs are
resource-specific anyway — proven, not assumed, by
AzureDiagnosticsholding zero rows while the dedicated table held three. - Lesson learned: a plan that is never empty trains you to ignore the diff that matters. Perpetual drift is a bug to remove, not noise to tolerate.
A test that concluded scale-to-zero was broken.
- Problem: replicas were still above zero at the
300-second mark, with
cooldown_period_in_secondsset to 300. - Root cause: the cooldown governs when the scaler starts counting down, not when the last replica disappears. Zero came at 602 seconds, roughly twice the configured value.
- Resolution: poll for zero with a generous ceiling rather than asserting at the configured number.
- Lesson learned: a timing setting names the start of a process, not its completion. Asserting at the configured value tests the documentation rather than the system.
Cleanup and Key Takeaways
Cleanup
$ ./scripts/cleanup.sh
resource groups named rg-containerapps-lab-dev-scus-001 : 0
Container Apps environments : 0
container apps : 0
wk09 managed identities : 0
week 08's workspace still there : log-platform-prod-scus-001
That last line is deliberate. Week 8's workspace is read through a data source, never owned, so this week's teardown cannot destroy it — and the teardown checks that it didn't.
References
- Billing in Azure Container Apps
- Log storage and monitoring options
- Set scaling rules
- The code for this week
Key takeaways
- Scale-to-zero is not free, it is cheap and slow. 16.81 seconds, paid by one user, invisible in any average. Put that number in front of whoever decides whether it is acceptable.
- A ceiling matters as much as a floor. Zero protects you
from idle cost;
max_replicasprotects you from a runaway one. - Last week's security decision is this week's constraint — which is what good security does, and better found in week 9 than in an incident.
Comments