|
| 1 | +data "azurerm_client_config" "current" {} |
| 2 | + |
| 3 | +locals { |
| 4 | + name_template = "${var.resource_prefix}-<service>-iac" |
| 5 | +} |
| 6 | + |
| 7 | +# Resource Group |
| 8 | +resource "azurerm_resource_group" "rg" { |
| 9 | + name = replace(local.name_template, "<service>", "rg") |
| 10 | + location = var.default_location |
| 11 | +} |
| 12 | + |
| 13 | +resource "azuread_group" "group-rg-contributor" { |
| 14 | + display_name = format("%s-contributor", azurerm_resource_group.rg.name) |
| 15 | + prevent_duplicate_names = true |
| 16 | + security_enabled = true |
| 17 | + members = [data.azurerm_client_config.current.object_id] |
| 18 | + lifecycle { |
| 19 | + # apart from setting initially; do not flag changes in members and owners as state change |
| 20 | + ignore_changes = [members, owners] |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +# Storage Account |
| 25 | +resource "azurerm_storage_account" "sa" { |
| 26 | + resource_group_name = azurerm_resource_group.rg.name |
| 27 | + name = format("%s03", "storiac") |
| 28 | + location = var.default_location |
| 29 | + account_tier = "Standard" |
| 30 | + account_replication_type = "GRS" |
| 31 | + shared_access_key_enabled = false |
| 32 | + default_to_oauth_authentication = true |
| 33 | + min_tls_version = "TLS1_2" |
| 34 | +} |
| 35 | + |
| 36 | +# container iac-state |
| 37 | +resource "azurerm_storage_container" "tfstate" { |
| 38 | + name = "tfstate" |
| 39 | + storage_account_id = azurerm_storage_account.sa.id |
| 40 | +} |
| 41 | + |
| 42 | +resource "azurerm_role_assignment" "blob-data-owner" { |
| 43 | + scope = azurerm_resource_group.rg.id |
| 44 | + role_definition_name = "Storage Blob Data Owner" |
| 45 | + principal_id = azuread_group.group-rg-contributor.object_id |
| 46 | +} |
| 47 | + |
| 48 | +resource "azurerm_role_assignment" "rg-contributor" { |
| 49 | + scope = azurerm_resource_group.rg.id |
| 50 | + role_definition_name = "Contributor" |
| 51 | + principal_id = azuread_group.group-rg-contributor.object_id |
| 52 | +} |
0 commit comments