Terraform 0.11-在资源之间共享复杂的属性值

时间:2020-01-08 16:16:48

标签: terraform terraform-provider-aws

我目前正在使用Terraform编写多个AWS Kinesis Data Analytics应用程序。两个应用程序共享相同的Kinesis输入流,因此具有相同的架构:

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...."
}

我假设如果有可用于模式的数据源,这是可能的:

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...."
}

但是,没有用于此资源特定属性的数据源。是否可以在不使用数据源的情况下在资源之间共享如此复杂的属性?

1 个答案:

答案 0 :(得分:0)

您可以为此使用变量:

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...."
}
相关问题