Terraform JSON 代
Terraform JSON generation
我正在尝试创建一个 AWS dashboard using terraform 来显示 S3 指标。我正在考虑遍历存储在列表变量中的所有 S3 存储桶并生成仪表板 json.
for 循环能够添加指标,但我无法删除尾随逗号,这会导致错误 json。
- 有没有一种简单的方法可以使用这种方法解决这个问题json?
- 是否有更好的方法来进行 json 处理?
- 我应该使用 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 是理想的实施方式,但即便如此,您也可能需要使用此解决方法。
最终代码:-
{
"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"
}
}
]
}
Is there an easy way to fix this json using this approach?
我会使用 jsonencode
函数在 HCL 中制作地图并将其翻译成 JSON。
Is there a better way to do json processing?
有。试试 templatefile
函数。您将拥有一个模板并将一些变量插入其中。
Should I be using terraform for this processing?
是的,你应该。 Terraform 提供了执行此操作的工具。该代码应类似于下面的示例代码:
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"
}
}
]
}
我正在尝试创建一个 AWS dashboard using terraform 来显示 S3 指标。我正在考虑遍历存储在列表变量中的所有 S3 存储桶并生成仪表板 json.
for 循环能够添加指标,但我无法删除尾随逗号,这会导致错误 json。
- 有没有一种简单的方法可以使用这种方法解决这个问题json?
- 是否有更好的方法来进行 json 处理?
- 我应该使用 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 是理想的实施方式,但即便如此,您也可能需要使用此解决方法。
最终代码:-
{
"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"
}
}
]
}
Is there an easy way to fix this json using this approach?
我会使用 jsonencode
函数在 HCL 中制作地图并将其翻译成 JSON。
Is there a better way to do json processing?
有。试试 templatefile
函数。您将拥有一个模板并将一些变量插入其中。
Should I be using terraform for this processing?
是的,你应该。 Terraform 提供了执行此操作的工具。该代码应类似于下面的示例代码:
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"
}
}
]
}