How to Create AWS Backup Configurations for RDS and S3 Using Terraform


Managing backups in AWS is essential to ensure the safety and availability of your data. By using Terraform, you can automate the creation and management of AWS Backup configurations for both Amazon RDS and S3, ensuring consistent, reliable backups across your AWS infrastructure.

Step 1: Create an S3 Bucket for Backups

First, you’ll need to create an S3 bucket to store your backups. The following Terraform code snippet sets up an S3 bucket with versioning and lifecycle rules to transition older backups to Glacier storage and eventually delete them after a specified period.

resource "aws_s3_bucket" "backup_bucket" {
bucket = "my-backup-bucket"

versioning {
enabled = true
}

server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}

lifecycle_rule {
enabled = true

transition {
days = 30
storage_class = "GLACIER"
}

expiration {
days = 365
}
}
}

Step 2: Create an RDS Instance

Next, you can create an Amazon RDS instance. The example below creates an RDS instance with a daily automated backup schedule, retaining each backup for seven days.

resource "aws_db_instance" "example" {
allocated_storage = 20
engine = "mysql"
engine_version = "8.0"
instance_class = "db.t3.micro"
name = "mydatabase"
username = "foo"
password = "barbaz"
parameter_group_name = "default.mysql8.0"
skip_final_snapshot = true

backup_retention_period = 7
backup_window = "03:00-06:00"

tags = {
Name = "my-rds-instance"
Backup = "true"
}
}

Step 3: Set Up AWS Backup Plan

With AWS Backup, you can define a centralized backup plan. This plan will dictate how often backups are taken and how long they are retained. Here’s an example of a daily backup plan:

resource "aws_backup_plan" "example" {
name = "example-backup-plan"

rule {
rule_name = "daily-backup"
target_vault_name = aws_backup_vault.example.name
schedule = "cron(0 12 * * ? *)" # Every day at 12:00 UTC

lifecycle {
cold_storage_after = 30
delete_after = 365
}

recovery_point_tags = {
"Environment" = "Production"
}
}
}

Step 4: Assign Resources to the Backup Plan

Now, assign the RDS instance and S3 bucket to the backup plan so they are included in the automated backup schedule:

resource "aws_backup_selection" "rds_selection" {
name = "rds-backup-selection"
iam_role_arn = aws_iam_role.backup_role.arn
backup_plan_id = aws_backup_plan.example.id

resources = [
aws_db_instance.example.arn,
]
}

resource "aws_backup_selection" "s3_selection" {
name = "s3-backup-selection"
iam_role_arn = aws_iam_role.backup_role.arn
backup_plan_id = aws_backup_plan.example.id

resources = [
aws_s3_bucket.backup_bucket.arn,
]
}

Step 5: Create an IAM Role for AWS Backup

AWS Backup needs the appropriate permissions to manage the backup process. This requires creating an IAM role with the necessary policies:

resource "aws_iam_role" "backup_role" {
name = "aws_backup_role"

assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [{
"Action" : "sts:AssumeRole",
"Principal" : {
"Service" : "backup.amazonaws.com"
},
"Effect" : "Allow",
"Sid" : ""
}]
})
}

resource "aws_iam_role_policy_attachment" "backup_role_policy" {
role = aws_iam_role.backup_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
}

Conclusion

By using Terraform to automate AWS Backup configurations for RDS and S3, you can ensure that your critical data is backed up regularly and securely. This approach not only simplifies backup management but also makes it easier to scale and replicate your backup strategy across multiple AWS accounts and regions. With this Terraform setup, you have a robust solution for automating and managing backups, giving you peace of mind that your data is safe.

Monitoring backups is crucial to ensure that your backup processes are running smoothly, that your data is being backed up correctly, and that you can quickly address any issues that arise. AWS provides several tools and services to help you monitor your backups effectively. Here’s how you can monitor backups in AWS:

1. AWS Backup Monitoring

a. AWS Backup Dashboard

  • The AWS Backup console provides a dashboard that gives you an overview of your backup activity.
  • You can see the status of recent backup jobs, including whether they succeeded, failed, or are currently in progress.
  • The dashboard also shows a summary of protected resources and the number of recovery points created.

b. Backup Jobs

  • In the AWS Backup console, navigate to Backup jobs.
  • This section lists all backup jobs with detailed information such as:
    • Job status (e.g., COMPLETED, FAILED, IN_PROGRESS).
    • Resource type (e.g., EC2, RDS, S3).
    • Start and end times.
    • Recovery point ID.
  • You can filter backup jobs by status, resource type, and time range to focus on specific jobs.

c. Protected Resources

  • The Protected resources section shows which AWS resources are currently being backed up by AWS Backup.
  • You can view the backup plan associated with each resource and the last backup status.

d. Recovery Points

  • In the Recovery points section, you can monitor the number of recovery points created for each resource.
  • This helps ensure that backups are being created according to the defined backup plan.

2. CloudWatch Alarms for Backup Monitoring

AWS CloudWatch can be used to create alarms based on metrics that AWS Backup publishes, allowing you to receive notifications when something goes wrong.

a. Backup Metrics

  • AWS Backup publishes metrics to CloudWatch, such as:
    • BackupJobSuccess: The number of successful backup jobs.
    • BackupJobFailure: The number of failed backup jobs.
    • RestoreJobSuccess: The number of successful restore jobs.
    • RestoreJobFailure: The number of failed restore jobs.

b. Create a CloudWatch Alarm

  • Go to the CloudWatch console and navigate to Alarms.
  • Create an alarm based on the AWS Backup metrics. For example, you can create an alarm that triggers if there are any BackupJobFailure events in the last hour.
  • Configure the alarm to send notifications via Amazon SNS (Simple Notification Service) to email, SMS, or other endpoints.

