Skip to content

Commit d0b38a9

Browse files
authored
Merge pull request #36 from MicrosoftCloudEssentials-LearningHub/keyvault-template
managed identify + KeyVault + App registration
2 parents 5cb2567 + fde4d96 commit d0b38a9

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

4_identity-security/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Last updated: 2026-02-09
1616

1717
- [Microsoft Entra ID (Entra ID)](./entra_id)
1818
- [Azure Key Vault](./key-vault)
19+
- [User Assigned Managed Identity](./managed-identity)
1920

2021
<!-- START BADGE -->
2122
<div align="center">
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Terraform Template - User Assigned Managed Identity
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-09
9+
10+
------------------------------------------
11+
12+
> This template contains Terraform configurations to create an Azure User Assigned Managed Identity with dependencies on a Resource Group.
13+
14+
<img width="650" alt="image" src="https://github.com/user-attachments/assets/8149b211-3565-4c74-b7e0-17a15d0e3f1d" />
15+
16+
<img width="650" alt="image" src="https://github.com/user-attachments/assets/ab38d984-bd82-46d6-afc3-e11ea5175920" />
17+
18+
## File Descriptions
19+
20+
- **main.tf**: Contains the main configuration for creating the Resource Group and the User Assigned Managed Identity.
21+
- **variables.tf**: Defines the input variables used in the Terraform configuration.
22+
- **provider.tf**: Configures the Azure provider to interact with Azure resources.
23+
- **terraform.tfvars**: Provides example values for the variables defined in `variables.tf`.
24+
- **outputs.tf**: Defines outputs such as the identity resource ID, client ID, and principal ID.
25+
26+
## Variables
27+
28+
| Variable Name | Description | Type | Example Value |
29+
| --- | --- | --- | --- |
30+
| `resource_group_name` | The name of the Azure Resource Group to create and place the identity in. | string | `"rg-identity-security-dev"` |
31+
| `location` | The Azure region where the Resource Group (and identity) will be created. | string | `"East US"` |
32+
| `managed_identity_name` | The name of the User Assigned Managed Identity to create. | string | `"id-identity-security-dev-001"` |
33+
| `tags` | A map of tags to assign to the resources. | map(string) | `{ "env": "dev" }` |
34+
35+
## Usage
36+
37+
1. Authenticate:
38+
39+
```sh
40+
az login
41+
```
42+
43+
2. Ensure Azure CLI has the correct active subscription:
44+
45+
```sh
46+
az account show
47+
# If needed:
48+
az account set --subscription "<subscription-id-or-name>"
49+
```
50+
51+
3. Initialize:
52+
53+
```sh
54+
terraform init -upgrade
55+
```
56+
57+
4. Validate and plan:
58+
59+
```sh
60+
terraform validate
61+
terraform plan
62+
```
63+
64+
5. Apply:
65+
66+
```sh
67+
terraform apply -auto-approve
68+
```
69+
70+
> [!NOTES]
71+
>
72+
> - This template creates the Resource Group for you.
73+
> - A User Assigned Managed Identity can be attached to Azure resources (VMs, App Service, Functions, etc.) and granted permissions via Azure RBAC.
74+
75+
<!-- START BADGE -->
76+
<div align="center">
77+
<img src="https://img.shields.io/badge/Total%20views-1646-limegreen" alt="Total views">
78+
<p>Refresh Date: 2026-02-09</p>
79+
</div>
80+
<!-- END BADGE -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# main.tf
2+
# This file contains the main configuration for creating an Azure User Assigned Managed Identity.
3+
# It defines the resource blocks for the Azure Resource Group and the Managed Identity.
4+
5+
resource "azurerm_resource_group" "example" {
6+
name = var.resource_group_name
7+
location = var.location
8+
9+
tags = var.tags
10+
}
11+
12+
resource "azurerm_user_assigned_identity" "example" {
13+
name = var.managed_identity_name
14+
location = azurerm_resource_group.example.location
15+
resource_group_name = azurerm_resource_group.example.name
16+
17+
tags = var.tags
18+
19+
depends_on = [
20+
azurerm_resource_group.example
21+
]
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# outputs.tf
2+
# This file defines the outputs of the Terraform configuration.
3+
4+
output "managed_identity_id" {
5+
description = "The resource ID of the User Assigned Managed Identity."
6+
value = azurerm_user_assigned_identity.example.id
7+
}
8+
9+
output "managed_identity_name" {
10+
description = "The name of the User Assigned Managed Identity."
11+
value = azurerm_user_assigned_identity.example.name
12+
}
13+
14+
output "managed_identity_client_id" {
15+
description = "The client ID (application ID) of the User Assigned Managed Identity."
16+
value = azurerm_user_assigned_identity.example.client_id
17+
}
18+
19+
output "managed_identity_principal_id" {
20+
description = "The principal ID (object ID) of the User Assigned Managed Identity."
21+
value = azurerm_user_assigned_identity.example.principal_id
22+
}
23+
24+
output "resource_group_name" {
25+
description = "The name of the Resource Group created for this template."
26+
value = azurerm_resource_group.example.name
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# provider.tf
2+
# This file configures the Azure provider to interact with Azure resources.
3+
# It specifies the required provider and its version, along with provider-specific configurations.
4+
5+
terraform {
6+
required_version = ">= 1.8, < 2.0"
7+
8+
required_providers {
9+
azurerm = {
10+
source = "hashicorp/azurerm"
11+
version = "~> 3.116"
12+
}
13+
}
14+
}
15+
16+
provider "azurerm" {
17+
features {
18+
resource_group {
19+
prevent_deletion_if_contains_resources = false
20+
}
21+
}
22+
23+
# Uses the current Azure CLI context (az login + az account set)
24+
skip_provider_registration = false
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example values for the Managed Identity template
2+
3+
resource_group_name = "rg-identity-security-dev"
4+
location = "East US"
5+
managed_identity_name = "id-identity-security-dev-001"
6+
7+
tags = {
8+
env = "dev"
9+
app = "identity-security"
10+
owner = "terraform"
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# variables.tf
2+
# This file defines the input variables used in the Terraform configuration.
3+
4+
variable "resource_group_name" {
5+
description = "The name of the Azure Resource Group to create and place the Managed Identity in."
6+
type = string
7+
}
8+
9+
variable "location" {
10+
description = "The Azure region where the Resource Group (and Managed Identity) will be created."
11+
type = string
12+
}
13+
14+
variable "managed_identity_name" {
15+
description = "The name of the User Assigned Managed Identity to create."
16+
type = string
17+
18+
validation {
19+
condition = length(trimspace(var.managed_identity_name)) > 0
20+
error_message = "managed_identity_name must not be empty."
21+
}
22+
}
23+
24+
variable "tags" {
25+
description = "A map of tags to assign to the resources."
26+
type = map(string)
27+
default = {}
28+
}

0 commit comments

Comments
 (0)