Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
20 changes: 20 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
57 changes: 57 additions & 0 deletions modules/long_term_retention/README.md
Original file line number Diff line number Diff line change
@@ -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. |
31 changes: 31 additions & 0 deletions modules/long_term_retention/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 29 additions & 0 deletions modules/long_term_retention/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions modules/long_term_retention/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions modules/long_term_retention/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.3.0"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.0.0, < 5.0.0"
}
}
}
30 changes: 30 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}