3. Automated Notifications and Reporting

a. SNS Notifications

  • AWS Backup can be configured to send notifications about backup job statuses via Amazon SNS.
  • Create an SNS topic, and subscribe your email or other communication tools (e.g., Slack, SMS) to this topic.
  • In the AWS Backup settings, link your SNS topic to receive notifications about backup jobs.

b. Backup Reports

  • AWS Backup allows you to generate reports on your backup activities.
  • Use the AWS Backup Audit Manager to generate and automate reports that provide detailed insights into the backup activities across your resources.
  • Reports can include information on compliance with your backup policies, success/failure rates, and other important metrics.

4. AWS Config for Backup Compliance

AWS Config allows you to monitor the compliance of your AWS resources against defined rules, including backup-related rules.

a. Create Config Rules

  • You can create AWS Config rules that automatically check whether your resources are backed up according to your organization’s policies.
  • Example rules:
    • rds-instance-backup-enabled: Ensures that RDS instances have backups enabled.
    • ec2-instance-backup-enabled: Ensures that EC2 instances are being backed up.
    • s3-bucket-backup-enabled: Ensures that S3 buckets have backup configurations in place.

b. Monitor Compliance

  • AWS Config provides a dashboard where you can monitor the compliance status of your resources.
  • Non-compliant resources can be investigated to ensure that backups are configured correctly.

5. Custom Monitoring with Lambda

For advanced scenarios, you can use AWS Lambda to automate and customize your monitoring. For example, you can write a Lambda function that:

  • Checks the status of recent backup jobs.
  • Sends a detailed report via email or logs the results in a specific format.
  • Integrates with third-party monitoring tools for centralized monitoring.

6. Third-Party Monitoring Tools

If you use third-party monitoring or logging tools (e.g., Datadog, Splunk), you can integrate AWS Backup logs and metrics into those platforms. This allows you to monitor backups alongside other infrastructure components, providing a unified monitoring solution.

Summary

Monitoring your AWS backups is essential for ensuring that your data protection strategy is effective. AWS provides a range of tools, including AWS Backup, CloudWatch, SNS, and AWS Config, to help you monitor, receive alerts, and ensure compliance with your backup policies. By setting up proper monitoring and notifications, you can quickly detect and respond to any issues, ensuring that your backups are reliable and your data is secure.

The cost of performing restore tests in AWS primarily depends on the following factors:

1. Data Retrieval Costs

  • Warm Storage: If your backups are in warm storage (the default in AWS Backup), there are no additional costs for data retrieval.
  • Cold Storage: If your backups are in cold storage (e.g., Amazon S3 Glacier or S3 Glacier Deep Archive), you will incur data retrieval costs. The cost varies depending on the retrieval speed:
    • Expedited retrieval: Typically costs around $0.03 per GB.
    • Standard retrieval: Usually costs around $0.01 per GB.
    • Bulk retrieval: Usually the cheapest, around $0.0025 per GB.

2. Compute Resources (for RDS and EC2 Restores)

  • RDS Instances: When you restore an RDS instance, you are essentially launching a new database instance, which incurs standard RDS pricing based on the instance type, storage type, and any additional features (e.g., Multi-AZ, read replicas).
    • Example: A small db.t3.micro RDS instance could cost around $0.015 per hour, while larger instances cost significantly more.
  • EC2 Instances: If you restore an EC2 instance, you will incur standard EC2 instance costs based on the instance type and the duration the instance runs during the test.

3. S3 Storage Costs

  • Restored Data Storage: If you restore data to an S3 bucket, you will pay for the storage costs of that data in the bucket.
    • The standard S3 storage cost is around $0.023 per GB per month for S3 Standard storage.
  • Data Transfer Costs: If you transfer data out of S3 (e.g., to another region or outside AWS), you will incur data transfer costs. Within the same region, data transfer is typically free.

4. Network Data Transfer Costs

  • If your restore involves transferring data across regions or to/from the internet, there are additional data transfer charges. These costs can add up depending on the amount of data being transferred.

5. EBS Storage Costs (for EC2 Restores)

  • If the restored EC2 instance uses Amazon EBS volumes, you’ll incur standard EBS storage costs, which depend on the volume type and size.
  • Example: General Purpose SSD (gp2) storage costs about $0.10 per GB per month.

6. Duration of Testing

  • The longer you keep the restored resources running (e.g., RDS or EC2 instances), the higher the costs.
  • Consider running your tests efficiently by restoring, validating, and terminating the resources promptly to minimize costs.

7. Additional Costs

  • IAM Role Costs: While there is no direct cost for IAM roles used in the restore process, you might incur costs if using AWS KMS (Key Management Service) for encryption keys, especially if these keys are used during the restore process.
  • AWS Config Costs: If you use AWS Config to monitor and manage your restore tests, there may be additional costs associated with the number of resources being tracked.

Example Cost Breakdown

Let’s assume you restore a 100 GB database from cold storage (S3 Glacier) to an RDS db.t3.micro instance and run it for 1 hour:

  • Data Retrieval (Cold Storage): 100 GB x $0.01/GB (Standard retrieval) = $1.00
  • RDS Instance (db.t3.micro): $0.015 per hour = $0.015
  • S3 Storage for Restored Data: 100 GB x $0.023/GB per month = $2.30 per month (if data is retained in S3)
  • EBS Storage for EC2 Restore: If relevant, say 100 GB x $0.10/GB per month = $10.00 per month (pro-rated for time used).

Total Cost Estimate:

For the above scenario, the one-time restore test cost would be approximately $1.015 for immediate data retrieval and the RDS instance run-time. Storage costs will accumulate if the restored data is kept in S3 or EBS for longer durations.