diff --git a/README.md b/README.md index 1ce836b..095c80d 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,4 @@ module "sql" { | database_id | Database resource ID | | database_name | Database name | | connection_string | ADO.NET connection string (sensitive) | +Added LTR submodule scaffolding (modules/long_term_retention) with default 13-month retention. diff --git a/main.tf b/main.tf index 30bba00..aacecbf 100644 --- a/main.tf +++ b/main.tf @@ -10,12 +10,32 @@ resource "azurerm_mssql_server" "this" { tags = var.tags } +module "long_term_retention" { + source = "./modules/long_term_retention" + + enabled = var.long_term_retention_enabled + weekly_retention = var.long_term_retention_weekly + monthly_retention = var.long_term_retention_monthly + yearly_retention = var.long_term_retention_yearly + week_of_year = var.long_term_retention_week_of_year +} + resource "azurerm_mssql_database" "this" { name = var.database_name server_id = azurerm_mssql_server.this.id sku_name = var.sku_name max_size_gb = var.max_size_gb + dynamic "long_term_retention_policy" { + for_each = module.long_term_retention.enabled ? [module.long_term_retention.policy] : [] + content { + weekly_retention = long_term_retention_policy.value.weekly_retention + monthly_retention = long_term_retention_policy.value.monthly_retention + yearly_retention = long_term_retention_policy.value.yearly_retention + week_of_year = long_term_retention_policy.value.week_of_year + } + } + tags = var.tags } diff --git a/modules/long_term_retention/README.md b/modules/long_term_retention/README.md new file mode 100644 index 0000000..35f3151 --- /dev/null +++ b/modules/long_term_retention/README.md @@ -0,0 +1,57 @@ +# long_term_retention + +Centralises Long-Term Retention (LTR) policy settings for an Azure SQL +Database, defaulting to **13 months** of monthly retention (`P13M`). + +With the azurerm provider build in use here, LTR is configured via the +nested `long_term_retention_policy` block on the `azurerm_mssql_database` +resource (there is no standalone LTR resource type). This module therefore +exposes the policy values via outputs so they can be wired into the +database resource. + +## Usage + +```hcl +module "long_term_retention" { + source = "./modules/long_term_retention" + + # Defaults to 13 months — override as needed. + monthly_retention = "P13M" +} + +resource "azurerm_mssql_database" "this" { + # ... other arguments ... + + dynamic "long_term_retention_policy" { + for_each = module.long_term_retention.enabled ? [module.long_term_retention.policy] : [] + content { + weekly_retention = long_term_retention_policy.value.weekly_retention + monthly_retention = long_term_retention_policy.value.monthly_retention + yearly_retention = long_term_retention_policy.value.yearly_retention + week_of_year = long_term_retention_policy.value.week_of_year + } + } +} +``` + +## Inputs + +| Name | Description | Type | Default | +|------|-------------|------|---------| +| enabled | Whether to wire the LTR policy. | bool | true | +| database_id | Optional, informational. | string | null | +| weekly_retention | ISO 8601 weekly retention. | string | null | +| monthly_retention | ISO 8601 monthly retention. | string | "P13M" | +| yearly_retention | ISO 8601 yearly retention. | string | null | +| week_of_year | Week (1-52) for yearly backup. | number | null | + +## Outputs + +| Name | Description | +|------|-------------| +| enabled | Whether the policy is enabled. | +| policy | Object with all LTR block values, ready for the nested block. | +| weekly_retention | Pass-through. | +| monthly_retention | Pass-through. | +| yearly_retention | Pass-through. | +| week_of_year | Pass-through. | diff --git a/modules/long_term_retention/main.tf b/modules/long_term_retention/main.tf new file mode 100644 index 0000000..fd6f298 --- /dev/null +++ b/modules/long_term_retention/main.tf @@ -0,0 +1,31 @@ +# Long-Term Retention (LTR) configuration module. +# +# Note: With this azurerm provider build, LTR is configured via the +# nested `long_term_retention_policy` block on the `azurerm_mssql_database` +# resource (no standalone resource type is available). +# +# This module centralises and exposes the LTR settings (defaulting to 13 +# months of monthly retention) so that they can be wired into the database +# resource via the `policy` output, e.g.: +# +# resource "azurerm_mssql_database" "this" { +# ... +# dynamic "long_term_retention_policy" { +# for_each = var.enable_ltr ? [module.long_term_retention.policy] : [] +# content { +# weekly_retention = long_term_retention_policy.value.weekly_retention +# monthly_retention = long_term_retention_policy.value.monthly_retention +# yearly_retention = long_term_retention_policy.value.yearly_retention +# week_of_year = long_term_retention_policy.value.week_of_year +# } +# } +# } + +locals { + policy = { + weekly_retention = var.weekly_retention + monthly_retention = var.monthly_retention + yearly_retention = var.yearly_retention + week_of_year = var.week_of_year + } +} diff --git a/modules/long_term_retention/outputs.tf b/modules/long_term_retention/outputs.tf new file mode 100644 index 0000000..d5d59a9 --- /dev/null +++ b/modules/long_term_retention/outputs.tf @@ -0,0 +1,29 @@ +output "enabled" { + description = "Whether the Long-Term Retention (LTR) policy is enabled." + value = var.enabled +} + +output "policy" { + description = "Long-Term Retention policy values to be applied to the long_term_retention_policy block of azurerm_mssql_database." + value = local.policy +} + +output "weekly_retention" { + description = "Weekly retention (ISO 8601 duration)." + value = var.weekly_retention +} + +output "monthly_retention" { + description = "Monthly retention (ISO 8601 duration). Defaults to P13M (13 months)." + value = var.monthly_retention +} + +output "yearly_retention" { + description = "Yearly retention (ISO 8601 duration)." + value = var.yearly_retention +} + +output "week_of_year" { + description = "Week of year used for yearly backup." + value = var.week_of_year +} diff --git a/modules/long_term_retention/variables.tf b/modules/long_term_retention/variables.tf new file mode 100644 index 0000000..d862d2c --- /dev/null +++ b/modules/long_term_retention/variables.tf @@ -0,0 +1,35 @@ +variable "enabled" { + description = "Whether to enable the Long-Term Retention (LTR) policy. Exposed for consumers that conditionally wire the policy block." + type = bool + default = true +} + +variable "database_id" { + description = "Optional ID of the Azure SQL Database the LTR policy applies to. Informational only — with this azurerm provider build, LTR is set via the nested long_term_retention_policy block on azurerm_mssql_database." + type = string + default = null +} + +variable "weekly_retention" { + description = "Weekly backup retention period (ISO 8601 duration, e.g. P12W). Optional." + type = string + default = null +} + +variable "monthly_retention" { + description = "Monthly backup retention period (ISO 8601 duration, e.g. P13M for 13 months). Defaults to 13 months." + type = string + default = "P13M" +} + +variable "yearly_retention" { + description = "Yearly backup retention period (ISO 8601 duration, e.g. P5Y). Optional." + type = string + default = null +} + +variable "week_of_year" { + description = "Week of year (1-52) for the yearly backup. Required by Azure if yearly_retention is set." + type = number + default = null +} diff --git a/modules/long_term_retention/versions.tf b/modules/long_term_retention/versions.tf new file mode 100644 index 0000000..26d316b --- /dev/null +++ b/modules/long_term_retention/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.3.0" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">= 3.0.0, < 5.0.0" + } + } +} diff --git a/variables.tf b/variables.tf index b1d236c..245f226 100644 --- a/variables.tf +++ b/variables.tf @@ -49,3 +49,33 @@ variable "tags" { type = map(string) default = {} } + +variable "long_term_retention_enabled" { + description = "Whether to enable the Long-Term Retention (LTR) policy on the SQL database." + type = bool + default = true +} + +variable "long_term_retention_weekly" { + description = "Weekly LTR retention (ISO 8601 duration, e.g. P12W). Optional." + type = string + default = null +} + +variable "long_term_retention_monthly" { + description = "Monthly LTR retention (ISO 8601 duration). Defaults to P13M (13 months) per requirement." + type = string + default = "P13M" +} + +variable "long_term_retention_yearly" { + description = "Yearly LTR retention (ISO 8601 duration, e.g. P5Y). Optional." + type = string + default = null +} + +variable "long_term_retention_week_of_year" { + description = "Week of year (1-52) for the yearly LTR backup. Required by Azure if long_term_retention_yearly is set." + type = number + default = null +}