2022-11-16 14:19:10 -05:00
|
|
|
terraform {
|
|
|
|
required_providers {
|
|
|
|
azurerm = {
|
|
|
|
source = "hashicorp/azurerm"
|
2023-09-26 07:17:17 -04:00
|
|
|
version = "3.74.0"
|
2022-11-16 14:19:10 -05:00
|
|
|
}
|
|
|
|
azuread = {
|
|
|
|
source = "hashicorp/azuread"
|
2023-09-26 05:48:55 -04:00
|
|
|
version = "2.43.0"
|
2022-11-16 14:19:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Configure Azure resource management provider
|
|
|
|
provider "azurerm" {
|
2023-04-19 02:30:11 -04:00
|
|
|
features {
|
|
|
|
resource_group {
|
|
|
|
prevent_deletion_if_contains_resources = false
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 14:19:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Configure Azure active directory provider
|
|
|
|
provider "azuread" {
|
|
|
|
tenant_id = data.azurerm_subscription.current.tenant_id
|
|
|
|
}
|
|
|
|
|
|
|
|
# Access current subscription (available via Azure CLI)
|
|
|
|
data "azurerm_subscription" "current" {}
|
|
|
|
|
2023-12-15 04:36:58 -05:00
|
|
|
# Access current AzureAD configuration
|
2022-11-16 14:19:10 -05:00
|
|
|
data "azuread_client_config" "current" {}
|
|
|
|
|
|
|
|
# Create base resource group
|
|
|
|
resource "azurerm_resource_group" "base_resource_group" {
|
|
|
|
name = var.resource_group_name
|
2023-12-15 04:36:58 -05:00
|
|
|
location = var.location
|
2022-11-16 14:19:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create identity resource group
|
|
|
|
resource "azurerm_resource_group" "identity_resource_group" {
|
|
|
|
name = "${var.resource_group_name}-identity"
|
2023-12-15 04:36:58 -05:00
|
|
|
location = var.location
|
2022-11-16 14:19:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create managed identity
|
|
|
|
resource "azurerm_user_assigned_identity" "identity_uami" {
|
2023-12-15 04:36:58 -05:00
|
|
|
location = var.location
|
2022-11-16 14:19:10 -05:00
|
|
|
name = var.service_principal_name
|
|
|
|
resource_group_name = azurerm_resource_group.identity_resource_group.name
|
|
|
|
}
|
|
|
|
|
|
|
|
# Assign roles to managed identity
|
|
|
|
resource "azurerm_role_assignment" "virtual_machine_contributor_role" {
|
2023-02-22 03:29:24 -05:00
|
|
|
scope = azurerm_resource_group.base_resource_group.id
|
2022-11-16 14:19:10 -05:00
|
|
|
role_definition_name = "Virtual Machine Contributor"
|
|
|
|
principal_id = azurerm_user_assigned_identity.identity_uami.principal_id
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_role_assignment" "application_insights_component_contributor_role" {
|
2023-02-22 03:29:24 -05:00
|
|
|
scope = azurerm_resource_group.base_resource_group.id
|
2022-11-16 14:19:10 -05:00
|
|
|
role_definition_name = "Application Insights Component Contributor"
|
|
|
|
principal_id = azurerm_user_assigned_identity.identity_uami.principal_id
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:00:13 -04:00
|
|
|
resource "azurerm_role_assignment" "uami_owner_role" {
|
|
|
|
scope = azurerm_resource_group.base_resource_group.id
|
|
|
|
role_definition_name = "Owner"
|
|
|
|
principal_id = azurerm_user_assigned_identity.identity_uami.principal_id
|
|
|
|
}
|