How to extract the arn of a AWS LB using a tag that has . & / in it

Hello,

I am trying to extract out the arn of an AWS LB. One of the tags for this ALB is as follows
elbv2.k8.aws/cluster = “my-shiny-eks-cluster” and ideally also narrow it down by the vpc-id too, but if I can live with just using elbv2.k8.aws/cluster tag to get the arn.

When I tried to do the following, I am not getting anywhere. I tried using the “filter” command but no luck there either. I did read somewhere that the “filter” command is not available for all data resources. What are my options here?

Thank you!
NJN

data "aws_lb" "alb_listener_details" {
  tags {
    elbv2.k8.aws/cluster = "my-shiny-eks-cluster"
  }
}

The details of the data source can be found at Terraform Registry

As you can see it doesn’t have a “filter” block or attribute, and instead has three optional attributes - arn, name & tags.

What is the problem you are seeing? That nothing is being returned, or that you are getting an error?

I am sorry I should have mentioned the error message. Please note the forums are blocked at work, so I am typing up the alerts, excuse any typos here, I don’t have any typos in my code.

I am running terraform 1.3.1

When I change the data source block with the exact “name” or the “arn” of the ALB, I get the results. I just don’t know what I am doing wrong when I compose the tags map especially when it has characters like “.” & “/” in it. I have tried the standard linux \ options, but no luck.

Also please note there are 2 other tags AWS populates, so the following 3 tags are present in this ALB(Application Load Balancer)

ingress.k8s.aws/resource = LoadBalancer
ingress.k8s.aws/stack = xyzalb
elbv2.k8.aws/cluster = "my-shiny-eks-cluster"

The error message I get is

Error: Argument or block definition is required.

on main.tf line 33, in data "aws_lb" "alb_listener_details" 
33: elbv2.k8s.aws/cluster = "my-shiny-eks-cluster"

"An argument or block definition is required here.  To set an argument, use the equals sign "=" to introduce the argument value.

Hello all,

I found out the issue. It was a silly mistake on my part, although I really went down the rabbit hole and learnt a lot more things.

When I put in the tags value I forgot to include the equal to sign in the hash. Once I did that things started working as expected.

Thanks all!

Hi @njnerd! Thanks for sharing your solution.

Looking at what you shared, I expect you would also have needed to put that tag name in quotes, because the dots and slashes mean it isn’t a valid identifier in the HCL language. So presumably your final configuration looks something like this:

data "aws_lb" "alb_listener_details" {
  tags = {
    "elbv2.k8.aws/cluster" = "my-shiny-eks-cluster"
  }
}

(I’m posting this mainly because questions here tend to otherwise attract “I have the same problem! How did you solve it?” comments months later, so I’m trying to pre-empt that by sharing a working example! :grinning:)