【问题标题】:Pass on a map value as string to an argument list将映射值作为字符串传递给参数列表
【发布时间】:2022-07-27 07:58:20
【问题描述】:

AWS Glue 作业采用默认参数列表。我需要阅读包含所有这些参数的 YAML 配置文件。一些参数是嵌套的 YAML,我需要将嵌套值作为字符串传递,我不确定在 Terraform 中是否可行。

resource \"aws_glue_job\" \"glue_jobs\" {
 name = xxx
 default_arguments = zipmap([ for key, value in local.param_config_file[\"default-params\"] : \"${key}\" ], [ for key, value in local.param_config_file[\"default-params\"] : value ])
}

配置文件结构:

job-description: Initial load
enable-continuous-cloudwatch-log: true
enable-metrics: false
enable-spark-ui: true
job-bookmark-option: job-bookmark-disable
job-language: python

connectors:
  conn-name-1: xxx
  conn-name-2: xxx

script-file: path/to/script_file

default-params:
  arg1: rds_db
  arg2: rds_cat_name

   schemas:
     schema_1: schema_name_1
     schema_2: schema_name_2

  rds_input_table_list: 
      - database: db_name
        schema: schema_name
        table: table_name
      - database: db_name
        schema: schema_name
        table: table_name
  rds_output_table: output_table
  # # --SQL
  sql: |
    This is the SQL definition for each job
  sql_type: sparksql

仅当键的值具有单个值时,zipmap 解决方案才有效。例如:键 = \"值\" 但是,当值是嵌套映射时,让我们以“模式”为例,它的映射值为

 schemas:
   schema_1: schema_name_1
   schema_2: schema_name_2

那么,如何将它作为字符串传递给参数的值呢?

argument_schema = string(         
                          schema_1: schema_name_1
                          schema_2: schema_name_2
                        )

或类似的方法。

换句话说,我如何将对象/对象列表转换为字符串并将其作为一个变量的单个字符串值传递。

  • 您当前的代码有什么问题?您的 zipmap 的上下文是什么?您能否提供使用 zipmap 的实际资源代码?
  • 上面的代码仅适用于非嵌套值。 Key = Value Key = Value 但是,如果 Value 本身是一个嵌套对象,我不知道如何将整个值映射作为字符串传递。例如: Key = Value Key = nested_map_as_in_the_config_file 如果存在嵌套值,zipmap 会提示错误。
  • 那么你能提供实际的、完全可重现的代码吗?你得到什么错误?遗憾的是,您的问题缺乏细节且不清楚。
  • 因此,您可以提供实际的错误消息,显示错误发生的确切行?还有你在哪里以及如何使用你的 zipmap?您的问题缺乏细节且不清楚。
  • 我们肯定需要更多信息来填补这里的空白,但通常在大多数语言中,这可以通过转储到支持复杂结构的字符串格式来解决,例如YAML、JSON 等,然后在消费者中解析输出。

标签: terraform terraform-provider-aws terraform-provider-azure terraform0.12+


【解决方案1】:

如果 YAML 变量的值是对象本身的映射/列表,这就是我将其转换为字符串的方式:

default-params:
  arg1: rds_db
  arg2: rds_cat_name

   schemas:
     schema_1: schema_name_1
     schema_2: schema_name_2

  rds_input_table_list: 
      - database: db_name
        schema: schema_name
        table: table_name
      - database: db_name
        schema: schema_name
        table: table_name
  rds_output_table: output_table
  # # --SQL
  sql: |
    This is the SQL definition for each job
  sql_type: sparksql

join(";", [ for par in local.param.other-params: join(",", tolist( [for key, value in par: format("%s=%s",key,value)])) ])

如果您仍然认为将此类列表转换为字符串有更好的方法,请告知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多