简体   繁体   English

S3 buckets 使用 terraform 进行双向复制

[英]S3 buckets two-way replication with terraform

Using terraform, I'm trying to create two s3 buckets, that each replicate back to each other.使用 terraform,我正在尝试创建两个 s3 存储桶,每个存储桶都相互复制。 This causes a dependency cycle.这会导致依赖循环。 I'm not sure how to handle this in terraform.我不确定如何在 terraform 中处理这个问题。

One solution I thought of is possibly split it into two sets of terraform scripts, one to create the two buckets, and then a second to modify those buckets adding the replication rules.我想到的一个解决方案可能是将其拆分为两组 terraform 脚本,一组用于创建两个存储桶,然后另一组用于修改那些添加复制规则的存储桶。

Is there another way to handle this scenario?还有另一种方法来处理这种情况吗?

Solution for you is described for you.为您描述解决方案。 It has some issues with data consistency but works very well.它在数据一致性方面存在一些问题,但效果很好。

So basically let's assume you have 2 buckets in 2 separated regions:所以基本上让我们假设您在 2 个独立的区域中有 2 个存储桶:

  • bucket1-us-east-1 bucket1-us-east-1
  • bucket1-us-west-2 bucket1-us-west-2

To two way replicate you need to setup replication from bucket1-us-east-1 to bucket1-us-west-2 .要进行双向复制,您需要设置从bucket1-us-east-1bucket1-us-west-2复制。 Then setup replication from bucket1-us-west-2 to bucket1-us-east-1然后设置从bucket1-us-west-2bucket1-us-east-1复制

Terraform solution Terraform解决方案

There is a problem with circular dependency, so you need to create resources first in one place then you need to enable replication for them:循环依赖存在问题,所以你需要先在一个地方创建资源,然后你需要为它们启用复制:

resource "aws_s3_bucket" "west" {
    provider = "aws.west"
    bucket   = "bucket1-us-west-2"
    region   = "us-west-2"
    acl      = "private"

    versioning {
        enabled = true
    }

  replication_configuration {
    role = "${aws_iam_role.some_replication_role.arn}"

    rules {
      id     = "replicate_all"
      prefix = ""
      status = "Enabled"

      destination {
        bucket        = "arn:aws:s3:::bucket1-us-east-1"
        storage_class = "STANDARD"
      }
    }
  }

}

resource "aws_s3_bucket" "east" {
    provider = "aws.east"
    bucket   = "bucket1-us-east-1"
    region   = "us-east-1"
    acl      = "private"

    versioning {
        enabled = true
    }

  replication_configuration {
    role = "${aws_iam_role.some_replication_role.arn}"

    rules {
      id     = "replicate_all"
      prefix = ""
      status = "Enabled"

      destination {
        bucket        = "arn:aws:s3:::bucket1-us-west-2"
        storage_class = "STANDARD"
      }
    }
  }

}

References参考

This is supported in AWS Terraform Provider version 4 (released Feb 2022) by separating out the replication configuration as a separate resource. AWS Terraform Provider 版本 4(2022 年 2 月发布)通过将复制配置分离为单独的资源来支持这一点。 From the provider documentation :来自提供商文档

# ... other configuration ...

resource "aws_s3_bucket" "east" {
  bucket = "tf-test-bucket-east-12345"
}

resource "aws_s3_bucket_versioning" "east" {
  bucket = aws_s3_bucket.east.id
  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket" "west" {
  provider = aws.west
  bucket   = "tf-test-bucket-west-12345"
}

resource "aws_s3_bucket_versioning" "west" {
  provider = aws.west

  bucket = aws_s3_bucket.west.id
  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket_replication_configuration" "east_to_west" {
  # Must have bucket versioning enabled first
  depends_on = [aws_s3_bucket_versioning.east]

  role   = aws_iam_role.east_replication.arn
  bucket = aws_s3_bucket.east.id

  rule {
    id = "foobar"

    filter {
      prefix = "foo"
    }

    status = "Enabled"

    destination {
      bucket        = aws_s3_bucket.west.arn
      storage_class = "STANDARD"
    }
  }
}

resource "aws_s3_bucket_replication_configuration" "west_to_east" {
  provider = aws.west
  # Must have bucket versioning enabled first
  depends_on = [aws_s3_bucket_versioning.west]

  role   = aws_iam_role.west_replication.arn
  bucket = aws_s3_bucket.west.id

  rule {
    id = "foobar"

    filter {
      prefix = "foo"
    }

    status = "Enabled"

    destination {
      bucket        = aws_s3_bucket.east.arn
      storage_class = "STANDARD"
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM