Azure Citadel
  • Blogs

  • Azure Arc
    • Overview
    • Azure Arc-enabled Kubernetes
      • Prereqs
      • Background
      • Deploy Cluster
      • Connect to Arc
      • Enable GitOps
      • Deploy Application
      • Enable Azure AD
      • Enforce Policy
      • Enable Monitoring
      • Enable Azure Defender
      • Enable Data Services
      • Enable Application Delivery
    • Azure Arc-enabled Servers
      • Prereqs
      • Scenario
      • Hack Overview
      • Azure Landing Zone
      • Arc Pilot resource group
      • Azure Monitoring Agent
      • Additional policy assignments
      • Access your on prem VMs
      • Create onboarding scripts
      • Onboarding using scripts
      • Inventory
      • Monitoring
      • SSH
      • Windows Admin Center
      • Governance
      • Custom Script Extension
      • Key Vault Extension
      • Managed Identity
    • Useful Links
  • Azure CLI
    • Install
    • Get started
    • JMESPATH queries
    • Integrate with Bash
  • Azure landing zone
    • ALZ Accelerator
      • Prereqs
      • Elevate
      • Bootstrap
      • Demote
      • Components
    • Deploy an Azure landing zone
      • Create an initial ALZ config
      • Add a local override library
      • Test locally
      • Run through the CI/CD workflow
    • Libraries
      • What is a library?
      • Policies, Assignments and Roles
      • Archetypes, Overrides and Architecture
      • Metadata and Policy Default Values
      • Custom libraries
    • Example Library Configs
      • Azure landing zone library
      • Azure landing zone library with overrides
  • Azure Lighthouse
    • Minimal Lighthouse definition
    • Using service principals
    • Privileged Identity Management
  • Azure Policy
    • Azure Policy Basics
      • Policy Basics in the Azure Portal
      • Creating Policy via the CLI
      • Deploy If Not Exists
      • Management Groups and Initiatives
    • Creating Custom Policies
      • Customer scenario
      • Policy Aliases
      • Determine the logic
      • Create the custom policy
      • Define, assign and test
  • Marketplace
    • Introduction
      • Terminology
      • Offer Types
    • Partner Center
    • Offer Type
    • Publish a VM Offer HOL
      • Getting Started
      • Create VM Image
      • Test VM Image
      • VM Offer with SIG
      • VM Offer with SAS
      • Publish Offer
      • Other VM Resources
    • Publish a Solution Template HOL
      • Getting Started
      • Create ARM Template
      • Validate ARM Template
      • Create UI Definition
      • Package Assets
      • Publish Offer
    • Publish a Managed App HOL
      • Getting Started
      • Create ARM Template
      • Validate ARM Template
      • Create UI Definition
      • Package Assets
      • Publish Offer
    • Managed Apps with AKS HOL
    • Other Managed App Resources
    • SaaS Offer HOLs
    • SaaS Offer Video Series
      • Video 1 - SaaS Offer Overview
      • Video 2 - Purchasing a SaaS Offer
      • Video 3 - Purchasing a Private SaaS Plan
      • Video 4 - Publishing a SaaS Offer
      • Video 5 - Publishing a Private SaaS Plan
      • Video 6 - SaaS Offer Technical Overview
      • Video 7 - Azure AD Application Registrations
      • Video 8 - Using the SaaS Offer REST Fulfillment API
      • Video 9 - The SaaS Client Library for .NET
      • Video 10 - Building a Simple SaaS Landing Page in .NET
      • Video 11 - Building a Simple SaaS Publisher Portal in .NET
      • Video 12 - SaaS Webhook Overview
      • Video 13 - Implementing a Simple SaaS Webhook in .NET
      • Video 14 - Securing a Simple SaaS Webhook in .NET
      • Video 15 - SaaS Metered Billing Overview
      • Video 16 - The SaaS Metered Billing API with REST
  • Microsoft Fabric
    • Theory
    • Prereqs
    • Fabric Capacity
    • Set up a Remote State
    • Create a repo from a GitHub template
    • Configure an app reg for development
    • Initial Terraform workflow
    • Expanding your config
    • Configure a workload identity
    • GitHub Actions for Microsoft Fabric
    • GitLab pipeline for Microsoft Fabric
  • Packer & Ansible
    • Packer
    • Ansible
    • Dynamic Inventories
    • Playbooks & Roles
    • Custom Roles
    • Shared Image Gallery
  • Partner Admin Link
    • Understanding PAL
    • Service principals with credentials
    • PAL tagging with a service principal
    • CI/CD pipelines & OpenID Connect
    • Using AzAPI in Terraform
    • User and guest IDs
    • Azure Lighthouse & PAL
    • PAL FAQ
  • REST API
    • REST API theory
    • Using az rest
  • Setup
  • Sovereign landing zone
    • ALZ Accelerator
      • Prereqs
      • Elevate
      • Bootstrap
      • Demote
      • Components
    • Deploy Sovereign landing zone
      • Create an initial SLZ config
      • Add a local override library
      • Test locally
      • Run through the CI/CD workflow
    • Libraries
      • What is a library?
      • Policies, Assignments and Roles
      • Archetypes, Overrides and Architecture
      • Metadata and Policy Default Values
      • Custom libraries
    • Reference Library Configs
      • Sovereign landing zone
      • Sovereign landing zone library with overrides
      • SLZ extended with a country pack
  • Terraform
    • Fundamentals
      • Initialise
      • Format
      • Validate
      • Plan
      • Apply
      • Adding resources
      • Locals and outputs
      • Managing state
      • Importing resources
      • Destroy
    • Get set up for Terraform
      • Cloud Shell
      • macOS
      • Windows with PowerShell
      • Windows with Ubuntu in WSL2
    • Using AzAPI
      • Using the REST API
      • azapi_resource
      • Removing azapi_resource
      • azapi_update_resource
      • Data sources and outputs
      • Removing azapi_update_resource
  • Virtual Machines
    • Azure Bastion with native tools & AAD
    • Managed Identities

  • About
  • Archive
  1. Home
  2. Partner Admin Link
  3. Using AzAPI in Terraform
