Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS Backup: Cross Region Copy #11589

Closed
dannyleesmith opened this issue Jan 14, 2020 · 8 comments · Fixed by #11923
Closed

AWS Backup: Cross Region Copy #11589

dannyleesmith opened this issue Jan 14, 2020 · 8 comments · Fixed by #11923
Assignees
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/backup Issues and PRs that pertain to the backup service.
Milestone

Comments

@dannyleesmith
Copy link

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

@dannyleesmith dannyleesmith added the enhancement Requests to existing resources that expand the functionality or scope. label Jan 14, 2020
@ghost ghost added the service/backup Issues and PRs that pertain to the backup service. label Jan 14, 2020
@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label Jan 14, 2020
@ewbankkit
Copy link
Contributor

Requires AWS SDK v1.28.1:

@slapula
Copy link
Contributor

slapula commented Feb 3, 2020

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

@grumpper
Copy link

grumpper commented Mar 6, 2020

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

@bflad bflad removed the needs-triage Waiting for first response or review from a maintainer. label Mar 16, 2020
@bflad bflad self-assigned this Mar 16, 2020
@VMGlobantGNBSudameris
Copy link

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.

@bflad bflad added this to the v2.58.0 milestone Apr 15, 2020
@bflad
Copy link
Contributor

bflad commented Apr 15, 2020

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. 👍

@ghost
Copy link

ghost commented Apr 17, 2020

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!

@VMGlobantGNBSudameris
Copy link

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]

}

@ghost
Copy link

ghost commented May 15, 2020

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!

@ghost ghost locked and limited conversation to collaborators May 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/backup Issues and PRs that pertain to the backup service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants