问题
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.
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.
- Is there an easy way to fix this json using this approach?
- Is there a better way to do json processing?
- Should I be using terraform for this processing?
Code snippet :-
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
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.
Final code:-
{
"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"
}
}
]
}
回答1:
Is there an easy way to fix this json using this approach?
I'd use jsonencode function to make a map in HCL and translate it into JSON.
Is there a better way to do json processing?
There is. Try templatefile function. You will have a template and interpolate some variables into it.
Should I be using terraform for this processing?
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
}
where file.json.tmpl
is:
{
"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"
}
}
]
}
来源:https://stackoverflow.com/questions/62888707/terraform-json-generation