Using AzAPI in Terraform
Using AzAPI in Terraform
Partner Admin Link
Understanding PAL
Service principals with credentials
PAL tagging with a service principal
CI/CD pipelines & OpenID Connect
Using AzAPI in Terraform
User and guest IDs
Azure Lighthouse & PAL
PAL FAQ
  • Introduction
  • Example Terraform config
  • Example Terraform run
  • Equivalent Azure CLI commands

Using AzAPI in Terraform

Here is an example Terraform config to create the Partner Admin Link using azapi_resource_action, useful in CI/CD and subscription vending machine situations.

Table of Contents

  • Introduction
  • Example Terraform config
  • Example Terraform run
  • Equivalent Azure CLI commands

Introduction

You can also create a Partner Admin Link easily if you have a Terraform config that runs as a service principal or managed identity.

Remember that each security principal can be linked to only one partner ID.

The examples use 314159 as a placeholder as I won’t be sharing the partner ID that I use for testing!

Example Terraform config

There are multiple ways for azapi to authenticate as a service principal or managed identity.

This example is based on Authentication: Authenticating via a Service Principal and a Client Secret, and assumes that you have exported ARM_TENANT_ID, ARM_CLIENT_ID, and ARM_CLIENT_SECRET.

terraform {
  required_providers {
    azapi = {
      source  = "azure/azapi"
      version = ">= 2.8.0"
    }
  }
}

provider "azapi" {}

variable "partner_id" {
  type    = string
  default = "314159"
}

