Terraform-provider-aws: AWS Backup: Cross Region Copy

Created on 14 Jan 2020  ·  8Comments  ·  Source: hashicorp/terraform-provider-aws

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

https://aws.amazon.com/blogs/aws/aws-backup-ec2-instances-efs-single-file-restore-and-cross-region-backup/

AWS Backup now supports cross-region snapshot copying.

New or Affected Resource(s)

Potential Terraform Configuration

resource "aws_backup_plan" "example" {
  name = "tf_example_backup_plan"

  rule {
    rule_name         = "tf_example_backup_rule"
    target_vault_name = "${aws_backup_vault.test.name}"
    schedule          = "cron(0 12 * * ? *)"
  }

  copy_action {
    lifecycle {
      cold_storage_after = 30
      delete_after       = 120
    }

    destination_vault_arn = "${aws_backup_vault.this.arn}"
  }
}

Example based on https://docs.aws.amazon.com/cli/latest/reference/backup/create-backup-plan.html and https://www.terraform.io/docs/providers/aws/r/backup_plan.html

References

enhancement servicbackup

Most helpful comment

I'm working on this and should have a PR ready soon.

All 8 comments

I'm working on this and should have a PR ready soon.

Still no movement for the PR.
Doing cross region backup is a huge deal, we need higher priority on this one...

Doing both, cross region copy and sns is very important!
I will have to go on a cf stack to get this going, while we wait.

Support for this functionality has been merged and will release with version 2.58.0 of the Terraform AWS Provider later this week. Thanks to @slapula for the implementation. 👍

This has been released in version 2.58.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

Hello, since Ineeded this pretty bas i came out with this module...

// We need an extra aws provider for the replica vault
provider "aws" {
  region = var.replication_zone
  alias  = "replica"
}

// and yes, we need this local provider for the aws cli
provider "local" {}

// we will need this to get the account id
data "aws_caller_identity" "current" {}

# IAM Role & Policy Attachment
module "backup_role" {
  source      = "../../../security_identity_compliance/iam/iam_role"
  name        = "awsbackup-role-for-${var.name}"
  type        = "backup"
  environment = var.environment
}

module "backup_role_policy_attachment" {
  source      = "../../../security_identity_compliance/iam/iam_policy_attachment"
  name        = "awsbackup-policy-attachment-for-${var.name}"
  policy_arn  = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
  roles       = [module.backup_role.iam_role_name]
  environment = var.environment
}


# Backup plan
resource "aws_backup_plan" "awsbackup_plan" {
  name = "${var.plan_name}-${var.environment}"

  # Daily, 35 days
  rule {
    rule_name         = "${var.rule_name}-${var.environment}"
    target_vault_name = var.vault_main_name
    schedule          = var.schedule

    lifecycle {
      delete_after       = var.delete_after
      cold_storage_after = var.cold_storage
    }
  }
}

# Protected Resources
resource "aws_backup_selection" "plan_selection" {
  iam_role_arn = module.backup_role.arn
  name         = "${var.plan_name}-resource-selection-${var.environment}"
  plan_id      = aws_backup_plan.awsbackup_plan.id

  selection_tag {
    key   = var.resource_tag_key
    type  = "STRINGEQUALS"
    value = var.resource_tag_value
  }
}

resource "random_string" "random_copy_filename" {
  length  = 20
  special = false
  number  = false
  lower   = true
  upper   = false
}

resource "local_file" "copy_vault_input_file" {
  provider = local
  content  = <<EOF
{
    "BackupPlanId": "${aws_backup_plan.awsbackup_plan.id}",
    "BackupPlan": {
      "BackupPlanName": "${var.plan_name}-${var.environment}",
      "Rules": [
        {
          "RuleName": "${var.rule_name}-${var.environment}",
          "TargetBackupVaultName": "${var.vault_main_name}",
          "ScheduleExpression": "${var.schedule}",
          "Lifecycle": {
            %{if var.cold_storage != null} "MoveToColdStorageAfterDays": ${var.cold_storage}, %{endif}
            "DeleteAfterDays": ${var.delete_after}
          },
          "CopyActions": [
            {
              "Lifecycle": {
                %{if var.cold_storage != null} "MoveToColdStorageAfterDays": ${var.cold_storage}, %{endif}
                "DeleteAfterDays":  ${var.delete_after}
              },
              "DestinationBackupVaultArn": "${var.vault_replica_arn}"
            }
          ]
        }
      ]
    }
  }
EOF
  filename = "${random_string.random_copy_filename.result}.json"
}

resource "null_resource" "add_copy_vault" {
  provisioner "local-exec" {
    command = "aws backup update-backup-plan --cli-input-json file://$JSON_FILE"

    environment = {
      JSON_FILE = local_file.copy_vault_input_file.filename
    }
  }

}

resource "aws_sns_topic" "sns_backup_topic" {
  name         = "${var.name}-sns-main-${var.environment}"
  display_name = "${var.name}-sns-main-${var.environment}"

}

resource "aws_sns_topic_subscription" "sms_subscriptions" {
  count     = length(var.phones_to_sms)
  endpoint  = var.phones_to_sms[count.index]
  protocol  = "sms"
  topic_arn = aws_sns_topic.sns_backup_topic.arn
}


resource "null_resource" "email_sns_notifications" {
  count = length(var.emails_to_notify)
  provisioner "local-exec" {
    command = "aws sns subscribe --topic-arn $TOPIC_ARN  --protocol email --notification-endpoint $EMAIL"

    environment = {
      TOPIC_ARN = aws_sns_topic.sns_backup_topic.arn
      EMAIL     = var.emails_to_notify[count.index]
    }
  }
}


resource "null_resource" "enable_sns_integration" {
  provisioner "local-exec" {
    command = "aws backup put-backup-vault-notifications --endpoint-url https://backup.$AWS_REGION.amazonaws.com --backup-vault-name $VAULT_NAME  --sns-topic-arn $SNS_TOPIC --backup-vault-events $EVENTS"

    environment = {
      AWS_REGION = var.aws_region
      VAULT_NAME = var.vault_main_name
      SNS_TOPIC  = aws_sns_topic.sns_backup_topic.arn
      EVENTS     = var.backup_events
    }
  }
  depends_on = [aws_sns_topic.sns_backup_topic]

}

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings