Skip to main content

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.

note

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: sha1sum or shasum, od, and date. The script checks for these tools and for az before 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.

  • These permissions for the principal that runs the script:

StepPermission
Service principalApplication Administrator, Cloud Application Administrator, or Application.ReadWrite.All
Management group rolesOwner or User Access Administrator on each management group
Billing role, EAEnterprise Administrator on the enrollment
Billing role, MCABilling account owner
Billing exportsOwner 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>'.

./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

The script:

  1. Creates a service principal of the Infracost multi-tenant application in your tenant.
  2. Assigns the Reader, Cost Management Reader and Monitoring Reader roles to it on each management group.
  3. Assigns a billing reader role on the billing account: EnrollmentReader for EA, Billing account reader for MCA.
  4. Registers the Microsoft.Storage and Microsoft.CostManagement resource providers, and creates the infracost-billing-exports resource group, a storage account, and the infracost-exports blob container.
  5. Creates a daily FOCUS 1.2 cost export of the billing account into the container.
  6. Assigns the Storage Blob Data Reader role to Infracost on the container.
note

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​

OptionDescriptionDefault
--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-runPrint the changes. Do not apply them.
--skip-preflightDo 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
SettingValue
ScopeBilling account
Export typeCost and usage (FOCUS 1.2)
Dataset version1.2-preview
FrequencyDaily export of month-to-date costs
FormatParquet
CompressionSnappy
File partitioningOn
Overwrite dataOn
./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>'

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:

  1. Go to Org Settings > Integrations > Add integrations, then click Microsoft Azure.
  2. Enter the integration details:
    • Tenant ID: the tenant_id from 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.
  3. 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.

tip

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).

./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
caution

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.