resource "azapi_resource_action" "pal" {
  type        = "Microsoft.ManagementPartner/partners@2018-02-01"
  resource_id = "/providers/Microsoft.ManagementPartner/partners/${var.partner_id}"
  method      = "PUT"
  when        = "apply"

  body = {
    partnerId = var.partner_id
  }

  response_export_values = {
    id          = "id"
    objectId    = "properties.objectId"
    partnerName = "properties.partnerName"
    partnerId   = "properties.partnerId"
  }
}

resource "azapi_resource_action" "pal_destroy" {
  type        = "Microsoft.ManagementPartner/partners@2018-02-01"
  resource_id = "/providers/Microsoft.ManagementPartner/partners/${var.partner_id}"
  method      = "DELETE"
  when        = "destroy"

  depends_on = [azapi_resource_action.pal]
}

output "pal" {
  value = azapi_resource_action.pal.output
}

Example Terraform run

  1. Initialise

    terraform init
    
    Expected output:
    Initializing the backend...
    Initializing provider plugins...
    - Reusing previous version of azure/azapi from the dependency lock file
    - Installing azure/azapi v2.8.0...
    - Installed azure/azapi v2.8.0 (signed by a HashiCorp partner, key ID 6F0B91BDE98478CF)
    Partner and community providers are signed by their developers.
    If you'd like to know more about provider signing, you can read about it here:
    https://developer.hashicorp.com/terraform/cli/plugins/signing
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
    
  2. Validate

    terraform validate
    
    Expected output:
    Success! The configuration is valid.
    
  3. Plan

    terraform plan
    
    Expected output:
    Terraform used the selected providers to generate the following execution
    plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # azapi_resource_action.pal will be created
      + resource "azapi_resource_action" "pal" {
          + body                   = {
              + partnerId = "314159"
            }
          + id                     = (known after apply)
          + method                 = "PUT"
          + output                 = (known after apply)
          + resource_id            = "/providers/Microsoft.ManagementPartner/partners/314159"
          + response_export_values = {
              + id          = "id"
              + objectId    = "properties.objectId"
              + partnerId   = "properties.partnerId"
              + partnerName = "properties.partnerName"
            }
          + sensitive_output       = (sensitive value)
          + type                   = "Microsoft.ManagementPartner/partners@2018-02-01"
          + when                   = "apply"
        }
    
      # azapi_resource_action.pal_destroy will be created
      + resource "azapi_resource_action" "pal_destroy" {
          + id               = (known after apply)
          + method           = "DELETE"
          + output           = (known after apply)
          + resource_id      = "/providers/Microsoft.ManagementPartner/partners/314159"
          + sensitive_output = (sensitive value)
          + type             = "Microsoft.ManagementPartner/partners@2018-02-01"
          + when             = "destroy"
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
    Changes to Outputs:
      + pal = (known after apply)
    
    ─────────────────────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't
    guarantee to take exactly these actions if you run "terraform apply" now.
    
  4. Apply

    terraform apply
    
    Expected output:
    - snip -
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
    Changes to Outputs:
      + pal = (known after apply)
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
    yes
      Enter a value:
    azapi_resource_action.pal: Creating...
    azapi_resource_action.pal: Still creating... [00m10s elapsed]
    azapi_resource_action.pal: Creation complete after 15s [id=/providers/Microsoft.ManagementPartner/partners/314159]
    azapi_resource_action.pal_destroy: Creating...
    azapi_resource_action.pal_destroy: Creation complete after 0s [id=/providers/Microsoft.ManagementPartner/partners/314159]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    pal = {
      "id" = "/providers/microsoft.managementpartner/partners/314159"
      "objectId" = "84964f8f-22ce-4a1e-ba9d-1e45a53ca1c4"
      "partnerId" = "314159"
      "partnerName" = "Azure Citadel"
    }
    
  5. Destroy

    ⚠️ Only use the destroy switch if you are intending to remove the applied config. This is included here to demonstrate the azapi_resource_action.pal_destroy block.

    terraform destroy
    
    Expected output:
    azapi_resource_action.pal: Refreshing state... [id=/providers/Microsoft.ManagementPartner/partners/314159]
    azapi_resource_action.pal_destroy: Refreshing state... [id=/providers/Microsoft.ManagementPartner/partners/314159]
    
    Terraform used the selected providers to generate the following execution
    plan. Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # azapi_resource_action.pal will be destroyed
      - resource "azapi_resource_action" "pal" {
          - body                   = {
              - partnerId = "314159"
            } -> null
          - id                     = "/providers/Microsoft.ManagementPartner/partners/314159" -> null
          - method                 = "PUT" -> null
          - output                 = {
              - id          = "/providers/microsoft.managementpartner/partners/314159"
              - objectId    = "84964f8f-22ce-4a1e-ba9d-1e45a53ca1c4"
              - partnerId   = "314159"
              - partnerName = "Azure Citadel"
            } -> null
          - resource_id            = "/providers/Microsoft.ManagementPartner/partners/314159" -> null
          - response_export_values = {
              - id          = "id"
              - objectId    = "properties.objectId"
              - partnerId   = "properties.partnerId"
              - partnerName = "properties.partnerName"
            } -> null
          - sensitive_output       = (sensitive value) -> null
          - type                   = "Microsoft.ManagementPartner/partners@2018-02-01" -> null
          - when                   = "apply" -> null
        }
    
      # azapi_resource_action.pal_destroy will be destroyed
      - resource "azapi_resource_action" "pal_destroy" {
          - id          = "/providers/Microsoft.ManagementPartner/partners/314159" -> null
          - method      = "DELETE" -> null
          - resource_id = "/providers/Microsoft.ManagementPartner/partners/314159" -> null
          - type        = "Microsoft.ManagementPartner/partners@2018-02-01" -> null
          - when        = "destroy" -> null
        }
    
    Plan: 0 to add, 0 to change, 2 to destroy.
    
    Changes to Outputs:
      - pal = {
          - id          = "/providers/microsoft.managementpartner/partners/314159"
          - objectId    = "84964f8f-22ce-4a1e-ba9d-1e45a53ca1c4"
          - partnerId   = "314159"
          - partnerName = "Azure Citadel"
        } -> null
    
    Do you really want to destroy all resources?
      Terraform will destroy all your managed infrastructure, as shown above.
      There is no undo. Only 'yes' will be accepted to confirm.
    
    yes
      Enter a value:
    azapi_resource_action.pal_destroy: Destroying... [id=/providers/Microsoft.ManagementPartner/partners/314159]
    azapi_resource_action.pal_destroy: Still destroying... [id=/providers/Microsoft.ManagementPartner/partners/314159, 00m10s elapsed]
    azapi_resource_action.pal_destroy: Destruction complete after 15s
    azapi_resource_action.pal: Destroying... [id=/providers/Microsoft.ManagementPartner/partners/314159]
    azapi_resource_action.pal: Destruction complete after 0s
    
    Destroy complete! Resources: 2 destroyed.
    

Equivalent Azure CLI commands

These are included for reference only.

  1. Set the partner ID

    partnerId=314159
    
  2. Authenticate

    az login --service-principal --username "${ARM_CLIENT_ID}" --password "${ARM_CLIENT_SECRET}" --tenant "${ARM_TENANT_ID}" --allow-no-subscriptions
    
  3. Set the URI

    uri="https://management.azure.com/providers/microsoft.managementpartner/partners/${partnerId}?api-version=2018-02-01"
    
  4. Create the Partner Admin Link

    az rest --method PUT --url $uri --body '{"partnerId": "'${partnerId}'"}'
    
  5. Delete the Partner Admin Link

    az rest --method DELETE --url $uri
    
Source: https://www.azurecitadel.com/pal/azapi/
Published: 16 Feb 2026
Printed:
CI/CD pipelines & OpenID Connect Using AzAPI in Terraform User and guest IDs