Skip to content

Commit c23525d

Browse files
committed
managed identity template
1 parent 5cb2567 commit c23525d

7 files changed

Lines changed: 190 additions & 0 deletions

File tree

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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
## File Descriptions
15+
16+
- **main.tf**: Contains the main configuration for creating the Resource Group and the User Assigned Managed Identity.
17+
- **variables.tf**: Defines the input variables used in the Terraform configuration.
18+
- **provider.tf**: Configures the Azure provider to interact with Azure resources.
19+
- **terraform.tfvars**: Provides example values for the variables defined in `variables.tf`.
20+
- **outputs.tf**: Defines outputs such as the identity resource ID, client ID, and principal ID.
21+
22+
## Variables
23+
24+
| Variable Name | Description | Type | Example Value |
25+
| --- | --- | --- | --- |
26+
| `resource_group_name` | The name of the Azure Resource Group to create and place the identity in. | string | `"rg-identity-security-dev"` |
27+
| `location` | The Azure region where the Resource Group (and identity) will be created. | string | `"East US"` |
28+
| `managed_identity_name` | The name of the User Assigned Managed Identity to create. | string | `"id-identity-security-dev-001"` |
29+
| `tags` | A map of tags to assign to the resources. | map(string) | `{ "env": "dev" }` |
30+
31+
## Usage
32+
33+
1. Authenticate:
34+
35+
```sh
36+
az login
37+
```
38+
39+
2. Ensure Azure CLI has the correct active subscription:
40+
41+
```sh
42+
az account show
43+
# If needed:
44+
az account set --subscription "<subscription-id-or-name>"
45+
```
46+
47+
3. Initialize:
48+
49+
```sh
50+
terraform init -upgrade
51+
```
52+
53+
4. Validate and plan:
54+
55+
```sh
56+
terraform validate
57+
terraform plan
58+
```
59+
60+
5. Apply:
61+
62+
```sh
63+
terraform apply -auto-approve
64+
```
65+
66+
> [!NOTES]
67+
>
68+
> - This template creates the Resource Group for you.
69+
> - A User Assigned Managed Identity can be attached to Azure resources (VMs, App Service, Functions, etc.) and granted permissions via Azure RBAC.
70+
71+
<!-- START BADGE -->
72+
<div align="center">
73+
<img src="https://img.shields.io/badge/Total%20views-1646-limegreen" alt="Total views">
74+
<p>Refresh Date: 2026-02-09</p>
75+
</div>
76+
<!-- 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)