Terraform JSON 生成 [英] Terraform JSON generation

查看:73
本文介绍了Terraform JSON 生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 terraform 创建一个 AWS 仪表板 显示 S3 指标.我正在考虑遍历存储在列表变量中的所有 S3 存储桶并生成仪表板 json.

I'm trying to create an AWS dashboard using terraform to display the S3 metrics. I was thinking of looping through all the S3 buckets stored in a list variable and generate the dashboard json.

for 循环能够添加指标,但我无法删除尾随逗号,这会导致错误的 json.

The for loop is able to add the metrics, but I'm not able to remove the trailing comma, which results in an erroneous json.

  1. 有没有一种简单的方法可以使用这种方法来修复这个 json?
  2. 有没有更好的 json 处理方式?
  3. 我应该使用 terraform 进行此处理吗?

代码片段:-

dashboard_body = <<EOF
 {
  "start":"-P6M",
   "widgets": [
       {
          "type":"metric",
          "x":0,
          "y":0,
          "width":12,
          "height":6,
          "properties":{
             "metrics":[
%{ for bucket in var.buckets }
[
                   "AWS/S3",
                   "BucketSizeBytes",
                   "StorageType",
                   "StandardStorage",
                    "BucketName",
                   "${bucket}"
]
%{ endfor }
             ],
             "period":86400,
             "stat":"Average",
             "region":"us-east-1",
             "title":"Storage usage"
          }
       }
   ]
 }
 EOF

解决方法:-我最终在指标"末尾硬编码了一个额外的聚合.大批.无论如何,我需要总数,这是一个简单的解决方法.@kharandziuk 是实现的理想方式,但即便如此,您也可能需要使用此解决方法.

Workaround:- I ended up hardcoding an extra aggregation at the end of the "metrics" array. I needed the total anyway and this was an easy workaround. @kharandziuk is the ideal way to implement, but even in that you might need to use this workaround.

最终代码:-

{
  "start":"-P6M",
   "widgets": [
       {
          "type":"metric",
          "x":0,
          "y":0,
          "width":12,
          "height":6,
          "properties":{
             "metrics":[
                %{ for bucket in buckets }
                [
                   "AWS/S3",
                   "BucketSizeBytes",
                   "StorageType",
                   "StandardStorage",
                    "BucketName",
                   "${bucket}"
                ],
                %{ endfor }
                [
                  { "expression": "SUM(METRICS())", "label": "Total Storage", "id": "e3" }
                ]
             ],
             "period":86400,
             "stat":"Average",
             "region":"us-east-1",
             "title":"Storage usage"
          }
       }
   ]
 }

推荐答案

有没有一种简单的方法可以使用这种方法来修复这个 json?

Is there an easy way to fix this json using this approach?

我会使用 jsonencode 在 HCL 中制作地图并将其转换为 JSON 的函数.

I'd use jsonencode function to make a map in HCL and translate it into JSON.

有没有更好的json处理方式?

Is there a better way to do json processing?

有.试试 templatefile 函数.您将拥有一个模板并在其中插入一些变量.

There is. Try templatefile function. You will have a template and interpolate some variables into it.

我应该使用 terraform 进行此处理吗?

Should I be using terraform for this processing?

是的,你应该这样做.Terraform 提供了执行此操作的工具.代码应该类似于下面的示例代码:

Yes, you should. Terraform provides tools to do it. The code should look similar to the sample coed below:

variable "buckets" {
  default = ["max", "sandeep"]
}

locals{
  dashboard_body = templatefile("${path.module}/file.json.tmpl", {
    metrics = jsonencode( # we create the list in HCL and transform it into json
      [for bucket in var.buckets: [
          "AWS/S3",
          "BucketSizeBytes",
          "StorageType",
          "StandardStorage",
          "BucketName",
          "${bucket}"
        ]
      ]
    )
  })
}

output "result" {
  value = local.dashboard_body
}

其中 file.json.tmpl 是:

{
  "start":"-P6M",
    "widgets": [
    {
      "type":"metric",
      "x":0,
      "y":0,
      "width":12,
      "height":6,
      "properties":{
        "metrics": ${metrics},
        "period":86400,
        "stat":"Average",
        "region":"us-east-1",
        "title":"Storage usage"
      }
    }
  ]
}

这篇关于Terraform JSON 生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