Skip to content

Commit c7c334a

Browse files
committed
entra ID template app registration
1 parent 6d5e71a commit c7c334a

8 files changed

Lines changed: 242 additions & 0 deletions

File tree

4_identity-security/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Last updated: 2026-02-03
1212
> [!IMPORTANT]
1313
> This folder contains sample Terraform templates for Azure identity and security services. These templates are starting points and should be customized based on your application needs.
1414
15+
## Templates available
16+
17+
- [Microsoft Entra ID (Entra ID)](./entra_id)
18+
1519
<!-- START BADGE -->
1620
<div align="center">
1721
<img src="https://img.shields.io/badge/Total%20views-1283-limegreen" alt="Total views">
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Terraform Template - Microsoft Entra ID (Entra ID)
2+
3+
Costa Rica
4+
5+
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
6+
[brown9804](https://github.com/brown9804)
7+
8+
Last updated: 2026-02-03
9+
10+
------------------------------------------
11+
12+
> High level: creates a Microsoft Entra ID application registration (tenant-level). Optionally creates a service principal, a client secret, and (optionally) assigns an Azure RBAC role at a provided scope.
13+
14+
> High level: creates a Microsoft Entra ID application registration (tenant-level). Optionally creates a service principal and a client secret.
15+
16+
> No Azure resource group is required unless you choose a resource-group RBAC scope (because the RG must already exist for role assignment).
17+
18+
## File Descriptions
19+
20+
- **main.tf**: Creates the application registration, optional service principal, and optional client secret.
21+
- **variables.tf**: Defines the input variables used in the Terraform configuration.
22+
- **provider.tf**: Configures the AzureRM and AzureAD providers.
23+
- **terraform.tfvars**: Provides example values for the variables defined in `variables.tf`.
24+
- **outputs.tf**: Defines outputs such as the application client ID, object IDs, and the client secret.
25+
26+
## Variables
27+
28+
Below is a list of variables used in this template, their expected values, types, and examples:
29+
30+
| Variable Name | Description | Type | Example Value |
31+
| --- | --- | --- | --- |
32+
| `app_display_name` | The display name for the Entra ID application registration. | string | `"example-entra-app"` |
33+
| `sign_in_audience` | The Microsoft account types supported for the application. | string | `"AzureADMyOrg"` |
34+
| `application_owners` | Optional set of object IDs to set as owners. If empty, the current principal is used. | set(string) | `["00000000-0000-0000-0000-000000000000"]` |
35+
| `create_service_principal` | Whether to create a service principal for the application. | bool | `true` |
36+
| `use_existing_service_principal` | Import existing service principal linked to the application when present. | bool | `true` |
37+
| `create_client_secret` | Whether to create a client secret (application password). | bool | `true` |
38+
| `client_secret_display_name` | Display name for the client secret. | string | `"terraform-client-secret"` |
39+
| `client_secret_end_date_relative` | Relative duration for which the secret is valid. | string | `"4320h"` |
40+
41+
## Usage
42+
43+
1. Authenticate:
44+
45+
```sh
46+
az login
47+
```
48+
49+
2. Initialize and apply:
50+
51+
```sh
52+
terraform init -upgrade
53+
terraform apply -auto-approve
54+
```
55+
56+
Keep your `terraform.tfvars` minimal: set only `app_display_name`, and explicitly opt-in to optional resources (service principal, client secret, RBAC scope) when you need them.
57+
58+
3. Validate and plan:
59+
60+
```sh
61+
terraform validate
62+
terraform plan
63+
```
64+
65+
4. Apply:
66+
67+
```sh
68+
terraform apply -auto-approve
69+
```
70+
71+
If you need Azure RBAC role assignments, use an AzureRM-based template (requires a subscription context and scope such as a subscription or resource group).
72+
73+
> These `TF_VAR_*` environment variables are scoped to your current shell session.
74+
75+
## Notes
76+
77+
- Creating applications, service principals, and secrets requires Microsoft Entra ID permissions (e.g., Application Administrator or appropriate Microsoft Graph application roles).
78+
- This template does not create Azure resources and does not require a resource group.
79+
80+
<!-- START BADGE -->
81+
<div align="center">
82+
<img src="https://img.shields.io/badge/Total%20views-1283-limegreen" alt="Total views">
83+
<p>Refresh Date: 2026-02-03</p>
84+
</div>
85+
<!-- END BADGE -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# main.tf
2+
# High level: creates a Microsoft Entra ID application registration (tenant-level).
3+
# Optionally creates a service principal and a client secret.
4+
# Azure RBAC assignment is intentionally not included in this template (it requires the AzureRM provider and an Azure subscription context).
5+
6+
data "azuread_client_config" "current" {}
7+
8+
locals {
9+
effective_owners = length(var.application_owners) > 0 ? var.application_owners : toset([data.azuread_client_config.current.object_id])
10+
}
11+
12+
resource "azuread_application" "this" {
13+
display_name = var.app_display_name
14+
owners = local.effective_owners
15+
sign_in_audience = var.sign_in_audience
16+
}
17+
18+
resource "azuread_service_principal" "this" {
19+
count = var.create_service_principal ? 1 : 0
20+
client_id = azuread_application.this.client_id
21+
owners = local.effective_owners
22+
use_existing = var.use_existing_service_principal
23+
app_role_assignment_required = false
24+
}
25+
26+
resource "azuread_application_password" "this" {
27+
count = var.create_client_secret ? 1 : 0
28+
application_id = azuread_application.this.id
29+
display_name = var.client_secret_display_name
30+
end_date = timeadd(timestamp(), var.client_secret_end_date_relative)
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
output "tenant_id" {
2+
description = "The Microsoft Entra ID (Entra ID) tenant ID"
3+
value = data.azuread_client_config.current.tenant_id
4+
}
5+
6+
output "application_client_id" {
7+
description = "The client ID (app ID) of the application registration"
8+
value = azuread_application.this.client_id
9+
}
10+
11+
output "application_object_id" {
12+
description = "The object ID of the application registration"
13+
value = azuread_application.this.object_id
14+
}
15+
16+
output "service_principal_object_id" {
17+
description = "The object ID of the service principal (if created)"
18+
value = try(azuread_service_principal.this[0].object_id, null)
19+
}
20+
21+
output "client_secret" {
22+
description = "The generated client secret value (if created)"
23+
value = try(azuread_application_password.this[0].value, null)
24+
sensitive = true
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# provider.tf
2+
# This file configures the AzureRM and AzureAD (Microsoft Entra ID) providers.
3+
# It specifies the required providers and versions, along with provider-specific configurations.
4+
5+
terraform {
6+
required_version = ">= 1.8, < 2.0"
7+
8+
required_providers {
9+
azuread = {
10+
source = "hashicorp/azuread"
11+
version = "~> 3.0"
12+
}
13+
}
14+
}
15+
16+
provider "azuread" {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# terraform.tfvars
2+
# High level: this module creates an Entra ID app registration and (optionally) a service principal, client secret, and Azure RBAC role assignment.
3+
# This file provides example values for the variables defined in variables.tf.
4+
# Tip: IDs come from your current Azure CLI session (PowerShell):
5+
# $env:ARM_SUBSCRIPTION_ID = (az account show --query id -o tsv)
6+
# $env:ARM_TENANT_ID = (az account show --query tenantId -o tsv)
7+
8+
# Required
9+
app_display_name = "eg-brown-entra-app"
10+
11+
# Optional (opt-in)
12+
# Create a service principal for the app registration
13+
create_service_principal = false
14+
15+
# Create a client secret (only applies if you manage secrets via Terraform)
16+
create_client_secret = false
17+
18+
# Optional: set owners explicitly (object IDs). If empty, current principal becomes owner.
19+
# application_owners = ["00000000-0000-0000-0000-000000000000"]
20+
21+
# If create_client_secret = true
22+
# client_secret_display_name = "terraform-client-secret"
23+
# client_secret_end_date_relative = "4320h"
24+
25+
# Optional: assign RBAC role to the service principal
26+
# Azure RBAC is not part of this Entra-only template.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
variable "app_display_name" {
2+
description = "The display name for the Entra ID application registration"
3+
type = string
4+
}
5+
6+
variable "sign_in_audience" {
7+
description = "The Microsoft account types that are supported for the application"
8+
type = string
9+
default = "AzureADMyOrg"
10+
}
11+
12+
variable "application_owners" {
13+
description = "Optional set of object IDs to set as owners for the application (users or service principals). If empty, the current principal is used."
14+
type = set(string)
15+
default = []
16+
}
17+
18+
variable "create_service_principal" {
19+
description = "Whether to create a service principal for the application"
20+
type = bool
21+
default = true
22+
}
23+
24+
variable "use_existing_service_principal" {
25+
description = "When true, an existing service principal linked to the application will be automatically imported"
26+
type = bool
27+
default = true
28+
}
29+
30+
variable "create_client_secret" {
31+
description = "Whether to create a client secret (application password)"
32+
type = bool
33+
default = true
34+
}
35+
36+
variable "client_secret_display_name" {
37+
description = "Display name for the client secret (application password)"
38+
type = string
39+
default = "terraform-client-secret"
40+
}
41+
42+
variable "client_secret_end_date_relative" {
43+
description = "Relative duration for which the client secret is valid (e.g. 240h for 10 days, 4320h for 180 days)"
44+
type = string
45+
default = "4320h"
46+
}
47+

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ Last updated: 2026-02-03
9090

9191
</details>
9292

93+
<details>
94+
<summary><b> Identity and Security </b> (Click to expand) </summary>
95+
96+
- [Identity and Security](./4_identity-security)
97+
- [Microsoft Entra ID (Entra ID)](./4_identity-security/entra_id)
98+
99+
</details>
100+
93101
<details>
94102
<summary><b> Migration and Backup </b> (Click to expand) </summary>
95103

0 commit comments

Comments
 (0)