Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one combine concat with formatlist in terraform?

Tags:

terraform

Is it possible to concat lists produced by formatlist? The following gives the error

At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 6 is TypeList):

{
    "Action": [
        "s3:Get*",
        "s3:List*"
    ],
   "Effect": "Allow",
   "Resource": ["${concat(
         formatlist("arn:aws:s3:::%s", ${var.data_pipeline_s3_buckets}),
         formatlist("arn:aws:s3:::%s/*", ${var.data_pipeline_s3_buckets}))}"]
},
like image 449
junichiro Avatar asked Dec 14 '22 23:12

junichiro


2 Answers

It looks like you're trying to build a JSON array here, in which case something like the following should work:

{
    "Action": [
        "s3:Get*",
        "s3:List*"
    ],
   "Effect": "Allow",
   "Resource": ${jsonencode(
     concat(
       formatlist("arn:aws:s3:::%s", var.data_pipeline_s3_buckets),
       formatlist("arn:aws:s3:::%s/", var.data_pipeline_s3_buckets)
     )
   )}
}

Your original example has a few parts that are problematic here:

  • When referring to a variable when you're already inside a ${ ... } sequence you can't use a second ${ delimiter. This marker signals the transition from string context into interpolation expression context, so it's not valid when you're already in interpolation expression context.
  • When working with templates, all interpolation expressions must return strings because the template system doesn't have any iteration construct. The error message you got here is a little inaccurate (it should be telling you that only a single string is allowed) but it's resolved by the inclusion of jsonencode in the above example, thus turning the list into a string before returning it.
like image 188
Martin Atkins Avatar answered Mar 11 '23 04:03

Martin Atkins


That error message means that you are providing a list where you should be providing a string.

$concat doesn't do what I think you think it does; it does not concatenate the items in a list to form a string, it concatenates two lists to form another list.

You need to use $join instead.

I have a worked example at http://thecloudwoman.com/2017/05/how-to-use-a-terraform-list-variable/

like image 30
Rachel Avatar answered Mar 11 '23 03:03

Rachel