Emre Şahin

Diário de engenharia

A budget Azure platform in OpenTofu

Designing a lean but secure Azure production platform for several products, plus an on-prem k3s staging twin fed by the same pipeline.

Emre Şahin ·

"Budget" and "secure" are often treated as opposites. You either pay for the enterprise tier or you accept shortcuts. When I designed the production platform for our products, I wanted to test that assumption. The goal was a platform that a small team can afford and operate, that does not cut corners on the things that are expensive to get wrong later, and that is entirely described in code.

The result runs several products: a handful of .NET APIs, a Python service, background workers, some heavier on-demand workloads, and their frontends. It is defined in OpenTofu, deployed from Azure DevOps, and mirrored by a staging environment that runs on a single server in our own office.

This post is about the decisions, not the resource list.

What "budget" meant, concretely

I set three rules before writing any HCL:

  1. Pay for usage, not for capacity we might need. Size every workload from measured behaviour, and let things that are rarely used scale to zero.
  2. Never trade away the things you cannot add later. Network isolation, identity-based access and backups are cheap at the start and painful to retrofit.
  3. Make the expensive knobs explicit. High availability, geo-redundant backups and extra security tooling are variables, off by default, with a comment explaining what turning them on costs and buys.

Everything below follows from those three rules.

Container Apps instead of AKS

The first real decision was the compute platform. AKS was the obvious candidate. It is what most reference architectures show, and the team already knew Kubernetes.

I chose Azure Container Apps instead, on the Consumption profile. The reasoning:

  • Operational surface. AKS means owning node pools, upgrades, ingress controllers, certificate management and cluster add-ons. For a small team running a few products, that is a part-time job that produces no product value.
  • Cost shape. With AKS you pay for nodes whether or not anything is running on them. Container Apps bills per replica, and replicas can go to zero.
  • Good enough primitives. Revisions, internal and external ingress, KEDA-based scaling, managed identity and Key Vault references cover what our services need.

The trade-off is real. Container Apps gives you less control. There is no DaemonSet, no custom admission control and fewer networking options, and some things the azurerm provider does not yet expose have to be patched outside Terraform. If we outgrow it, the services are plain containers and the move is a deployment change, not a rewrite. I would rather pay that migration cost later, if we ever need to, than pay the AKS operating cost every month from day one.

Sizing as a documented decision

Each service is one entry in a map, and each entry carries its reasoning:

"api-gateway"  = { cpu = 0.25, mem = "0.5Gi", min = 1, max = 5, ingress = "external" } # warm: public entry point
"media-worker" = { cpu = 1.0,  mem = "2Gi",   min = 0, max = 5, ingress = "internal",
                   transport = "http2" }                                              # zero: gRPC, on demand
"agent-worker" = { cpu = 0.25, mem = "0.5Gi", min = 1, max = 1, ingress = "none" }    # max=1: a second copy would duplicate work

The comments are the important part. "Warm" services keep one replica because a cold start on an authentication path or a public page is a user-visible problem. "Zero" services are heavy and rarely used, so they pay a cold start in exchange for costing nothing when idle. A background worker that is not safe to run twice is pinned to exactly one replica, and the map says why, so nobody "fixes" it by raising the maximum.

The same reasoning applies to an antivirus scanner that checks uploaded files. It scales to zero because uploads are rare. The calling service fails closed: if the scanner is unavailable, the upload is rejected and retried later instead of being accepted unscanned. A cold scanner costs a delay, not a security gap.

The database is private, full stop

PostgreSQL runs on Flexible Server with VNet integration, a private DNS zone and public network access disabled. There is no firewall rule to forget to remove, because there is no public endpoint at all.

delegated_subnet_id           = azurerm_subnet.postgres.id
private_dns_zone_id           = azurerm_private_dns_zone.postgres.id
public_network_access_enabled = false

Budget shows up elsewhere. One server hosts several product databases. Zone-redundant HA is off and documented as a single variable change. Backups and point-in-time restore are on, because a restore capability is the one thing you cannot buy after the incident. Each database has prevent_destroy, and connection and disconnection logging is enabled so authentication attempts are visible.

Sharing a server between products is a deliberate compromise. It couples their maintenance windows and noisy-neighbour risk. At our current scale the saving is real and the risk is manageable, and splitting later is a data move, not a redesign.

Managed identity over secrets

The platform has one user-assigned managed identity for the apps, with two role assignments: AcrPull on the registry and Key Vault Secrets User on the vault. Container Apps pull images and read secrets through that identity. There are no registry passwords and no long-lived service principal secrets in app configuration.

Secret values never pass through Terraform variables or files in the repo. The database admin password is generated by the IaC itself and stored in Key Vault, so no person ever types it. Application secrets are added to Key Vault and referenced from Container Apps without a version, so a rotation is picked up without a redeploy.

The state file still contains generated values, which is why state lives in a remote backend with restricted access and never on a laptop. "No secrets in code" does not mean "no secrets in state", and the design should say so plainly.

What I deliberately did not buy

Redis and RabbitMQ run as small internal containers rather than managed services. For Redis, used as a cache, that is easy to justify. For RabbitMQ it is a known gap: durable messaging needs persistent storage, and the IaC carries a note that the next step is either persistent volumes or a move to a managed broker. Writing that gap down next to the resource is better than pretending it does not exist.

Frontends are on Static Web Apps, behind a CDN and WAF layer, with no servers at all. That is the cheapest and simplest option on the whole platform, and there was no reason to do anything else.

The staging twin on our own hardware

Production is in Azure. Development and staging are not. We had a capable rack server in the office, and paying cloud prices for environments that get dozens of deployments a day made little sense.

We rebuilt that server as an isolated environment, again entirely as code: Terraform for the VMs, Ansible for configuration.

  • Isolation by default. A pfSense VM is the only thing on the office network. All work VMs sit on an internal bridge with no physical NIC. Each product gets its own VM, which gives us product-level isolation we do not have in the cloud.
  • No open ports. Remote access is through Tailscale only, via a subnet router. There is nothing to port-scan.
  • k3s with an in-cluster registry. The product VMs join a small k3s cluster. Images are built by a self-hosted Azure DevOps agent on the same server and pushed to a registry inside the cluster.
  • Same secret source. The External Secrets Operator pulls from Key Vault, so staging and production read secrets from the same kind of place.
  • Same telemetry contract. Apps export OTLP to an OpenTelemetry Collector, which feeds a Grafana stack (Prometheus, Loki, Tempo). The app configuration is just an endpoint, so moving between environments changes a URL, not the code.

One pipeline, two environments

Every product has the same branching contract:

develop ─▶ self-hosted agent ─▶ in-cluster registry ─▶ k3s rollout       (office)
main    ─▶ hosted agent      ─▶ ACR                 ─▶ Container Apps    (Azure)

One CI system, one set of pipeline definitions, two targets selected by branch. Develop builds do not consume hosted build minutes, which was a meaningful cost on its own.

The honest weakness is parity. k3s is not Container Apps. Scaling behaviour, ingress and cold starts differ, so some classes of problems only show up in production. We accept that because the alternative, a second Azure environment, would roughly double the cloud bill for a benefit we can mostly get by being careful. If that trade-off stops holding, the IaC makes a cloud staging environment a variables file away.

What I would keep, and what I would change

I would keep the three rules, the documented sizing map and the private-by-default database. I would move the message broker to a durable option sooner, and I would add policy checks to the pipeline so that "public network access" or "secret in plain config" fails the plan instead of relying on review.

A budget platform does not have to be a fragile one. Being cheap is easy. The design work is in deciding, explicitly and in writing, which corners you are not willing to cut.