0
votes

Using terraform, I'm trying to create two s3 buckets, that each replicate back to each other. This causes a dependency cycle. I'm not sure how to handle this in 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.

Is there another way to handle this scenario?

1

1 Answers

0
votes

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:

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

To two way replicate you need to setup replication from bucket1-us-east-1 to bucket1-us-west-2. Then setup replication from bucket1-us-west-2 to bucket1-us-east-1

Terraform solution

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