【问题标题】:Terraform 0.11 - Share complex attribute values between resourcesTerraform 0.11 - 在资源之间共享复杂的属性值
【发布时间】:2020-04-26 05:27:22
【问题描述】:

我目前正在使用 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...."
}

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

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    您可以为此使用变量:

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

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2019-03-07
      • 2021-10-10
      • 2020-01-10
      • 1970-01-01
      • 2022-01-23
      • 2013-12-10
      • 2011-05-11
      • 2019-10-07
      相关资源
      最近更新 更多