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"
}
}
]
}