# saif-resources / storage

Blob, queue, and file storage module. Wraps `iac-azure-modules` storageaccount (blob) with native `azurerm_storage_queue`/`azurerm_storage_share` resources for queue and file services, plus private endpoints and RBAC via App Registration SP.

## Usage

```hcl
module "storage" {
  source  = "tf.pulumi.com/saif-corp/resources/saif//modules/storage"
  version = "~> 3.10.0"

  context  = module.environment.context
  identity = module.identity.identity

  resource_group_name     = module.resource_group.resource_group_name
  resource_group_location = module.resource_group.location

  containers             = ["documents", "uploads"]
  connection_string_name = "storage"

  # Optional — queue/file are independently opt-in.
  queues      = ["document-intake"]
  file_shares = [{ name = "shared-config", quota_gb = 50 }]
}

# Compose app_settings
module "api" {
  app_settings = merge(
    module.storage.app_settings,
    # ...
  )
}
```

## Inputs

| Name                            | Description                                        | Required |
| -------------------------------- | ---------------------------------------------------- | -------- |
| `context`                        | Platform context from `environment` module            | yes      |
| `identity`                       | Identity bundle from `identity` module. Required unless only `enable_static_website` is set and no `queues` or `file_shares` are requested | conditional |
| `resource_group_name`            | Resource group for the storage account                 | yes      |
| `resource_group_location`        | Azure region                                           | yes      |
| `containers`                     | List of blob container names to create                 | no       |
| `connection_string_name`         | App settings key suffix (default: `storage`)           | no       |
| `queues`                         | List of storage queue names to create                  | no       |
| `queue_connection_string_name`   | App settings key suffix (default: `storagequeue`)      | no       |
| `file_shares`                    | List of `{ name, quota_gb }` file shares to create      | no       |
| `file_connection_string_name`    | App settings key suffix (default: `storagefile`)       | no       |
| `account_replication_type`       | Replication type (default: `LRS`)                      | no       |

## Outputs

| Name                     | Description                                                       |
| ------------------------ | ------------------------------------------------------------------- |
| `account_name`           | Storage account name                                                 |
| `account_id`             | Storage account resource ID                                          |
| `primary_blob_endpoint`  | Primary blob endpoint URL                                            |
| `container_names`        | Names of created containers                                          |
| `queue_names`            | Names of created storage queues                                      |
| `primary_queue_endpoint` | Primary queue endpoint URL                                           |
| `file_share_names`       | Names of created file shares                                         |
| `primary_file_endpoint`  | Primary file endpoint URL                                            |
| `app_settings`           | `{ "ConnectionStrings__{name}" = endpoint }` per enabled service      |

## What it creates

- Storage account (Standard LRS, shared key disabled, OAuth default)
- Blob containers (private access)
- Storage queues and file shares (only when requested — each is independently opt-in)
- Private endpoints (services subnet): one per service that has resources requested (blob unless explicitly disabled via `enable_blob = false`; queue/file only when non-empty)
- RBAC, granted only for services that are used:
  - Storage Blob Data Contributor → App Registration SP, `deployer_principal_ids`, and the identity running Terraform (`current_client`) (blob)
  - Storage Queue Data Contributor → App Registration SP (queue)
  - Storage File Data Privileged Contributor → App Registration SP (file — the only built-in file-share role that authorizes OAuth/AAD REST calls; the SMB Share roles only cover SMB-mounted access)

## Known Limitations

- **Storage tables are not supported by this module.** `azurerm_storage_table` always
  authenticates with a shared account key regardless of resource addressing or RBAC on the
  `azurerm` provider versions this module targets (`>= 4.46, < 5.0`; see `providers.tf`), but this
  module disables shared keys (`shared_access_key_enabled = false`) on every account it creates, unless
  `storage_secret_keyvault_name` is set to publish key1 to Key Vault.
  Table support was removed rather than kept dormant behind a flag; it can be reintroduced once
  the module is upgraded to `azurerm >= 5.0` and the resource can move onto the same Resource
  Manager addressing already used for queues and shares.

## RBAC notes

