# How to use dynamic block for resource argument

**URL:** <https://discuss.hashicorp.com/t/how-to-use-dynamic-block-for-resource-argument/51651>\
**Category:** Terraform\
**Created:** [March 20, 2023, 9:35am UTC](https://discuss.hashicorp.com/t/how-to-use-dynamic-block-for-resource-argument/51651 "2023-03-20T09:35:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alibek555](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/alibek555/32/23986_2.png) [@alibek555](https://discuss.hashicorp.com/u/alibek555)\
**Post date:** [March 20, 2023, 9:35am UTC](https://discuss.hashicorp.com/t/how-to-use-dynamic-block-for-resource-argument/51651/1 "2023-03-20T09:35:44Z")

</div>

i want to specify data for argument ‘data’ with dynamic block. Is it possible? why this code doesn’t work? i receive Error: “Missing required argument. The argument “data” is required, but no definition was found”. I have variables.tf file with all necessary variables.

```auto
resource "aws_prometheus_rule_group_namespace" "alert-rules" {
  name = var.prometheus_rule_group

  dynamic "data" {
    for_each = var.alert-rules
    content {
      data = <<EOF
groups:
  - name: alerts
    rules:
    - alert: data.value["alert-name"]
      expr: data.value["expr"]
      for: data.value["period"]
      labels:
        severity: data.value["severity"]
      annotations:
        summary: data.value["summary"]
EOF
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![macmiranda](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/macmiranda/32/34065_2.png) [@macmiranda](https://discuss.hashicorp.com/u/macmiranda)\
**Post date:** [March 20, 2023, 11:31am UTC](https://discuss.hashicorp.com/t/how-to-use-dynamic-block-for-resource-argument/51651/2 "2023-03-20T11:31:38Z")

</div>

`data` is a single _argument_ for that resource. You can’t use a dynamic block for that (it would create multiple `data` _blocks_ inside the resource definition)

You have to pass the `data` argument as a single string or variable in this case.

Check out the [documentation](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/prometheus_rule_group_namespace#data).

You can use the [yamlencode](https://developer.hashicorp.com/terraform/language/functions/yamlencode) function though, once you’ve built the terraform object with the values you need.
