I am trying to write Terraform that will create an Azure Storage account and then a bunch of storage containers inside it. An important detail is that the storage account has network rules that restrict access to a specific address space. This was causing the container creation to fail.
I managed to get around this by using azurerm_storage_account_network_rules
, depending on the containers so not to block their creation. Something like this:
resource "azurerm_storage_account" "this" {
name = local.storage_name
resource_group_name = azurerm_resource_group.this.name
location = var.location
account_tier = "Standard"
account_kind = "StorageV2"
is_hns_enabled = true
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "data" {
for_each = toset(var.storage_containers)
name = each.value
storage_account_name = azurerm_storage_account.this.name
container_access_type = "private"
}
# FIXME This order prevents destruction of infrastructure :(
resource "azurerm_storage_account_network_rules" "this" {
storage_account_id = azurerm_storage_account.this.id
default_action = "Deny"
bypass = ["AzureServices"]
virtual_network_subnet_ids = [
# Some address space here...
]
# NOTE The order here matters: We cannot create storage
# containers once the network rules are locked down
depends_on = [
azurerm_storage_container.data
]
}
This works for creating the infrastructure, but when I try to terraform destroy
, I get a 403 authentication error:
Error: retrieving Container "data" (Account "XXX" / Resource Group "XXX"): containers.Client#GetProperties: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error. Status=403 Code="AuthorizationFailure" Message="This request is not authorized to perform this operation.\nRequestId:XXX\nTime:XXX"
This is with my Service Principal, which has Contributor
and User Access Administrator
roles on the same subscription. Interestingly, when I'm logged in to the Azure Portal as myself (with the Owner
role), I can add and remove storage containers regardless of the network rules being present.
So, is there a way of setting the Terraform dependencies such that they can both be built and destroyed without hitting any authentication conflicts? Alternatively, would upgrading my SP's role to Owner
(or adding another, more targeted role) solve the problem?
Contributor
+User Access Administrator
is already pretty close toOwner
. I think all that's missing isMicrosoft.Blueprint/blueprintAssignments/write
,Microsoft.Blueprint/blueprintAssignments/delete
andMicrosoft.Compute/galleries/share/action
; none of which seem relevant. How is it that I am therefore free to perform these container actions in the Azure Portal? – Delectable