Terraform plan garbles jq/json output, but terraform console doesn't

Viewed 145

I've been building a github automation with TF to build an S3 bucket with one or more IAM roles as principals. When I assign the roles as JSON to a var (jsonencode/formatlist), testing with terraform console displays the resulting policy perfectly.

But when I run a TF plan the json is garbled instead, resulting in a badly formed principal block.

Here's my variable block with AWS account numbers

variable "account_num" {
  default = [
  "123456789011",
  "123456789012"
  ]
}

Terraform code block looks like this.

"Principal": {
    "AWS": ${jsonencode(formatlist("arn:aws:iam::%s:role/role-access", var.account_num))}
},

When I use the terraform console to try this var block with jsonencode/formatlist, it creates the policy block perfectly.

$ terraform console    
> jsonencode(formatlist("arn:aws:iam::%s:role/role-access", var.account_num))
      ["arn:aws:iam::123456789011:role/role-access","arn:aws:iam::123456789012:role/role-access"]

However in the actual terraform plan, the block is garbled.

+ Principal = {
    + AWS = [
        + <<~EOT
              arn:aws:iam::[
                "123456789011",
                "123456789012"
              ]:role/role-access
          EOT,
      ]
  }

Thanks for any help!

1 Answers

I resolved this by declaring the variable type "string" variable

"account_num" { 
type = list(string) 
    default = [] 
}
Related