Skip to content

Commit a97e915

Browse files
Copilotlgallard
andauthored
feature: Add support for multiple backup plans (#115)
* Initial plan for issue * Implemented support for multiple backup plans Co-authored-by: lgallard <6194359+lgallard@users.noreply.github.com> * Add terraform moved blocks and comprehensive migration guide Co-authored-by: lgallard <6194359+lgallard@users.noreply.github.com> * Remove moved blocks causing migration conflicts Co-authored-by: lgallard <6194359+lgallard@users.noreply.github.com> * Fix null value handling in dynamic blocks for conditions and selection_tags Co-authored-by: lgallard <6194359+lgallard@users.noreply.github.com> * Fix null value handling in conditions map values for dynamic blocks Co-authored-by: lgallard <6194359+lgallard@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lgallard <6194359+lgallard@users.noreply.github.com>
1 parent 10500f9 commit a97e915

File tree

12 files changed

+1009
-11
lines changed

12 files changed

+1009
-11
lines changed

README.md

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Terraform module to create AWS Backup plans. AWS Backup is a fully managed backu
1010
* Flexible backup plan customization
1111
* Comprehensive backup management:
1212
- Rules and selections
13+
- Multiple plans per vault
1314
- Copy actions and lifecycle policies
1415
- Retention periods and windows
1516
- Resource tagging
@@ -27,7 +28,7 @@ Terraform module to create AWS Backup plans. AWS Backup is a fully managed backu
2728

2829
## Usage
2930

30-
You can use this module to create a simple plan using the module's `rule_*` variables. You can also use the `rules` and `selections` list of maps variables to build a more complete plan by defining several rules and selections at once.
31+
You can use this module to create a simple plan using the module's `rule_*` variables. You can also use the `rules` and `selections` list of maps variables to build a more complete plan by defining several rules and selections at once. For multiple backup plans, you can use the `plans` variable to create several plans with their own rules and selections.
3132

3233
Check the [examples](/examples/) folder where you can see how to configure backup plans with different selection criteria.
3334

@@ -361,6 +362,214 @@ module "aws_backup_example" {
361362
}
362363
```
363364

365+
### Multiple backup plans
366+
367+
```hcl
368+
module "aws_backup_example" {
369+
source = "lgallard/backup/aws"
370+
371+
# Vault
372+
vault_name = "vault-1"
373+
374+
# Multiple plans
375+
plans = {
376+
# First plan for daily backups
377+
daily = {
378+
name = "daily-backup-plan"
379+
rules = [
380+
{
381+
name = "daily-rule"
382+
schedule = "cron(0 12 * * ? *)"
383+
start_window = 120
384+
completion_window = 360
385+
lifecycle = {
386+
cold_storage_after = 0
387+
delete_after = 30
388+
}
389+
recovery_point_tags = {
390+
Environment = "prod"
391+
Frequency = "daily"
392+
}
393+
}
394+
]
395+
selections = {
396+
prod_databases = {
397+
resources = [
398+
"arn:aws:dynamodb:us-east-1:123456789101:table/mydynamodb-table1"
399+
]
400+
selection_tags = [
401+
{
402+
type = "STRINGEQUALS"
403+
key = "Environment"
404+
value = "prod"
405+
}
406+
]
407+
}
408+
}
409+
},
410+
# Second plan for weekly backups
411+
weekly = {
412+
name = "weekly-backup-plan"
413+
rules = [
414+
{
415+
name = "weekly-rule"
416+
schedule = "cron(0 0 ? * 1 *)" # Run every Sunday at midnight
417+
start_window = 120
418+
completion_window = 480
419+
lifecycle = {
420+
cold_storage_after = 30
421+
delete_after = 120
422+
}
423+
recovery_point_tags = {
424+
Environment = "prod"
425+
Frequency = "weekly"
426+
}
427+
}
428+
]
429+
selections = {
430+
all_databases = {
431+
resources = [
432+
"arn:aws:dynamodb:us-east-1:123456789101:table/mydynamodb-table1",
433+
"arn:aws:dynamodb:us-east-1:123456789101:table/mydynamodb-table2"
434+
]
435+
}
436+
}
437+
}
438+
}
439+
440+
# Tags
441+
tags = {
442+
Owner = "backup team"
443+
Environment = "prod"
444+
Terraform = true
445+
}
446+
}
447+
```
448+
449+
### Migrating from Single Plan to Multiple Plans
450+
451+
When upgrading from a previous version that used single plan configuration to the new multiple plans feature, you have two options:
452+
453+
#### Option 1: Continue using single plan (recommended for simple cases)
454+
455+
The module maintains full backward compatibility. Your existing configuration will continue to work without changes:
456+
457+
```hcl
458+
# This will continue to work as before
459+
module "aws_backup_example" {
460+
source = "lgallard/backup/aws"
461+
462+
vault_name = "my-vault"
463+
plan_name = "my-plan"
464+
465+
# Single rule using variables
466+
rule_name = "daily-rule"
467+
rule_schedule = "cron(0 12 * * ? *)"
468+
469+
# Or multiple rules using list
470+
rules = [
471+
{
472+
name = "rule-1"
473+
schedule = "cron(0 12 * * ? *)"
474+
lifecycle = {
475+
delete_after = 30
476+
}
477+
}
478+
]
479+
480+
# Single selection using variables
481+
selection_name = "my-selection"
482+
selection_resources = ["arn:aws:dynamodb:..."]
483+
484+
# Or multiple selections using list
485+
selections = [
486+
{
487+
name = "selection-1"
488+
resources = ["arn:aws:dynamodb:..."]
489+
}
490+
]
491+
}
492+
```
493+
494+
#### Option 2: Migrate to multiple plans (recommended for complex scenarios)
495+
496+
If you want to use the new multiple plans feature, follow these steps:
497+
498+
1. **Update your configuration** to use the `plans` variable:
499+
500+
```hcl
501+
# Before: Single plan configuration
502+
module "aws_backup_example" {
503+
source = "lgallard/backup/aws"
504+
505+
vault_name = "my-vault"
506+
plan_name = "my-plan"
507+
508+
rules = [
509+
{
510+
name = "daily-rule"
511+
schedule = "cron(0 12 * * ? *)"
512+
lifecycle = { delete_after = 30 }
513+
}
514+
]
515+
516+
selections = [
517+
{
518+
name = "my-selection"
519+
resources = ["arn:aws:dynamodb:..."]
520+
}
521+
]
522+
}
523+
524+
# After: Multiple plans configuration
525+
module "aws_backup_example" {
526+
source = "lgallard/backup/aws"
527+
528+
vault_name = "my-vault"
529+
530+
plans = {
531+
default = { # Use "default" as the plan key for smooth migration
532+
name = "my-plan"
533+
rules = [
534+
{
535+
name = "daily-rule"
536+
schedule = "cron(0 12 * * ? *)"
537+
lifecycle = { delete_after = 30 }
538+
}
539+
]
540+
selections = {
541+
my-selection = {
542+
resources = ["arn:aws:dynamodb:..."]
543+
}
544+
}
545+
}
546+
}
547+
}
548+
```
549+
550+
2. **Handle resource migration** using Terraform state commands:
551+
552+
```bash
553+
# Move the backup plan
554+
terraform state mv 'module.aws_backup_example.aws_backup_plan.ab_plan[0]' 'module.aws_backup_example.aws_backup_plan.ab_plans["default"]'
555+
556+
# Move the backup selection(s) - adjust the selection key as needed
557+
terraform state mv 'module.aws_backup_example.aws_backup_selection.ab_selection[0]' 'module.aws_backup_example.aws_backup_selection.plan_selections["default-my-selection"]'
558+
559+
# If using multiple selections, move each one:
560+
terraform state mv 'module.aws_backup_example.aws_backup_selection.ab_selections["selection-name"]' 'module.aws_backup_example.aws_backup_selection.plan_selections["default-selection-name"]'
561+
```
562+
563+
3. **Run terraform plan** to verify no resources will be recreated:
564+
565+
```bash
566+
terraform plan
567+
# Should show "No changes" if migration was successful
568+
```
569+
570+
> **Note**: The exact state move commands depend on your current configuration. Use `terraform state list` to see your current resource addresses, and `terraform plan` to see what changes would be made before running the state move commands.
571+
572+
364573
### AWS Backup Audit Manager Framework
365574

366575
```hcl

0 commit comments

Comments
 (0)