# Azure budget guard

This exercise provisions a real Azure VM, network, Automation account, webhook,
action group, and subscription budget. The VM, managed disk, Automation jobs,
notifications, and network resources may incur charges; quotas do not guarantee
free use. Deallocating the VM does not remove its disk or all charges. Review
Azure pricing and quota, use an isolated subscription with known safe guardrails,
and tear the lab down promptly. Azure cost data commonly lags by 8–24 hours and
budget evaluations are typically daily. A budget is not a spending cap.

The guard identity has access only to the uniquely named lab VM. Its runbook
checks the lab marker and deallocates that VM; it does not delete resource
groups or enumerate other VMs. The target VM has no public IP and its network
security group denies inbound and outbound traffic. The budget tracks only the
target resource group, so costs of the guard group are not included. Other
subscription budgets still apply, and a synthetic notification tests the
action group and runbook, **not** real cost ingestion or threshold evaluation.
The lab budget is 10 units of the subscription's billing currency.

Install Azure CLI, Terraform 1.5+, Python 3, OpenSSH and Bash on Linux. Log in
and select the intended subscription before running the lab. Its subscription
must allow the providers `Microsoft.Resources`, `Microsoft.Network`,
`Microsoft.Compute`, `Microsoft.Automation`, `Microsoft.Insights`,
`Microsoft.Consumption` and `Microsoft.Authorization`; this lab does not
register providers or change existing resources or guardrails.

```sh
export AZURE_SUBSCRIPTION_ID="your-subscription-id"
./setup.sh "$AZURE_SUBSCRIPTION_ID" northeurope --accept-costs
```

The default VM size is `Standard_B1s`. If unavailable, supply another
compatible x64 VM size as a fourth argument after checking its price and quota;
availability varies by subscription and region. For an ARM64 VM size, also
pass `22_04-lts-arm64` as the fifth argument; verify the image is available in
the chosen region.

Setup creates a private `.lab-state` directory, a generated SSH key and uniquely
named lab resources. It refuses to overwrite an existing state directory, and
checks Terraform's initial plan contains only expected creations. If setup
fails, preserve `.lab-state` and run `./teardown.sh` after resolving the error.

Investigate the action group and its webhook configuration. Terraform runs from
`.lab-state`; review its plan before applying a change. State and plan files
contain a **bearer webhook URL**, and Terraform may display it in a plan:
never share them, commit them, paste them
into tickets, or put them in process arguments. Use a private local Linux
filesystem with restrictive permissions, not a shared or world-readable mount.

```sh
terraform -chdir=.lab-state plan
./verify.sh
./teardown.sh
```

Verification sends an Azure Monitor **synthetic** budget notification to the
actual configured action-group receiver, then checks the lab Automation job and
VM deallocation. It can take several minutes. Teardown uses Terraform and
refuses unexpected resource groups or unmanaged resources. It keeps local state
afterward for inspection; remove the private `.lab-state` directory only after
confirming all lab resources, budget and role assignment are gone. Failed
teardown retains state. Since Azure operations and billing are asynchronous,
check Azure for residual resources and charges after cleanup.
