Terraform JSON生成 [英] Terraform JSON generation

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

问题描述

我正在尝试使用terraform创建 AWS仪表板显示S3指标.我当时想遍历存储在list变量中的所有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天全站免登陆