Hi,
I have one bucket that replicates the content of the bucket to several buckets.
the rule was created manually and now I want to create it as code.
I have written this sort code to allow to create multiple rules in a single bucket.
variable "replication_rules" {
type = any
default = {}
}
resource "aws_s3_bucket_replication_configuration" "replication_rules" {
provider = aws.provider
count = var.versioning && length(var.replication_rules) > 0 ? length(var.replication_rules) : 0
bucket = aws_s3_bucket.b[0].id
role = var.replication_rules[count.index].role
rule {
id = var.replication_rules[count.index].id
priority = try(var.replication_rules[count.index].priority, null)
prefix = try(var.replication_rules[count.index].prefix, null)
status = try(var.replication_rules[count.index].status, "Disabled")
delete_marker_replication {
status = try(var.replication_rules[count.index].delete_marker_replication, "Disabled")
}
destination {
bucket = var.replication_rules[count.index].bucket
storage_class = try(var.replication_rules[count.index].storage_class, "STANDARD")
}
filter {}
}
}
the above code get variable value
replication_rules = [
{
role = "arn:aws:iam::XXXXXX:role/replication-test-1",
id = "replication-test-1",
status = true,
priority = 100,
bucket = "arn:aws:s3:::replication-test-1",
delete_marker_replication = "Enabled"
},
{
role = "arn:aws:iam::XXXXXXX:role/replication-test-2",
id = "replication-test-2",
status = true,
priority = 200,
bucket = "arn:aws:s3:::replication-test-2",
delete_marker_replication = "Enabled"
}
]
my issue is that the terraform show that he wants to create both rules but in the real world he overwrote the first rule with the other
Plan: 2 to add, 0 to change, 0 to destroy.
aws_s3_bucket_replication_configuration.replication_rules[0]: Creating...
aws_s3_bucket_replication_configuration.replication_rules[1]: Creating...
aws_s3_bucket_replication_configuration.replication_rules[1]: Creation complete after 1s [id=replication-bucket]
aws_s3_bucket_replication_configuration.replication_rules[0]: Creation complete after 2s [id=replication-bucket]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Outputs:
now if I will run again terraform plan this is the output that I’m getting
aws_s3_bucket.b[0]: Refreshing state... [id=replication-bucket]
aws_s3_bucket_replication_configuration.replication_rules[1]: Refreshing state... [id=replication-bucket]
aws_s3_bucket_replication_configuration.replication_rules[0]: Refreshing state... [id=replication-bucket]
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# aws_s3_bucket_replication_configuration.replication_rules[1] will be updated in-place
~ resource "aws_s3_bucket_replication_configuration" "replication_rules" {
id = "replication-bucket"
~ role = "arn:aws:iam::XXXXXXXXX:role/replication-test-1" -> "arn:aws:iam::XXXXXXXXX:role/replication-test-2"
# (1 unchanged attribute hidden)
~ rule {
~ id = "replication-test-1" -> "replication-test-2"
~ priority = 100 -> 200
# (1 unchanged attribute hidden)
~ destination {
~ bucket = "arn:aws:s3:::replication-test-1" -> "arn:aws:s3:::replication-test-2"
# (1 unchanged attribute hidden)
}
# (2 unchanged blocks hidden)
}
}
Plan: 0 to add, 1 to change, 0 to destroy.
the issue is that every plan applying the terraform resource is updating the existing one and deleting the other.
any idea why it happens and why I can’t attach multiple replication rules?
thanks