If you goal is to set this security_groups argument only when an instance of aws_security_group.test is created, you could use a splat expression as a concise way to express that:
security_groups = aws_security_group.test[*].id
The above means to take the id attribute of each of the objects in aws_security_group.test. Because of how you’ve written your count expression, there will either be zero elements or one element in that list.
Incidentally, please note that the security_groups argument for aws_instance is for EC2-Classic only. If you are using VPC (likely, unless this is a very old AWS account) then you will need to set vpc_security_group_ids instead in order to get correct behavior:
You didn’t mention which resource type you are setting security_groups for, so I just guessed aws_instance here. If you’re setting that argument in some other resource type then this advice may not apply.
It would be helpful if you could share a complete configuration example and the full error messages you are receiving, including all of the context about where in the configuration the problem is occurring. Otherwise we can only guess what’s going on here. Thanks!
on ../../../../modules/rs-apache/main.tf line 535, in resource "aws_lb" "internal_alb":
535: security_groups = [aws_security_group.permit_alb_apache.id, aws_security_group.permit_vpn.id, aws_security_group.permit_backend.id, aws_security_group.permit_k8.id, aws_security_group.test[*].id]
|----------------
| aws_security_group.test is empty tuple
| aws_security_group.permit_alb_apache.id is "sg-<removed>"
| aws_security_group.permit_backend.id is "sg-<removed>"
| aws_security_group.permit_k8.id is "sg-<removed>"
| aws_security_group.permit_vpn.id is "sg-<removed>"
Inappropriate value for attribute "security_groups": element 4: string
required.
All the other SGs work as intended, as they do not rely on any environment differences
It looks like the problem is that the aws_security_group.test[*].id expression returns a list (or, more accurately, a tuple) of ids of the instances of that resource, and so it’s introducing a nested list into your list, and thus that doesn’t match the expected type (set of strings) for security_groups.
One way to get that working is to employ the flatten function which will remove the intermediate list and produce a flat list of strings, which Terraform can then automatically convert into the flat set of strings the argument is expecting:
Both of these should produce the same result, so which to use is a subjective matter of which one seems to more clearly represent your intent to future readers.