0
votes

I'm currently writing multiple AWS Kinesis Data Analytics applications with Terraform. Both applications share the same Kinesis input stream and thus have the same schema:

resource "aws_kinesis_analytics_application" "example1" {
  name = "example1"

  inputs {
    # SHARE THIS ATTRIBUTE VALUE WITH example2
    schema {
      record_columns {
        mapping  = "$.FIELD1"
        name     = "FIELD1"
        sql_type = "VARCHAR(32)"
      }

      record_columns {
        mapping  = "$.FIELD2"
        name     = "FIELD2"
        sql_type = "VARCHAR(32)"
      }

      record_format {
        mapping_parameters {
          json {
            record_row_path = "$"
          }
        }
      }

    }
  }

  outputs {
    # other attributes

    schema {
      record_format_type = "JSON"
    }
  }

  code = "SQL code ...."
}

resource "aws_kinesis_analytics_application" "example2" {
  name = "example2"

  inputs {
    # SHARE THIS ATTRIBUTE VALUE WITH example1
    schema {
      record_columns {
        mapping  = "$.FIELD1"
        name     = "FIELD1"
        sql_type = "VARCHAR(32)"
      }

      record_columns {
        mapping  = "$.FIELD2"
        name     = "FIELD2"
        sql_type = "VARCHAR(32)"
      }

      record_format {
        mapping_parameters {
          json {
            record_row_path = "$"
          }
        }
      }

    }
  }

  outputs {
    # Other attributes

    schema {
      record_format_type = "JSON"
    }
  }

  code = "SQL code...."
}

I'm assuming that this is possible if there is a data source available for schema:

data "aws_kinesis_analytics_application_schema" "example_input" {
      record_columns {
        mapping  = "$.FIELD1"
        name     = "FIELD1"
        sql_type = "VARCHAR(32)"
      }

      record_columns {
        mapping  = "$.FIELD2"
        name     = "FIELD2"
        sql_type = "VARCHAR(32)"
      }

      record_format {
        mapping_parameters {
          json {
            record_row_path = "$"
          }
        }
      }
}

resource "aws_kinesis_analytics_application" "example1" {
  name = "example1"

  inputs {
    # Other attributes
    schema = "${data.aws_kinesis_analytics_application_schema.example_input}"
  }

  outputs {
    # other attributes

    schema {
      record_format_type = "JSON"
    }
  }

  code = "SQL code ...."
}

resource "aws_kinesis_analytics_application" "example2" {
  name = "example2"

  inputs {
    # Other attributes
    schema = "${data.aws_kinesis_analytics_application_schema.example_input}"
  }

  outputs {
    # Other attributes

    schema {
      record_format_type = "JSON"
    }
  }

  code = "SQL code...."
}

However, there is no data source available for this specific attribute of this resource. Is it possible to share such a complex attribute between resources without using a data source?

1

1 Answers

0
votes

You can use variables for that:

variable schema {
   record_columns = [
      {....},
      {....}
   ]
   record_format {
      mappings_parameters = [
         {....}
      ]
   }
}

resource "aws_kinesis_analytics_application" "example1" {
  name = "example1"

  inputs {
    schema = "${var.schema}"
  }

  outputs {
    # other attributes

    schema {
      record_format_type = "JSON"
    }
  }

  code = "SQL code ...."
}

resource "aws_kinesis_analytics_application" "example2" {
  name = "example2"

  inputs {
    # SHARE THIS ATTRIBUTE VALUE WITH example1
    schema = "${var.schema}"
  }

  outputs {
    # Other attributes

    schema {
      record_format_type = "JSON"
    }
  }

  code = "SQL code...."
}