I am creating an AWS Stack Set (at org level) which is written in cloud formation. For the cloud formation, it is written in a terraform template file so that variables are passed from terraform. However there one part which I am having issues with.

The `for` section needs to look the principals and create `Principal` block in the template. However the last index of the loop contains a comma which will makes the stack template invalid.

Is there away in terraform to prevent the last comma in a loop?

main.tf:

```
variable "cross_account" {
  type = list(object({
    principals  = list(string)
  }))
  default = [
    {
      principals  = ["ACCOUNT_ID1","ACCOUNT_ID2","ACCOUNT_ID3"]
    }
  ]
}
output "test" {
  value = templatefile("./json.tpl", {
    principals = var.cross_account[0].principals
  })
}

```

Template file (json.tpl):

```
"Principal": {
"AWS": [
  %{ for princ in principals ~}
      {
        "Fn::Sub": "arn:aws:iam::${princ}:root"
      }
  , # <--------------- Need to prevent this line on the last loop index
  %{ endfor ~}
]

```

I have shorten the code down to only the bits needed above. Thanks