Skip to content

Commit f2afcfd

Browse files
authored
Merge pull request #105 from lgallard/fix/windows_vss_backup
fix: Enhance Windows VSS backup validation and add example configuration
2 parents 7f69cb5 + 5ff6228 commit f2afcfd

File tree

7 files changed

+155
-2
lines changed

7 files changed

+155
-2
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Windows VSS Backup Example
2+
3+
This example demonstrates the Windows VSS (Volume Shadow Copy Service) backup functionality of the AWS Backup module. Windows VSS is a feature that allows for consistent backups of Windows EC2 instances.
4+
5+
## Usage
6+
7+
To run this example, execute:
8+
9+
```bash
10+
terraform init
11+
terraform plan
12+
terraform apply
13+
```
14+
15+
## Testing the Validation Logic
16+
17+
This example includes proper configuration with EC2 instances in the selection, which is required when Windows VSS backup is enabled.
18+
19+
### Testing the Error Case
20+
21+
To test the error case (when Windows VSS is enabled but no EC2 instances are selected), modify the `main.tf` file:
22+
23+
1. Comment out the current `selection_resources` block
24+
2. Uncomment the error test case below
25+
3. Run `terraform plan`
26+
27+
```terraform
28+
# Comment out the current selection_resources
29+
/*
30+
selection_resources = [
31+
"arn:aws:ec2:us-west-2:123456789012:instance/i-1234567890abcdef0",
32+
"arn:aws:dynamodb:us-west-2:123456789012:table/my-table"
33+
]
34+
*/
35+
36+
# Uncomment this to test the error case
37+
selection_resources = [
38+
# No EC2 instances here - will trigger the validation error
39+
"arn:aws:dynamodb:us-west-2:123456789012:table/my-table"
40+
]
41+
```
42+
43+
You should see an error message:
44+
45+
```
46+
Error: Resource precondition failed
47+
48+
on .terraform/modules/backup/main.tf line XX, in resource "aws_backup_plan" "ab_plan":
49+
XX: condition = !var.windows_vss_backup || (length(local.selection_resources) > 0 && can(regex(".*EC2.*", join(",", local.selection_resources))))
50+
├────────────────
51+
│ local.selection_resources doesn't contain EC2 instances
52+
│ var.windows_vss_backup is true
53+
54+
Windows VSS backup is enabled but no EC2 instances are selected for backup. Either disable windows_vss_backup or include EC2 instances in your backup selection.
55+
```
56+
57+
## Requirements
58+
59+
| Name | Version |
60+
|------|---------|
61+
| terraform | >= 1.0 |
62+
| aws | >= 4.0 |
63+
64+
## Notes
65+
66+
- Windows VSS backup is only applicable to Windows EC2 instances
67+
- When enabled, at least one EC2 instance must be included in the backup selection
68+
- This can be done via direct ARN references or tag-based selection
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Windows VSS Backup Example
2+
# This example demonstrates the Windows VSS backup functionality
3+
# and validation requirements for EC2 instances
4+
5+
module "aws_backup_windows_vss" {
6+
source = "../.."
7+
8+
# Basic configuration
9+
vault_name = "windows_vss_backup_vault"
10+
plan_name = "windows_vss_backup_plan"
11+
12+
# Enable Windows VSS backup (Windows Volume Shadow Copy Service)
13+
windows_vss_backup = true
14+
15+
# Add a simple rule
16+
rule_name = "daily_backup"
17+
rule_schedule = "cron(0 12 * * ? *)"
18+
rule_start_window = 60
19+
20+
# OPTION 1: Working configuration with EC2 instances
21+
# Windows VSS backup requires at least one EC2 instance in the selection
22+
selection_name = "windows_ec2_selection"
23+
selection_resources = [
24+
# Include an EC2 instance to satisfy the validation
25+
"arn:aws:ec2:us-west-2:123456789012:instance/i-1234567890abcdef0",
26+
27+
# You can also include other resources
28+
"arn:aws:dynamodb:us-west-2:123456789012:table/my-table"
29+
]
30+
31+
# OPTION 2: Error case - uncomment to test validation error
32+
# Comment out the above selection_resources and uncomment these lines
33+
/*
34+
selection_name = "windows_ec2_selection"
35+
selection_resources = [
36+
# No EC2 instances here - will trigger the validation error
37+
"arn:aws:dynamodb:us-west-2:123456789012:table/my-table"
38+
]
39+
*/
40+
41+
# Additional example with tag-based selection
42+
# This will select all EC2 instances with the specified tag
43+
selections = [
44+
{
45+
name = "tag_based_selection"
46+
selection_tag = {
47+
type = "STRINGEQUALS"
48+
key = "Backup"
49+
value = "windows-vss"
50+
}
51+
}
52+
]
53+
54+
tags = {
55+
Environment = "test"
56+
Purpose = "Windows VSS Backup Example"
57+
}
58+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "aws" {
2+
region = var.env["region"]
3+
profile = var.env["profile"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
env = {
2+
region = "us-east-1"
3+
profile = "default"
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "env" {
2+
description = "Environment configuration map. Used to define environment-specific parameters like tags, resource names, and other settings"
3+
type = map(any)
4+
default = {
5+
Environment = "prod"
6+
Owner = "devops"
7+
Terraform = true
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.0"
8+
}
9+
}
10+
}

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ resource "aws_backup_plan" "ab_plan" {
8989

9090
lifecycle {
9191
precondition {
92-
condition = !var.windows_vss_backup || can(regex(".*EC2.*", join(",", local.selection_resources)))
93-
error_message = "Windows VSS backup is enabled but no EC2 instances are selected for backup."
92+
condition = !var.windows_vss_backup || (length(local.selection_resources) > 0 && can(regex(".*EC2.*", join(",", local.selection_resources))))
93+
error_message = "Windows VSS backup is enabled but no EC2 instances are selected for backup. Either disable windows_vss_backup or include EC2 instances in your backup selection."
9494
}
9595

9696
# Add lifecycle validations at the plan level

0 commit comments

Comments
 (0)