# How to conditionally remove / add object from a dynamic content

**URL:** <https://discuss.hashicorp.com/t/how-to-conditionally-remove-add-object-from-a-dynamic-content/62624>\
**Category:** Terraform\
**Created:** [February 9, 2024, 12:35pm UTC](https://discuss.hashicorp.com/t/how-to-conditionally-remove-add-object-from-a-dynamic-content/62624 "2024-02-09T12:35:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gtwuser](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gtwuser/32/39239_2.png) [@gtwuser](https://discuss.hashicorp.com/u/gtwuser)\
**Post date:** [February 9, 2024, 12:35pm UTC](https://discuss.hashicorp.com/t/how-to-conditionally-remove-add-object-from-a-dynamic-content/62624/1 "2024-02-09T12:35:14Z")

</div>

Please help me to update the terraform script if i want to conditionally remove `replication_time` from the dynamically created content, say if a `var.rm == true` then dont set `replication_time`.

```auto
resource "aws_s3_bucket_replication_configuration" "replication" {
  dynamic "rule" {
    # we use deploy flag so rule value may be null
    for_each = {
      for k, v in var.replication_rules :
      k => v if v != null
    }

    content {
      destination {
         replication_time {
          status = rule.value.replication_time_control
          time {
            minutes = 15
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![rtwolfe](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rtwolfe/32/39999_2.png) [@rtwolfe](https://discuss.hashicorp.com/u/rtwolfe)\
**Post date:** [February 9, 2024, 9:01pm UTC](https://discuss.hashicorp.com/t/how-to-conditionally-remove-add-object-from-a-dynamic-content/62624/2 "2024-02-09T21:01:56Z")

</div>

[Charlotte v2.1 Transformer - Goggle DeepMind Assist]

To conditionally include or exclude the `replication_time` block based on a variable, such as `var.rm`, you can use Terraform’s `dynamic` block inside the `destination` block. This approach allows you to conditionally add the entire `replication_time` block based on the value of `var.rm`.

Here’s an updated version of your Terraform script with a conditional `dynamic` block for `replication_time`:

hclCopy code

```auto
resource "aws_s3_bucket_replication_configuration" "replication" {
  dynamic "rule" {
    for_each = {for k, v in var.replication_rules : k => v if v != null}

    content {
      destination {
        # Conditional dynamic block for replication_time
        dynamic "replication_time" {
          for_each = var.rm == true ? [] : [1] # If var.rm is true, don't include replication_time, otherwise include it.
          content {
            status = rule.value.replication_time_control
            time {
              minutes = 15
            }
          }
        }
      }
    }
  }
}

```

In this script, the `dynamic "replication_time"` block will only be included if `var.rm` is not `true`. The `for_each` in the `dynamic "replication_time"` block is set to an empty list `[]` if `var.rm` is `true`, effectively removing the block, and to `[1]` (a list with a single element) if `var.rm` is not `true`, which includes the block exactly once.

This approach maintains the dynamic nature of your Terraform configuration while allowing you to conditionally include or exclude specific parts of the configuration. Make sure that `var.rm` is defined in your variables file or passed as an input variable to your Terraform configuration.

---

<div class="post-metadata">

**Author:** ![gtwuser](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gtwuser/32/39239_2.png) [@gtwuser](https://discuss.hashicorp.com/u/gtwuser)\
**Post date:** [February 10, 2024, 6:11am UTC](https://discuss.hashicorp.com/t/how-to-conditionally-remove-add-object-from-a-dynamic-content/62624/3 "2024-02-10T06:11:53Z")

</div>

> [@rtwolfe](#):
>
> Charlotte v2.1 Transformer - Goggle DeepMind Assist

Thank you @rtwolfe 🙂 it works. Appreciate your timely response. 🙏

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/hashicorp/original/1X/79bf76adfd92a75631996933098f9ce29d37abf3.svg) [@system](https://discuss.hashicorp.com/u/system)\
**Post date:** [April 12, 2024, 6:12am UTC](https://discuss.hashicorp.com/t/how-to-conditionally-remove-add-object-from-a-dynamic-content/62624/4 "2024-04-12T06:12:02Z")

</div>

Closed on April 12, 2024, 6:12am UTC

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.
