Search code examples
terraformterraform-provider-gcpgoogle-datastream

How can I pass a list of values to a block within a resource


I am trying to set up a Datastream connection from MySQL to BigQuery using Terraform (docs here). The docs show this structure for specifying which tables to include:

resource "google_datastream_stream" "default" {
    depends_on = [
        google_kms_crypto_key_iam_member.key_user
    ]
    stream_id = "my-stream"
    desired_state = "NOT_STARTED"
    location = "us-central1"
    display_name = "my stream"
    labels = {
        key = "value"
    }
    source_config {
        source_connection_profile = google_datastream_connection_profile.source_connection_profile.id
        mysql_source_config {
            include_objects {
                mysql_databases {
                    database = "my-database"
                    mysql_tables {
                        table = "includedTable"
                        mysql_columns {
                            column = "includedColumn"
                            data_type = "VARCHAR"
                            collation = "utf8mb4"
                            primary_key = false
                            nullable = false
                            ordinal_position = 0
                        }
                    }
                }
            }

But I want to specify several tables to include not just one. Further info in the docs say:

The mysql_databases block supports:

database - (Required) Database name.

mysql_tables - (Optional) Tables in the database. Structure is documented below.

The mysql_tables block supports:

table - (Required) Table name.

mysql_columns - (Optional) MySQL columns in the schema. When unspecified as part of include/exclude objects, includes/excludes everything. Structure is documented below.

So it says the mysql_databases block is where I pass in the all the tables I want to include but the example syntax only shows how to pass one table like table = "myTable". How can I pass a list of values here or something like that? I'm quite new to Terraform so maybe I'm missing something standard functionality. Thanks in advance.


Solution

  • This is the solution to dynamically create nested blocks:

      dynamic "<block_name>" {
        for_each = toset(var.<your-var>)
        content {
          <thing> = <block_name>.value
        }
      }