Azure CLI
Set up the Infracost Azure integration with the setup scripts from the terraform-azure-integration repository. The scripts use the az CLI, for teams that do not want to run Terraform. They give the same result as the Terraform module. Prefer infrastructure as code? See the Terraform guide instead. It is easier to keep up to date.
This integration requires admin access to your Microsoft Entra ID tenant, to the management groups that Infracost reads, and to your billing account. Reach out to support@infracost.io before getting started if you have questions.
There are two scripts with the same options and the same steps:
scripts/linux/setup.sh, for bash 3.2 or later on Linux, macOS, WSL or Azure Cloud Shell.scripts/windows/setup.ps1, for PowerShell.
You can run each script more than once. The setup script only creates the items that do not exist.
Prerequisites
-
The Azure CLI.
-
For bash:
sha1sumorshasum,od, anddate. The script checks for these tools and forazbefore it starts. -
The Infracost app client ID — in Infracost Cloud, go to Org Settings > Integrations > Add integrations, click Microsoft Azure, and copy the Infracost app client ID.
-
Your billing account ID:
- Enterprise Agreement (EA): the enrollment number, for example
12345678. - Microsoft Customer Agreement (MCA): the billing account ID, in the format
<guid>:<guid>_<yyyy-mm-dd>.
In the Azure portal, go to Cost Management + Billing > Billing scopes, select the billing account, then Properties.
- Enterprise Agreement (EA): the enrollment number, for example
-
These permissions for the principal that runs the script:
| Step | Permission |
|---|---|
| Service principal | Application Administrator, Cloud Application Administrator, or Application.ReadWrite.All |
| Management group roles | Owner or User Access Administrator on each management group |
| Billing role, EA | Enterprise Administrator on the enrollment |
| Billing role, MCA | Billing account owner |
| Billing exports | Owner on the subscription, and billing account access to create the cost export |
Step 1: Get the scripts
Clone the repository at the latest release. Each setup script loads the common file from its directory, so keep the files of a directory together.
git clone --branch v0.6.0 https://github.com/infracost/terraform-azure-integration.git
cd terraform-azure-integration
Step 2: Sign in
Sign in to the tenant to set up, and select the subscription that will host the billing exports storage account:
az login --tenant <tenant-id>
az account set --subscription <subscription-id>
Pass --tenant-id and --subscription to the script. The script then stops if the az CLI targets a different tenant or subscription. This is important when you can sign in to more than one tenant, for example as an EA administrator with a different home tenant.
Step 3: Run the setup script
Do a dry run first. The script reads the current state, checks your permissions, and prints each change as a [dry-run] line, but does not apply it. Then run the same command without --dry-run (or -DryRun). The script then prints each change as an [apply] line before it applies it.
The command below sets up the integration on the tenant root management group, which covers all subscriptions in the tenant. For MCA, set --billing-agreement-type MCA and --billing-account-id '<guid>:<guid>_<yyyy-mm-dd>'.
- bash
- PowerShell
./scripts/linux/setup.sh --dry-run \
--client-id <infracost-client-id> \
--tenant-id <tenant-id> \
--subscription <subscription-id> \
--billing-agreement-type EA \
--billing-account-id 12345678 \
--location westeurope
.\scripts\windows\setup.ps1 -DryRun `
-ClientId <infracost-client-id> `
-TenantId <tenant-id> `
-Subscription <subscription-id> `
-BillingAgreementType EA `
-BillingAccountId 12345678 `
-Location westeurope
If Windows blocks the script, run it with powershell -ExecutionPolicy Bypass -File .\scripts\windows\setup.ps1 ....
The script:
- Creates a service principal of the Infracost multi-tenant application in your tenant.
- Assigns the
Reader,Cost Management ReaderandMonitoring Readerroles to it on each management group. - Assigns a billing reader role on the billing account:
EnrollmentReaderfor EA,Billing account readerfor MCA. - Registers the
Microsoft.StorageandMicrosoft.CostManagementresource providers, and creates theinfracost-billing-exportsresource group, a storage account, and theinfracost-exportsblob container. - Creates a daily FOCUS 1.2 cost export of the billing account into the container.
- Assigns the
Storage Blob Data Readerrole to Infracost on the container.
Infracost reads the export periodically, which may incur minor Azure storage transaction costs. Azure storage costs also apply.
The export runs daily from the day after the setup. Azure can take up to 24 hours to write the first export into the container.
Preflight
Before the first change, the script checks that you can do all the steps. The preflight also runs with --dry-run. If a check fails, the script stops before it changes anything and lists the missing permissions.
The preflight cannot find all problems, so the run can still fail. For example: conditions on role assignments, deny assignments, roles that you received a few minutes before the run, or billing roles that you get through a group. A role that is eligible in Privileged Identity Management, but not active, is reported as missing. Activate it and run the script again. If the preflight reports a permission as missing but you know you have it (for example, through a custom directory role), use --skip-preflight.
Options
| Option | Description | Default |
|---|---|---|
--client-id <guid> | Client ID of the Infracost Azure application. Required. | |
--tenant-id <guid> | Tenant to use. The script stops if the az CLI targets a different tenant. | Tenant of the az CLI |
--subscription <guid> | Subscription that hosts the billing exports. The script stops if the az CLI targets a different subscription. | Default subscription of the az CLI |
--management-groups <a,b> | Management groups where Infracost gets read access. | Tenant root management group |
--roles <a,b> | Roles on the management groups. | Reader,Cost Management Reader,Monitoring Reader |
--owners <oid,oid> | Object IDs to add as owners of the service principal. | No owners |
--billing-agreement-type <EA|MCA> | Billing agreement of the billing account. | |
--billing-account-id <id> | EA enrollment number, or MCA billing account ID. | |
--location <region> | Region of the storage account and cost export. | |
--resource-group <name> | Resource group of the storage account. | infracost-billing-exports |
--storage-account <name> | Name of the storage account. | infracost + 15 characters from the tenant ID hash |
--existing-export <id,container,path> | Use a FOCUS 1.2 cost export that exists. | |
--dry-run | Print the changes. Do not apply them. | |
--skip-preflight | Do not check the permissions before the changes. |
The PowerShell script uses the same options in PascalCase, for example --client-id becomes -ClientId. For the full help, run ./scripts/linux/setup.sh --help or Get-Help .\scripts\windows\setup.ps1 -Full.
Optional: Limit access to some management groups
Add --management-groups platform,workloads (or -ManagementGroups platform, workloads) to give Infracost read access on these management groups only.
Optional: Provide an existing cost export instead
Use this option if you prefer to manage your own cost export. The script does not create a storage account or a cost export. It only gives Infracost read access to the container of your export. When doing so, please contact support@infracost.io to ensure your use case and configuration are supported.
If you create the export manually (for example, in the Azure portal under Cost Management > Exports), configure it with the following settings so Infracost can ingest it:
Manual FOCUS 1.2 settings
| Setting | Value |
|---|---|
| Scope | Billing account |
| Export type | Cost and usage (FOCUS 1.2) |
| Dataset version | 1.2-preview |
| Frequency | Daily export of month-to-date costs |
| Format | Parquet |
| Compression | Snappy |
| File partitioning | On |
| Overwrite data | On |
- bash
- PowerShell
./scripts/linux/setup.sh --dry-run \
--client-id <infracost-client-id> \
--tenant-id <tenant-id> \
--billing-agreement-type EA \
--billing-account-id 12345678 \
--existing-export '/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<name>,<container>,<root-path>'
.\scripts\windows\setup.ps1 -DryRun `
-ClientId <infracost-client-id> `
-TenantId <tenant-id> `
-BillingAgreementType EA `
-BillingAccountId 12345678 `
-ExistingExport '/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<name>,<container>,<root-path>'
Data ownership and lifecycle
Your Azure subscription retains ownership of the data. Infracost is granted solely read-only access to the export container.
The storage account created by the script has a lifecycle policy: export files move to the cold tier after 90 days, and are deleted after 365 days.
Step 4: Add the integration in the Infracost Cloud
The script writes its progress to stderr, and the values for Infracost to stdout as JSON. To keep the values only, redirect stdout to a file, for example > infracost-azure.json.
{
"tenant_id": "...",
"service_principal_object_id": "...",
"management_group_ids": { "<name>": "/providers/Microsoft.Management/managementGroups/<name>" },
"role_assignment_ids": { "<name>/Reader": "..." },
"billing_role_assignment_id": "...",
"billing_exports": {
"storage_account_id": "...",
"container": "infracost-exports",
"root_path": "focus-cost",
"export_id": "..."
}
}
Add the integration to Infracost from the dashboard:
- Go to Org Settings > Integrations > Add integrations, then click Microsoft Azure.
- Enter the integration details:
- Tenant ID: the
tenant_idfrom the script output. - Agreement type: Enterprise Agreement (EA) or Microsoft Customer Agreement (MCA).
- Billing scope ID: the billing account ID that you gave to the script as a path,
/providers/Microsoft.Billing/billingAccounts/<billing-account-id>. - Alias (optional): a name to identify the integration.
- Tenant ID: the
- Click Save. Infracost finds the cost export on the billing scope, so you do not enter the storage account.
Upgrading
When Infracost adds new capabilities, the setup may need new permissions. Infracost will notify you when an update is required. To upgrade, get the scripts of the new release, then run the setup script again with the same options. It only creates the items that do not exist.
Don't forget to review the release notes for updates and breaking changes.
git fetch --tags
git checkout <new-version>
Removing
Run the teardown script with the same options that you gave to the setup script. It removes the items in the reverse order, and asks for confirmation first. Do a dry run first. To run without a terminal, for example in CI, add --yes (or -Yes).
- bash
- PowerShell
./scripts/linux/teardown.sh --dry-run \
--client-id <infracost-client-id> \
--tenant-id <tenant-id> \
--subscription <subscription-id> \
--billing-agreement-type EA \
--billing-account-id 12345678
.\scripts\windows\teardown.ps1 -DryRun `
-ClientId <infracost-client-id> `
-TenantId <tenant-id> `
-Subscription <subscription-id> `
-BillingAgreementType EA `
-BillingAccountId 12345678
The teardown script removes the storage account created by the setup script, with all exported data. Add --keep-storage (or -KeepStorage) to keep it. With --existing-export, the teardown script only removes the read access of Infracost. Your export and storage account stay. The resource providers stay registered.