The App Registration service principal receives the Contributor role for each data-plane service it uses (Blob, Queue, File) to enable data-plane operations (no connection strings, no SAS tokens, no shared keys). At runtime the platform pins `DefaultAzureCredential` to `EnvironmentCredential` (`AZURE_TOKEN_CREDENTIALS=environmentcredential`, `AZURE_CLIENT_ID` = app registration), so the SP — not the UAMI — is the identity making data-plane calls. The UAMI covers platform concerns only (ACR pull, Key Vault reference resolution). This matches the `cosmosdb` module, which grants its data-plane role to the SP.

Contributor rather than Owner follows least privilege and matches Aspire's default role assignment for `AddAzureStorage` (`StorageBlobDataContributor`); Owner only adds ADLS Gen2 POSIX ACL operations, which Forge apps don't use. Queue Contributor is the standard Azure data-plane role for that service. File Data Privileged Contributor is required (rather than the SMB Share roles) because forge apps access file shares over REST via the Azure SDK, not by mounting the share over SMB.

## Storage queues vs. Service Bus queues

Storage queues are a low-cost, high-throughput option for app-internal messaging without the
operational overhead of a Service Bus namespace, at the cost of no native ordering or
dead-lettering. See the per-service guides for provisioning steps and SDK usage:
[Queue Storage](https://github.com/saif-corp/forge/blob/main/docs/build/eventing/storage-queue.md)
and [File Share Storage](https://github.com/saif-corp/forge/blob/main/docs/build/data/storage-file-share.md).

<!-- BEGIN_TF_DOCS -->
## Providers

| Name | Version |
| ---- | ------- |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | >= 4.46, < 5.0 |
| <a name="provider_azurerm.shared-services"></a> [azurerm.shared-services](#provider\_azurerm.shared-services) | >= 4.46, < 5.0 |

## Inputs

| Name | Description | Type | Default | Required |
| ---- | ----------- | ---- | ------- | :------: |
| <a name="input_account_replication_type"></a> [account\_replication\_type](#input\_account\_replication\_type) | Storage account replication type (LRS, ZRS, GRS, etc.) | `string` | `"LRS"` | no |
| <a name="input_connection_string_name"></a> [connection\_string\_name](#input\_connection\_string\_name) | Key suffix for the app\_settings connection string: ConnectionStrings\_\_{name} | `string` | `"storage"` | no |
| <a name="input_containers"></a> [containers](#input\_containers) | List of blob container names to create | `list(string)` | `[]` | no |
| <a name="input_context"></a> [context](#input\_context) | Platform context from the environment module | `any` | n/a | yes |
| <a name="input_deployer_principal_ids"></a> [deployer\_principal\_ids](#input\_deployer\_principal\_ids) | Map of identity keys to principal IDs granted Blob Data Contributor for CI/CD deployments. The key current\_client is reserved for the identity running Terraform and cannot be used. | `map(string)` | `{}` | no |
| <a name="input_enable_blob"></a> [enable\_blob](#input\_enable\_blob) | Explicitly enable/disable blob provisioning (RBAC, private endpoint, app setting). Defaults to true when null; set to false to opt out for queue/file-only accounts. | `bool` | `null` | no |
| <a name="input_enable_static_website"></a> [enable\_static\_website](#input\_enable\_static\_website) | Enable static website hosting on the storage account | `bool` | `false` | no |
| <a name="input_file_connection_string_name"></a> [file\_connection\_string\_name](#input\_file\_connection\_string\_name) | Key suffix for the app\_settings file endpoint: ConnectionStrings\_\_{name} | `string` | `"storagefile"` | no |
| <a name="input_file_deployer_principal_ids"></a> [file\_deployer\_principal\_ids](#input\_file\_deployer\_principal\_ids) | Map of identity keys to principal IDs granted Storage File Data Privileged Contributor for CI/CD deployments | `map(string)` | `{}` | no |
| <a name="input_file_shares"></a> [file\_shares](#input\_file\_shares) | File shares to create. quota\_gb defaults to 100 GiB when unspecified. | <pre>list(object({<br/>    name     = string<br/>    quota_gb = optional(number, 100)<br/>  }))</pre> | `[]` | no |
| <a name="input_identity"></a> [identity](#input\_identity) | Identity bundle from the identity module. Required when enable\_static\_website is false (blob access via the App Registration SP), or whenever queues or file\_shares are non-empty (queue/file data-plane RBAC is also SP-backed). | `any` | `null` | no |
| <a name="input_name"></a> [name](#input\_name) | Override storage account name (bypasses namer). Use for migrations where the existing name differs from the namer convention. | `string` | `null` | no |
| <a name="input_network_rules_enabled"></a> [network\_rules\_enabled](#input\_network\_rules\_enabled) | Enable storage account network rules (deny by default) | `bool` | `true` | no |
| <a name="input_network_rules_ip_rules"></a> [network\_rules\_ip\_rules](#input\_network\_rules\_ip\_rules) | Additional public IP ranges to allow through the storage firewall. SAIF corporate/colocation ranges are always included. | `list(string)` | `[]` | no |
| <a name="input_private_endpoint_enabled"></a> [private\_endpoint\_enabled](#input\_private\_endpoint\_enabled) | Create private endpoints for blob access and for any requested queue/file services. Disable for static websites where FD Private Link provides connectivity. | `bool` | `true` | no |
| <a name="input_queue_connection_string_name"></a> [queue\_connection\_string\_name](#input\_queue\_connection\_string\_name) | Key suffix for the app\_settings queue endpoint: ConnectionStrings\_\_{name} | `string` | `"storagequeue"` | no |
| <a name="input_queue_deployer_principal_ids"></a> [queue\_deployer\_principal\_ids](#input\_queue\_deployer\_principal\_ids) | Map of identity keys to principal IDs granted Storage Queue Data Contributor for CI/CD deployments | `map(string)` | `{}` | no |
| <a name="input_queues"></a> [queues](#input\_queues) | List of storage queue names to create for app-internal producer/consumer messaging | `list(string)` | `[]` | no |
| <a name="input_resource_group_location"></a> [resource\_group\_location](#input\_resource\_group\_location) | The Azure region for the storage account | `string` | n/a | yes |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The resource group for the storage account | `string` | n/a | yes |
| <a name="input_storage_secret_keyvault_name"></a> [storage\_secret\_keyvault\_name](#input\_storage\_secret\_keyvault\_name) | Name of an existing Azure RBAC Key Vault in the shared services subscription. When set, shared key access is enabled on the account and key1 is stored there as a secret. Empty (default) keeps data-plane access OAuth-only. Requires the azurerm.shared-services provider, and the principal running Terraform must already hold Key Vault Secrets Officer on the vault. | `string` | `""` | no |

## Outputs

| Name | Description |
| ---- | ----------- |
| <a name="output_account_id"></a> [account\_id](#output\_account\_id) | The storage account resource ID |
| <a name="output_account_name"></a> [account\_name](#output\_account\_name) | The storage account name |
| <a name="output_app_settings"></a> [app\_settings](#output\_app\_settings) | App settings map for web app configuration |
| <a name="output_container_names"></a> [container\_names](#output\_container\_names) | Names of the created containers |
| <a name="output_file_share_names"></a> [file\_share\_names](#output\_file\_share\_names) | Names of the created file shares |
| <a name="output_primary_blob_endpoint"></a> [primary\_blob\_endpoint](#output\_primary\_blob\_endpoint) | The primary blob endpoint URL |
| <a name="output_primary_file_endpoint"></a> [primary\_file\_endpoint](#output\_primary\_file\_endpoint) | The primary file endpoint URL |
| <a name="output_primary_queue_endpoint"></a> [primary\_queue\_endpoint](#output\_primary\_queue\_endpoint) | The primary queue endpoint URL |
| <a name="output_queue_names"></a> [queue\_names](#output\_queue\_names) | Names of the created storage queues |
| <a name="output_static_website_host"></a> [static\_website\_host](#output\_static\_website\_host) | The primary web host for the static website (null when static website is disabled) |

## Resources


- resource.azurerm_key_vault_secret.storage_account_key (/terraform-docs/modules/storage/storage_key_secret.tf#40)
- resource.azurerm_role_assignment.file_data_contributor (/terraform-docs/modules/storage/main.tf#156)
- resource.azurerm_storage_queue.this (/terraform-docs/modules/storage/main.tf#120)
- resource.azurerm_storage_share.this (/terraform-docs/modules/storage/main.tf#136)
- data source.azurerm_client_config.current (/terraform-docs/modules/storage/main.tf#22)
- data source.azurerm_key_vault.storage_keys (/terraform-docs/modules/storage/storage_key_secret.tf#23)    
<!-- END_TF_DOCS -->

---

[View source on GitHub](https://github.com/saif-corp/forge/blob/main/src/terraform/saif-resources/modules/storage/README.md)
