From b382b210c28776551eeb0681e7734544526bef9e Mon Sep 17 00:00:00 2001 From: Joe Niland Date: Fri, 17 Nov 2023 10:35:46 +1100 Subject: [PATCH] Add restore_to_point_in_time variable (#165) --- README.md | 3 ++- docs/terraform.md | 3 ++- main.tf | 12 ++++++++++++ variables.tf | 14 +++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b7e3593..9195c00 100644 --- a/README.md +++ b/README.md @@ -286,9 +286,10 @@ Available targets: | [publicly\_accessible](#input\_publicly\_accessible) | Determines if database can be publicly available (NOT recommended) | `bool` | `false` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | | [replicate\_source\_db](#input\_replicate\_source\_db) | Specifies that this resource is a Replicate database, and to use this value as the source database. This correlates to the `identifier` of another Amazon RDS Database to replicate (if replicating within a single region) or ARN of the Amazon RDS Database to replicate (if replicating cross-region). Note that if you are creating a cross-region replica of an encrypted database you will also need to specify a `kms_key_id`. See [DB Instance Replication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.Replication.html) and [Working with PostgreSQL and MySQL Read Replicas](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html) for more information on using Replication. | `string` | `null` | no | +| [restore\_to\_point\_in\_time](#input\_restore\_to\_point\_in\_time) | An object specifying the restore point in time for the DB instance to restore from. Only used when `snapshot_identifier` is not provided. |
object({
restore_time = optional(string, null)
source_db_instance_identifier = optional(string, null)
source_db_instance_automated_backups_arn = optional(string, null)
source_dbi_resource_id = optional(string, null)
use_latest_restorable_time = optional(bool, null)
})
| `null` | no | | [security\_group\_ids](#input\_security\_group\_ids) | The IDs of the security groups from which to allow `ingress` traffic to the DB instance | `list(string)` | `[]` | no | | [skip\_final\_snapshot](#input\_skip\_final\_snapshot) | If true (default), no snapshot will be made before deleting DB | `bool` | `true` | no | -| [snapshot\_identifier](#input\_snapshot\_identifier) | Snapshot identifier e.g: rds:production-2019-06-26-06-05. If specified, the module create cluster from the snapshot | `string` | `null` | no | +| [snapshot\_identifier](#input\_snapshot\_identifier) | Snapshot identifier e.g: `rds:production-2019-06-26-06-05` for automated or `manual-backup-2023-11-16` for manual. If specified, the module create the instance from the snapshot. | `string` | `null` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | | [storage\_encrypted](#input\_storage\_encrypted) | (Optional) Specifies whether the DB instance is encrypted. The default is false if not specified | `bool` | `true` | no | | [storage\_throughput](#input\_storage\_throughput) | The storage throughput value for the DB instance. Can only be set when `storage_type` is `gp3`. Cannot be specified if the `allocated_storage` value is below a per-engine threshold. | `number` | `null` | no | diff --git a/docs/terraform.md b/docs/terraform.md index f2845c3..c3ee5a6 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -99,9 +99,10 @@ | [publicly\_accessible](#input\_publicly\_accessible) | Determines if database can be publicly available (NOT recommended) | `bool` | `false` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | | [replicate\_source\_db](#input\_replicate\_source\_db) | Specifies that this resource is a Replicate database, and to use this value as the source database. This correlates to the `identifier` of another Amazon RDS Database to replicate (if replicating within a single region) or ARN of the Amazon RDS Database to replicate (if replicating cross-region). Note that if you are creating a cross-region replica of an encrypted database you will also need to specify a `kms_key_id`. See [DB Instance Replication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.Replication.html) and [Working with PostgreSQL and MySQL Read Replicas](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html) for more information on using Replication. | `string` | `null` | no | +| [restore\_to\_point\_in\_time](#input\_restore\_to\_point\_in\_time) | An object specifying the restore point in time for the DB instance to restore from. Only used when `snapshot_identifier` is not provided. |
object({
restore_time = optional(string, null)
source_db_instance_identifier = optional(string, null)
source_db_instance_automated_backups_arn = optional(string, null)
source_dbi_resource_id = optional(string, null)
use_latest_restorable_time = optional(bool, null)
})
| `null` | no | | [security\_group\_ids](#input\_security\_group\_ids) | The IDs of the security groups from which to allow `ingress` traffic to the DB instance | `list(string)` | `[]` | no | | [skip\_final\_snapshot](#input\_skip\_final\_snapshot) | If true (default), no snapshot will be made before deleting DB | `bool` | `true` | no | -| [snapshot\_identifier](#input\_snapshot\_identifier) | Snapshot identifier e.g: rds:production-2019-06-26-06-05. If specified, the module create cluster from the snapshot | `string` | `null` | no | +| [snapshot\_identifier](#input\_snapshot\_identifier) | Snapshot identifier e.g: `rds:production-2019-06-26-06-05` for automated or `manual-backup-2023-11-16` for manual. If specified, the module create the instance from the snapshot. | `string` | `null` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | | [storage\_encrypted](#input\_storage\_encrypted) | (Optional) Specifies whether the DB instance is encrypted. The default is false if not specified | `bool` | `true` | no | | [storage\_throughput](#input\_storage\_throughput) | The storage throughput value for the DB instance. Can only be set when `storage_type` is `gp3`. Cannot be specified if the `allocated_storage` value is below a per-engine threshold. | `number` | `null` | no | diff --git a/main.tf b/main.tf index 5dcb1b3..d86e1e8 100644 --- a/main.tf +++ b/main.tf @@ -88,6 +88,18 @@ resource "aws_db_instance" "default" { monitoring_interval = var.monitoring_interval monitoring_role_arn = var.monitoring_role_arn + dynamic "restore_to_point_in_time" { + for_each = var.snapshot_identifier == null && var.restore_to_point_in_time != null ? [1] : [] + + content { + restore_time = lookup(var.restore_to_point_in_time, "restore_time", null) + source_db_instance_identifier = lookup(var.restore_to_point_in_time, "source_db_instance_identifier", null) + source_db_instance_automated_backups_arn = lookup(var.restore_to_point_in_time, "source_db_instance_automated_backups_arn", null) + source_dbi_resource_id = lookup(var.restore_to_point_in_time, "source_dbi_resource_id", null) + use_latest_restorable_time = lookup(var.restore_to_point_in_time, "use_latest_restorable_time", null) + } + } + depends_on = [ aws_db_subnet_group.default, aws_security_group.default, diff --git a/variables.tf b/variables.tf index 2cf6cd3..f140612 100644 --- a/variables.tf +++ b/variables.tf @@ -269,7 +269,7 @@ variable "db_options" { variable "snapshot_identifier" { type = string - description = "Snapshot identifier e.g: rds:production-2019-06-26-06-05. If specified, the module create cluster from the snapshot" + description = "Snapshot identifier e.g: `rds:production-2019-06-26-06-05` for automated or `manual-backup-2023-11-16` for manual. If specified, the module create the instance from the snapshot." default = null } @@ -370,3 +370,15 @@ variable "timeouts" { delete = "60m" } } + +variable "restore_to_point_in_time" { + type = object({ + restore_time = optional(string, null) + source_db_instance_identifier = optional(string, null) + source_db_instance_automated_backups_arn = optional(string, null) + source_dbi_resource_id = optional(string, null) + use_latest_restorable_time = optional(bool, null) + }) + description = "An object specifying the restore point in time for the DB instance to restore from. Only used when `snapshot_identifier` is not provided." + default = null +}