Skip to content

Commit

Permalink
Adding resource locks to terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
frasermolyneux committed Dec 9, 2023
1 parent e81fc68 commit 4ec9b09
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions terraform/app_data_storage.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ resource "azurerm_storage_account" "app_data_storage" {
tags = var.tags
}

resource "azurerm_management_lock" "app_data_storage_lock" {
name = "Terraform (CanNotDelete) - ${random_id.lock.hex}"
scope = azurerm_storage_account.app_data_storage.id
lock_level = "CanNotDelete"
notes = "CanNotDelete Lock managed by Terraform to prevent manual or accidental deletion of resource group and resources"
}

resource "azurerm_storage_container" "map_images_container" {
name = "map-images"

Expand Down
7 changes: 7 additions & 0 deletions terraform/common.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ resource "azurerm_resource_group" "rg" {

tags = var.tags
}

resource "azurerm_management_lock" "rg_lock" {
name = "Terraform (CanNotDelete) - ${random_id.lock.hex}"
scope = azurerm_resource_group.rg.id
lock_level = "CanNotDelete"
notes = "CanNotDelete Lock managed by Terraform to prevent manual or accidental deletion of resource group and resources"
}
7 changes: 7 additions & 0 deletions terraform/key_vault.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ resource "azurerm_key_vault" "kv" {
}
}

resource "azurerm_management_lock" "kv_lock" {
name = "Terraform (CanNotDelete) - ${random_id.lock.hex}"
scope = azurerm_key_vault.kv.id
lock_level = "CanNotDelete"
notes = "CanNotDelete Lock managed by Terraform to prevent manual or accidental deletion of resource group and resources"
}

resource "azurerm_role_assignment" "apim_kv_role_assignment" {
scope = azurerm_key_vault.kv.id
role_definition_name = "Key Vault Secrets User"
Expand Down
7 changes: 7 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ resource "random_id" "environment_id" {
resource "time_rotating" "thirty_days" {
rotation_days = 30
}

resource "random_id" "lock" {
keepers = {
id = "${timestamp()}"
}
byte_length = 8
}
7 changes: 7 additions & 0 deletions terraform/sql_database.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ resource "azurerm_mssql_database" "repo" {

max_size_gb = 2
}

resource "azurerm_management_lock" "repo_lock" {
name = "Terraform (CanNotDelete) - ${random_id.lock.hex}"
scope = azurerm_mssql_database.repo.id
lock_level = "CanNotDelete"
notes = "CanNotDelete Lock managed by Terraform to prevent manual or accidental deletion of resource group and resources"
}

0 comments on commit 4ec9b09

Please sign in to comment.