Terraform is one of the most popular open source "infrastructure-as-code" tool and support many cloud providers e.g. AWS, Azure, Google Cloud etc. With terraform modules, we can easily create reusable infrastructure.
The Azure AD Terraform provider can manage users, groups, service principals, and application as code.
Note: This article assumes you are well familiar with Terraform and Azure AD.
I have created modules for the below azure ad resources to manage applications and service principal in Azure Active directory:
azuread_application - Manages an application registration within Azure Active Directory.Link for the module:
Link for the module:
Link for the module:
Link for the module:
I have created Terraform code repository to consume the above modules for creating applications (link of the GitHub provided at the end), scopes, app roles, redirect URIs, service principal, password credentials, pre-authorizing client applications and API permissions.
The code creates the following scenario below:
Creates Authorizer application (web API / audience application)
Authorizer application will create:
- App roles as `Query-01.Read` and `Query-01.Write`
- Oauth2_permission_scope `TestScope_01`
- Microsoft Graph `User.Read` permission
- Identifier Uris ( # To set application uri to api//<app_id>, you need to update via script, this is not possible in terraform)
The code snippet below creates Audience application using terraform module:
Creates Authorized application (client application)
Client application will create:
- Grant application permissions for client app to access the above created web API
- Microsoft Graph User.Read delegated permission
- Redirect URIs or reply URIs where the authorization server sends the user once the app has been successfully authorized and granted an authorization code or access token. The authorization server sends the code or token to the redirect URI, so it's important you register the correct location as part of the app registration process.
The code snippet below creates Client application using same terraform module:
Creates Service principal for Authorizer and authorized applications
Service principal for audience and client app is created using module:
Creates Password credentials for client (authorized) applications
Creating password credentials for client application and storing in the existing key vault
Data source is used to access information about an existing key vault and below code snippets are used to create password credentials and store the password in the key vault. Password credentials are created using module:
Pre-authorizes client (authorized) applications
Client applications can be pre-authorized, the code snippet below does the needful using module:
Please feel free to download the code from my GitHub repository at:
Just came across your blog today while searching some AD terraform stuffs. Good Work :)
ReplyDelete