Bucket creation with for_each and input through yaml file

terraform version: 0.12.29

We have a aws s3 bucket creation resource block like this and it is taking inputs from the data.yaml file. The idea is to use for_each to loop over each bucket config indices in the yaml file.

But for some of the bucket we need replication rule. Is there some way we can use a conditional block inside the resource block and iterate through the yaml file and only create replication if the parameter in the data.yaml under bucket config is set True.

here is the data.yaml and main.tf file for AWS s3 bucket creation.


  • name: my-unique-s3bucket
    Name : my-unique-s3bucket
    env: integration
  • name: new-s3bucket
    Name : new-s3bucket
    env: integration


locals {
contentMap = { for r in yamldecode(file("./data.yaml"))[“buckets”] : lower(r.name) =>
name = r.name,
tags = r.tags

resource “aws_s3_bucket” “s3bucket” {
for_each = local.contentMap

bucket = each.value.name
acl = “private”
force_destroy = true
tags = each.value.tags

Since for_each is used in the resource block, we cannot use the count or dynamic block for the conditional replication configuration inside the aws_s3_bucket