It seems to be tricky to append a value to an array. I thought that something like this should work:
"${compact(concat(split(",", var.security_groups), var.common_security_group))}"
But it seems to cause a parse error.
Hi @joshuaspence - the problem here is concat
wants a list as it's second parameter. This will go down on record as the ugliest Terraform known to man but here goes:
${compact(concat(split(",", var.security_groups), split(",", join(",", var.common_security_group))))}
I believe _should_ work. However, this will be hugely simplified by native lists which I am working on right now. Please feel free to reopen if this doesn't work for you!
@jen20 - Hi Jen, I'm trying to do exactly this, but with security groups for the aws_launch_configuration
resource.
When i use
security_groups = "${compact(concat(split(",", var.instance_security_groups), split(",", join(",", aws_security_group.instance.id))))}"
terraform replies with the error:
* aws_launch_configuration.main: security_groups: should be a list
So i tried the new list object just with the one item
security_groups = "${list(aws_security_group.instance.id)}"
and it errors with
* aws_launch_configuration.main: security_groups: should be a list
What i'm trying to do is set security groups to use a list of security group IDs i pass in via environment variable TF_VAR_instance_security_groups
AND the security group I created via the config aws_security_group.instance.id
I was hoping the command
security_groups = "${concat(list(aws_security_group.instance.id), list(split(",", var.instance_security_groups)))}"
works but that too returns the same list error as the above. Now, if i set security_groups to a blank list like security_groups = "${list()}"
, that works fine.
Any ideas?
Turns out, the best way to do this is
security_groups = ["${aws_security_group.instance.id}", "${compact(split(",", var.instance_security_groups))}"]
I've endlessly tried all sorts of different combinations using list
, compact
, split
, join
and concat
; this is the only way that works.
Thanks @djenriquez . I had to do something like this too. I must say that the syntax of []
, concat
, and compact
, and how they interact, is very mysterious to me and doesn't seem consistent with the documentation, nor consistent with any other programming or configuration language I've ever come across.
Maybe I'm missing something here, but using any of the interpolation functions isn't necessary here. You can get away with just putting the item and the list into a new list (works for me on version 0.8.6):
security_groups = [
"${aws_security_group.instance.id}",
"${var.instance_security_groups)}",
]
Thank you @bosgood! I didn't realise that the [ ... ]
syntax also combined strings and lists. I had a ridiculous list builder with compact(distinct(concat(list(var.a_string), var.a_list)))
to do this until I saw your comment.
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
Maybe I'm missing something here, but using any of the interpolation functions isn't necessary here. You can get away with just putting the item and the list into a new list (works for me on version 0.8.6):