Terraform IaC - Entra ID SAML SSO and Identity Automation

Field

Details

Document Type

Automation

Applies To

Entra ID & Terraform

Audience

Entra ID engineer, Devops admin

Author

AK. Udofeh

Last Updated

March 2026

Overview

This documentation describes a reusable Terraform InfrastructureasCode (IaC) setup for automating Microsoft Entra ID (Azure AD) identity resources.

The project focuses on:

· SAML SSO application provisioning (App Registration + Service Principal + signing certificate)

· Conditional Access policies

· Groups, roles, and app role assignments

· Token protection and advanced identity features (optional modules)

· Environmentspecific configurations for dev and prod

Terraform uses the AzureAD provider to manage Entra ID objects such as applications, service principals, groups, and Conditional Access policies. AzAPI and Microsoft Graph can be layered in for advanced scenarios (claims mapping, Graphonly features).

A public GitHub repository with the full Terraform code is available here:

https://github.com/ak-wizzy/Terraform-SAML-SSO 

Repository Structure

terraform-SAML-SSO/

├── environments/
│   ├── dev/
│   │   ├── backend.tf
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   ├── providers.tf
│   │   ├── terraform.tfvars
│   │   └── variables.tf
│   │
│   └── prod/
│       ├── backend.tf
│       ├── main.tf
│       ├── outputs.tf
│       ├── providers.tf
│       ├── terraform.tfvars
│       └── variables.tf

├── modules/
│   ├── saml_sso/
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   ├── variables.tf
│   │   └── README.md
├── .gitignore
└── Readme.md

The project is organized to separate environments from modules, and to keep Terraform state isolated per environment.

Key Design Points
Components

1. SAML SSO Module (modules/saml_sso)

This module provisions everything needed for an Entra ID–backed SAML service provider:

· Outputs for:

The SAML SSO protocol configuration (reply URLs, Entity ID, and claims) is surfaced through:

Note: Microsoft does not currently offer a firstclass Terraform resource for full SAML Enterprise Application configuration; you can extend the module with AzAPI/Microsoft Graph calls if you need to automate claims mapping or the full SAML blade setup.

2. Conditional Access Module (modules/conditional_access)

This module encapsulates Conditional Access policies such as:

It typically wraps resources like azuread_conditional_access_policy.

Policies can be toggled per environment via variables, making it easy to:

3. Groups and Roles (modules/groups)

Manages:

This keeps identity governance consistent across environments, and allows app access to be managed entirely through group membership rather than static lists of users.

4. Service Principals (modules/service_principals)

Reusable module for:

This is useful when Terraform itself runs under a dedicated Entra ID service principal and you want to manage its lifecycle as code.

5. Token Protection / Advanced Modules (modules/token_protection)

Placeholder / optional module for more advanced identity features, such as:

These can be added without breaking the core SAML SSO or Conditional Access modules.

Environment Layout and Workflow

Each environment (dev, prod) wires the modules together and supplies environmentspecific values.

Typical environments/dev responsibilities:

State and Backends

Backends are typically configured per environment:

Using the Project

All commands are run from the environment directory you want to manage (e.g., environments/dev).

1. Prerequisites

2. Authenticate

For interactive use (Azure CLI):

az login
az account set --subscription "<subscription-id>"

For service principal use, run these in your terminal before your plan

$env:ARM_CLIENT_ID = "YOUR_APP_ID"
$env:ARM_CLIENT_SECRET = "YOUR_CLIENT_SECRET"
$env:ARM_TENANT_ID = "YOUR_TENANT_ID"
$env:ARM_SUBSCRIPTION_ID = "YOUR_SUBSCRIPTION_ID"

Why is this better? It’s faster, more stable for automation, and avoids the "exit status 1" errors you're seeing from the CLI wrapper.

3. Configure Environment Variables

Copy the sample tfvars and adjust for your environment:

cd environments/dev
cp terraform-sample.tfvars terraform.tfvars

Edit terraform.tfvars with values such as:

4. Initialize Terraform

terraform init

Initializes providers and backends for this environment.

5. Validate and Plan

terraform validate
terraform plan -out=tfplan

Review the generated plan to see which Entra ID resources will be created or updated.

6. Apply

terraform apply tfplan

This will:

SAML SSO Flow (What You Get After Apply)

Once the SAML SSO module has been applied:

If needed, you can extend the module with AzAPI calls to fully configure SAML attributes & claims, relay state, and other advanced settings through Microsoft Graph.

Extending and Contributing

The GitHub repository

https://github.com/ak-wizzy/terraform-entra-id

contains:

· Examples for:

Contributions are welcome via pull requests or issues:


Revision #4
Created 2026-03-25 11:47:27 UTC by AK. Udofeh
Updated 2026-04-17 16:25:40 UTC by AK. Udofeh