【问题标题】:How to access JSON from external data source in Terraform?如何从 Terraform 中的外部数据源访问 JSON?
【发布时间】:2019-08-27 20:34:59
【问题描述】:

我从 http terraform 数据源接收 JSON

data "http" "example" {
  url = "${var.cloudwatch_endpoint}/api/v0/components"

  # Optional request headers
  request_headers {
    "Accept" = "application/json"
    "X-Api-Key" = "${var.api_key}"
  }
}

它输出以下内容。

http = [{"componentID":"k8QEbeuHdDnU","name":"Jenkins","description":"","status":"Partial Outage","order":1553796836},{"componentID":"ui","name":"ui","description":"","status":"Operational","order":1554483781},{"componentID":"auth","name":"auth","description":"","status":"Operational","order":1554483781},{"componentID":"elig","name":"elig","description":"","status":"Operational","order":1554483781},{"componentID":"kong","name":"kong","description":"","status":"Operational","order":1554483781}]

这是 terraform 中的字符串。为了将此字符串转换为 JSON,我将其传递给 external 数据源,这是一个简单的 ruby​​ 函数。这是通过它的地形。

data "external" "component_ids" {
  program = ["ruby", "./fetchComponent.rb",]

  query = {
    data = "${data.http.example.body}"

  }
}

这里是红宝石函数

 #!/usr/bin/env ruby
require 'json'
data = JSON.parse(STDIN.read)
results = data.to_json
STDOUT.write results

所有这些都有效。 external 数据输出以下内容(看起来与 http 输出相同),但根据 terraform 文档,这应该是 map

external1 = {
  data = [{"componentID":"k8QEbeuHdDnU","name":"Jenkins","description":"","status":"Partial Outage","order":1553796836},{"componentID":"ui","name":"ui","description":"","status":"Operational","order":1554483781},{"componentID":"auth","name":"auth","description":"","status":"Operational","order":1554483781},{"componentID":"elig","name":"elig","description":"","status":"Operational","order":1554483781},{"componentID":"kong","name":"kong","description":"","status":"Operational","order":1554483781}]
}

我期待我现在可以访问 external 数据源中的数据。我做不到。

最终我想要做的是创建一个listcomponentID 变量,这些变量位于external 数据源中。

我尝试过的一些事情

* output.external: key "0" does not exist in map data.external.component_ids.result in:

${data.external.component_ids.result[0]}

* output.external: At column 3, line 1: element: argument 1 should be type list, got type string in:

${element(data.external.component_ids.result["componentID"],0)}
* output.external: key "componentID" does not exist in map data.external.component_ids.result in:

${data.external.component_ids.result["componentID"]}
ternal: lookup: lookup failed to find 'componentID' in:

${lookup(data.external.component_ids.*.result[0], "componentID")}

感谢您的帮助。

【问题讨论】:

  • 它可能是一个映射,但只有一个 kv 对,其中值是从外部数据输出的整个字符串。问题可能是 Ruby 脚本输出格式。

标签: ruby terraform


【解决方案1】:

无法使用变量cloudwatch_endpoint 进行测试,所以我必须考虑解决方案。

Terraform 在 0.11.x 之前无法直接解码 json。但是有一种解决方法可以处理嵌套列表。

您的 ruby​​ 需要调整以使输出为下面的变量http,那么您应该可以得到您需要的东西。

$ cat main.tf 
variable "http" {
  type = "list"
  default = [{componentID = "k8QEbeuHdDnU", name = "Jenkins"}]
}

output "http" {
  value = "${lookup(var.http[0], "componentID")}"
}

$ terraform apply 

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

http = k8QEbeuHdDnU

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 2018-08-18
    • 2013-01-14
    • 1970-01-01
    相关资源
    最近更新 更多