I have an issue with one module requiring data from a resource which does not yet exist when I run plan (we have multiple modules and none were yet run, I am attempting to run at root level using terragrunt).
As you can see the role of the failing module is to create a service principal in azure and assign roles binding storage account to the roles using the service principal so we need information about both storage account and service principal in one place.
I am probably missing some basic way of decomposing code in terraform (I am quite new to it) so please explain.
This is the module main.tf
that fails because azurerm_storage_account
does not exist (it is created in another module).
When we run apply
all works - terraform knows that it has to create storage account first, then goes and runs the module that creates the role assignments…
main.tf which fails (only for plan, works for apply)
data "azurerm_storage_account" "main" {
name = var.storage_account_name
resource_group_name = var.resource_group_name
}
module "platform_service_app" {
source = "../ad-application"
name = "svc_${var.release_id}_platform_service"
generate_password = true
create_service_principal = true
}
resource "azurerm_role_definition" "platform_service_role" {
name = "${var.release_id}-webservices-servicerole"
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}"
description = "Provides read/write/delete access to Azure Blob Storage."
permissions {
actions = [
"Microsoft.Storage/storageAccounts/blobServices/containers/read",
"Microsoft.Storage/storageAccounts/blobServices/generateUserDelegationKey/action"
]
data_actions = [
"Microsoft.Storage/storageAccounts/blobServices/containers/blobs/delete",
"Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read",
"Microsoft.Storage/storageAccounts/blobServices/containers/blobs/move/action",
"Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
]
}
assignable_scopes = [
data.azurerm_subscription.current.id,
data.azurerm_storage_account.main.id
]
}
resource "azurerm_role_assignment" "platform_service_to_storage_account" {
scope = data.azurerm_storage_account.main.id
role_definition_id = azurerm_role_definition.platform_service_role.role_definition_resource_id
principal_id = module.platform_service_app.service_principal_id
}
And the error:
Error: Storage Account: (Name "some name here" / Resource Group "some resource group") was not found
│ with data.azurerm_storage_account.main,
│ on main.tf line 1, in data "azurerm_storage_account" "main":
│ 1: data "azurerm_storage_account" "main" {