Would it be possible to attempt to find two resources and error if none of them are found?
Example:
data "aws_resourcegroupstaggingapi_resources" "elb_classic" {
resource_type_filters = ["elasticloadbalancing:loadbalancer"]
depends_on = [ ]
tag_filter {
key = "kubernetes.io/service-name"
values = ["service-name"]
}
}
data "aws_resourcegroupstaggingapi_resources" "elb_v2" {
resource_type_filters = ["elasticloadbalancing:loadbalancer"]
depends_on = [ ]
tag_filter {
key = "service.k8s.aws/stack"
values = ["service-name"]
}
}
data "aws_lb" "lb" {
arn = try(data.aws_resourcegroupstaggingapi_resources.elb_classic.resource_tag_mapping_list[0].resource_arn, try(data.aws_resourcegroupstaggingapi_resources.elb_v2.resource_tag_mapping_list[0].resource_arn, "LB not found"))
}
Is there perhaps a better way of configuring this?
try evaluates all of its argument expressions in turn and returns the result of the first one that does not produce any errors.
so you can do:
data "aws_lb" "lb" {
arn = try(data.aws_resourcegroupstaggingapi_resources.elb_classic.resource_tag_mapping_list[0].resource_arn, data.aws_resourcegroupstaggingapi_resources.elb_v2.resource_tag_mapping_list[0].resource_arn, "LB not found")
}
try
returns the first expression that does not throw an error, which in this case is the string you've defined, which in this case will imply an error given that "LB not found" is certainly not a valid ARN.
│ Error: "arn" (LB not found) is an invalid ARN: arn: invalid prefix